[点评:MSCOMM属于WIN32中比较常用的控件,在DELPHI中要先进行安装ACTIVEX控件后,才可以使用.]
//发送数据
procedure senddata(astr:string);
var k:integer;
p:pchar;
achr:array of char;
begin
setlength(sbuf,length(astr));
setlength(achr,length(astr));
p:=pchar(astr);
for k:=0 to length(astr)-1 do
begin
achr[k]:=p^;
sbuf[k]:=ord(achr[k]);
p:=p+1;
end;
if frmMain.mscomm1.PortOpen=true then
begin
frmMain.mscomm1.Output:=sbuf;
end;
sleep(200);
end;
这是发送字符串的代码,如果是发送十六进制,直接用一个array of byte就行了,更加简单,接收类似.
//接收数据
procedure TfrmMain.MSComm1Comm(Sender: TObject);
var
v:Olevariant;
astr:string;
in_array:array of byte;
xtemp : Olevariant;
xScr : array of byte;
i, j : Integer;
xnNAK, xnRSD : Integer;
xRecData: String;
begin
i:=msComm1.InBufferCount;
if i=0 then
Exit;
if msComm1.CommEvent = 2 then
begin
xtemp := msComm1.Input;
SetLength(xScr,i);
xScr := xtemp;
for j:=0 to i-1 do
xRecData := xRecData +char(xScr[j]);
lstDisplay.Items.Add(FormatDateTime(hh:mm:ss, Now)
+: +xRecData);
lstDisplay.Items.Add();
end;
end;
阅读(1189) | 评论(0) | 转发(0) |