文档库 最新最全的文档下载
当前位置:文档库 › 在delphi7中利用mscomm控件串口通信编程

在delphi7中利用mscomm控件串口通信编程

其他相关参考人民邮电《DELPHI串口通信编程》
-其他例子均是网络搜索。
:在delphi7中利用mscomm控件编程

Mscomm是微软一个强大的串口通讯的控件,其强大,简单的特点今我们不舍,在delphi中我们也可以使用它。下面这些代码是针对发送和接收为16进制处理的。
var
senddata:array[1..10] of char;
reData:array of Variant;
sendstr:string;
restr:string;
i:longint;
begin
https://www.wendangku.net/doc/074676852.html,mPort := 1; //指定端口
mscomm1.Settings := '9600,N,8,1'; //其它参数
mscomm1.InBufferSize := 1024; //接收缓冲区
mscomm1.OutBufferSize := 1024; //发送缓冲区
mscomm1.InputMode := comInputModeBinary; //接收模式
mscomm1.InputLen := 0; //一次读取所有数据
mscomm1.SThreshold := 0; //一次发送所有数据
mscomm1.InBufferCount := 0; //清空读取缓冲区
mscomm1.OutBufferCount := 0; //清空发送缓冲区
mscomm1.PortOpen:=true; //打开端口
MSComm1.RThreshold := 16; //设置接收多少字节开产生oncomm事件
senddata[1]:=chr($06); //要发送的数据
senddata[2]:=chr($03);
senddata[3]:=chr($00);
senddata[4]:=chr($03);
senddata[5]:=chr($10);
sendstr:='';
for i:=1 to 5 do
sendstr:=sendstr + senddata[i];
mscomm1.output:=sendstr; //发送数据
i:=0;
bzw:=false;

repeat
sleep(10);
Application.ProcessMessages;
i := i + 1;
If i > 30000 Then
begin
showmessage('发送超时!');
break;
end;
Until bzw = true;
redata:=mscomm1.Input; 接收数据
restr:='';
for i:=0 to vararrayhighbound(redata,1) do
restr:=restr + inttohex(redata[i],2)+' ';
mscomm1.PortOpen:=false;
flatmemo1.Text:=restr;
end;
//oncomm事件
procedure TForm1.MSComm1Comm(Sender: TObject);
begin
case https://www.wendangku.net/doc/074676852.html,mEvent of
comEvReceive: bzw := true;
end;
end;

相关文档