Q
qiujianben
Unregistered / Unconfirmed
GUEST, unregistred user!
协议:
头:D5C8D5C8
指令:FF
内容:信息<1000
尾:13AB13AB
现在发现:数据发送不出去,com.WriteCommData(send_txt,length(strHyData)) 返回值始终是false;
这是什么问题啊?头痛死了。
头:D5C8D5C8
指令:FF
内容:信息<1000
尾:13AB13AB
现在发现:数据发送不出去,com.WriteCommData(send_txt,length(strHyData)) 返回值始终是false;
这是什么问题啊?头痛死了。
代码:
unit CommConvert;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, RzCmboBx, ExtCtrls, RzPanel, RzButton, SPComm, RzLabel,
Mask, RzEdit, RzShellDialogs;
type
TfrmMain = class(TForm)
Com: TComm;
RzButtonHelp: TRzButton;
RzButtonBrowse: TRzButton;
RzGroupBoxInit: TRzGroupBox;
RzComboBoxComNo: TRzComboBox;
RzBtnOpenCom: TRzButton;
RzLabComSelect: TRzLabel;
RzLabBand: TRzLabel;
RzComboBoxBand: TRzComboBox;
RzLabelData: TRzLabel;
RzLabelCheck: TRzLabel;
RzLabelStop: TRzLabel;
RzComboBoxData: TRzComboBox;
RzComboBoxStop: TRzComboBox;
RzComboBoxCheck: TRzComboBox;
RzLabelFolder: TRzLabel;
RzEditFolder: TRzEdit;
RzButtonSend: TRzButton;
RzLabelShow: TRzLabel;
RzMemo: TRzMemo;
RzLabelComStates: TRzLabel;
RzOpenDialog: TRzOpenDialog;
procedure RzButtonBrowseClick(Sender: TObject);
procedure RzBtnOpenComClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure RzButtonSendClick(Sender: TObject);
private
{ Private declarations }
IsOpen: boolean;
procedure InitConfig;
function ReadFiles(aFileName:String):String;
function strToPchar(str:string):pchar;
public
{ Public declarations }
end;
var
frmMain: TfrmMain;
implementation
{$R *.dfm}
//获得数据文件路径以及名称
procedure TfrmMain.RzButtonBrowseClick(Sender: TObject);
begin
if (RzOpenDialog.Execute) then
RzEditFolder.Text := RzOpenDialog.FileName;
end;
//打开串口和关闭串口
procedure TfrmMain.RzBtnOpenComClick(Sender: TObject);
begin
if not IsOpen then
begin
InitConfig;
Com.StartComm;
RzBtnOpenCom.Caption := '关闭串口';
RzLabelComStates.Caption := 'OK';
IsOpen := True;
end
else
begin
com.StopComm;
RzBtnOpenCom.Caption := '打开串口';
RzLabelComStates.Caption := '';
IsOpen := false;
end;
end;
//程序开始时候执行基本参数设置
procedure TfrmMain.FormCreate(Sender: TObject);
begin
IsOpen:= False;
end;
//初始化串口基本参数
procedure TfrmMain.InitConfig;
begin
Com.CommName := RzComboBoxComNo.Text; //串口号
Com.BaudRate := StrToInt(RzComboBoxBand.Text); //波特率
case RzComboBoxData.ItemIndex of //数据位
0: Com.ByteSize := _5;
1: Com.ByteSize := _6;
2: Com.ByteSize := _7;
3: Com.ByteSize := _8;
end;
case RzComboBoxStop.ItemIndex of //停止位
0: com.StopBits := _1;
1: com.StopBits := _1_5;
2: com.StopBits := _2;
end;
case RzComboBoxCheck.ItemIndex of //校验位
0: com.Parity := None;
1: com.Parity := Odd;
2: com.Parity := Even;
3: com.Parity := Mark;
4: com.Parity := Space;
end;
end;
//数据资料发送
procedure TfrmMain.RzButtonSendClick(Sender: TObject);
var
strHyData :String;
send_txt :PChar;
begin
if (RzEditFolder.Text ='') then
begin
ShowMessage('请选择资料存放目录');
end
else
begin
RzMemo.Lines.Text := ReadFiles(RzEditFolder.Text);
strHyData := 'D5C8D5C8'+ReadFiles(RzEditFolder.Text)+'13AB13AB';
send_txt:= strToPchar(strHyData);
if com.WriteCommData(send_txt,length(strHyData)) then
ShowMessage('数据发送成功!')
else
ShowMessage('数据发送失败!');
FreeMem(send_txt);
end
end;
//读取海洋数据监测资料函数
function TfrmMain.ReadFiles(aFileName: String): String;
var
HyData : TStringList;
begin
try
HyData := TStringList.Create;
HyData.LoadFromFile(aFileName);
Result := HyData.Text;
finally
HyData.Free;
end
end;
function TfrmMain.strToPchar(str: string): pchar;
var
p:pchar;
begin
p:=AllocMem(Length(str)+1);
strpcopy(p,str);
result:=p;
end;
end.