当前位置:主页>Delphi教程>文章内容
在delphi7中利用mscomm控件编程
来源: 作者: 发布时间:2007-04-29  
Mscomm是微软一个强大的串口通讯的控件,其强大,简单的特点今我们不舍,在delphi中我们也可以使用它。下面这些代码是针对发送和接收为16进制处理的。
var
  senddata:array[1..10] of char;
  reData:array of Variant;
  sendstr:string;
  restr:string;
  i:longint;
begin
  mscomm1.CommPort := 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 mscomm1.CommEvent of
    comEvReceive: bzw := true;
  end;
end;

最后,记事发布软件时要带上Mscomm32.ocx文件。


 
上一篇:DBGrid中用光标键控制Cell   下一篇:检查RS232(串口)是否接有设备
 
  相关文章
·DBGrid中用光标键控制Cell
·检查RS232(串口)是否接有设备
·一个很实用的Ehlib排序函数,适合ADO
·delphi2005探讨之四
·用DELPHI开发AUTHORWARE的u32
·创建Photoshop式浮动窗口应用程序
·关于开发系统后门软件的几点思路
·Delphi+MapX5中使用自定义字体符号
·实现QQ窗体的缩入伸出功能
·进程查看器1.3版开发后记
·点击DBGrid的Title对查询结果排序
·Delphi控件制作技巧[一]
 
【关闭窗口】
推荐本站资源
最新文章