unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SPComm;
type
TForm1 = class(TForm)
smess: TEdit;
Button1: TButton;
Label2: TLabel;
Memo1: TMemo;
Label1: TLabel;
Memo2: TMemo;
Comm1: TComm;
procedure Button1Click(Sender: TObject);
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
s1,s2,SendInfo,sendmes:String;
i:integer;
fCheckValue:boolean;
begin
SendInfo:='';
if smess.Text='' then
showmessage('请输入数据!');
s1:=smess.text+',';
fCheckValue:=True;
for i:=1 to length(s1) do
begin
case s1 of
',': begin
case length(s2) of
2: SendInfo:=SendInfo+char(StrToInt('$'+s2));
1: SendInfo:=SendInfo+s2;
0: SendInfo:=SendInfo;
else
fCheckValue:=False;
end;
s2:='';
end
else
s2:=s2+s1;
end;
end;
if fCheckValue then
begin
Memo2.Lines.Add(s1);
comm1.WriteCommData(Pchar(SendInfo),length(SendInfo));
end
else
showmessage('输入数据的格式不正确'#13'信息间采用,区分'#13'注意字符串长度');
end;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var rx,s10:string;
i:integer;
begin
SetLength(rx,BufferLength);
move(buffer^,Pchar(rx)^,BufferLength);
for i:=1 to Length(rx) do
begin
s10:=s10+'$'+intToHex(ord(rx),2)+',';
end;
Memo1.Lines.Add(s10);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
try
comm1.StartComm;
except
showmessage('打开串口失败');
end;
end;
end.