//接收
procedure TForm1.MSComm1Comm(Sender: TObject);
var
i,j:integer;
rxbuf:Variant;
a:array of byte;
s:string;
begin
if (Mscomm1.CommEvent=2) then
while Mscomm1.inbuffercount<>0 do
begin
a:=mscomm1.input;
Memo1.SelStart:=Length(Memo1.Text);
if CheckBox4.Checked //HEX show
then Memo1.SelText:=StrToHex(char(a[0]))
else Memo1.SelText:=Char(a[0]);
end;
end;
//向串口转发数据
procedure TForm1.Button8Click(Sender: TObject);
begin
if CheckBox6.Checked
then MsComm1.Output:=HexToStr(Memo2.Text)//16进制发送
else //ASC 发送
if CheckBox3.Checked
then MsComm1.Output:=Memo2.Text+#13#10 //回车换行
else MsComm1.Output:=Memo2.Text;
end;
//HEX字串转ASC字串
function HexToStr(SourceString : string) : string;
var i:integer;
begin
result:='';
i:=1;
while i<=length(SourceString) do
if SourceString<>' '
then
if SourceString[i+1]=' '
then
begin
result:=result+char(StrToInt('$'+SourceString));
i:=i+2;
end
else
begin
result:=result+char(StrToInt('$'+SourceString+SourceString[i+1]));
i:=i+2;
end
else i:=i+1;
end;