\t\tLazarus实战开发之串口通信(WINCE/WIN32)

Lazarus最吸引人的地方就是她的开发方式类似Delphi,支持超好用的RAD开发方式,并且最厉害的地方是她还支持多个平台,多个CPU,例如ARM9的WINCE。

  本文要讲述的就是“如何使用LAZARUS开发Wince上的串口程序”,并且,本文的串口程序同时支持WINCE和WINXP系统,当然编译时要选择平台啦。WINCE与WINXP在本文中的代码区别只是OpenPort('COM1:',CBR_9600,8,NOPARITY,ONESTOPBIT);//wince用COM1:表示串口1;WINXP用COM1表示串口1.

  一、建立一个可重用的类,文件名为CE_Series.pas:

   1. unit CE_Series;
   2. interface
   3. uses
   4.   Windows,Classes, SysUtils, LResources, StdCtrls,ExtCtrls;
   5.  
   6.
   7. type
   8.   TCE_Series = class(TObject)
   9.  
  10.   private
  11.     hComm: THandle;
  12.   public
  13.     Function OpenPort(Port:LPCWSTR;BaudRate,ByteSize,Parity,StopBits:integer):String;
  14.     procedure Send(str:String);
  15.     Function Receive():String;
  16.     procedure ClosePort();
  17.   end;
  18.
  19.
  20. implementation
  21.
  22.
  23. //===============================================================================================
  24. // 语法格式:OpenPort(Port:LPCWSTR;BaudRate,ByteSize,Parity,StopBits:integer)
  25. // 实现功能:打开串口
  26. // 参数:port,串口号;例如wince下为从COM1:,COM2:.....win32下为COM1,COM2....... ;其他略,顾名思义哈
  27. // 返回值:错误信息
  28. //===============================================================================================
  29. function TCE_Series.OpenPort(Port:LPCWSTR;BaudRate,ByteSize,Parity,StopBits:integer):String;
  30. var
  31.   cc:TCOMMCONFIG;
  32. begin
  33.   result:='';
  34.   hComm:=CreateFile(port, GENERIC_READ or GENERIC_WRITE,
  35.        0, nil, OPEN_EXISTING, 0, 0); // 打开COM
  36.   if (hComm = INVALID_HANDLE_VALUE) then begin  // 如果COM 未打开
  37.     result:='CreateFile Error!';
  38.     exit;
  39.   end;
  40.
  41.   GetCommState(hComm,cc.dcb); // 得知目前COM 的状态
  42.   cc.dcb.BaudRate:=BaudRate; // 设置波特率为BaudRate
  43.   cc.dcb.ByteSize:=ByteSize;  // 字节为 ByteSize(8 bit)
  44.   cc.dcb.Parity:=Parity; // Parity 为 None
  45.   cc.dcb.StopBits:=StopBits; // 1 个Stop bit
  46.  
  47.   if not SetCommState(hComm, cc.dcb) then begin// 设置COM 的状态
  48.     result:='SetCommState Error!';
  49.     CloseHandle(hComm);
  50.     exit;
  51.   end;
  52. end;
  53.
  54. //===============================================================================================
  55. // 语法格式:Send(str:String)
  56. // 实现功能:发送数据
  57. // 参数:str,数据
  58. // 返回值:无
  59. //===============================================================================================
  60. procedure TCE_Series.Send(str:String);
  61. var
  62.   lrc:LongWord;
  63. begin
  64.   if (hComm=0) then exit; //检查Handle值
  65.   WriteFile(hComm,str,Length(str), lrc, nil); // 送出数据
  66. end;
  67.
  68.
  69. //=====================================================================
  70. //语法格式: Receive()
  71. //实现功能: 接收串口数据
  72. //参数:     无
  73. //返回值:   收到的字符串
  74. //=====================================================================
  75. Function TCE_Series.Receive():String;
  76. var
  77.   inbuff: array[0..2047] of Char;
  78.   nBytesRead, dwError:LongWORD ;
  79.   cs:TCOMSTAT;
  80. begin
  81.    ClearCommError(hComm,dwError,@CS);  //取得状态
  82.        // 数据是否大于我们所准备的Buffer
  83.    if cs.cbInQue > sizeof(inbuff) then begin
  84.      PurgeComm(hComm, PURGE_RXCLEAR);  // 清除COM 数据
  85.      exit;
  86.    end;
  87.    ReadFile(hComm, inbuff,cs.cbInQue,nBytesRead,nil); // 接收COM 的数据
  88.    //转移数据到变量中
  89.    result:=Copy(inbuff,1,cs.cbInQue);//返回数据
  90. end;                            
  91.
  92.
  93. //=====================================================================
  94. //语法格式: ClosePort()
  95. //实现功能:关闭串口
  96. //参数:  无
  97. //返回值:  无
  98. //=====================================================================
  99. procedure TCE_Series.ClosePort();
 100. begin
 101.    SetCommMask(hcomm,$0);
 102.    CloseHandle(hComm);
 103. end;
 104.
 105.
 106. end.

 

二、写调用程序演示如何使用这个类,请自行加入控件,所用的控件不多:

   1. unit Unit1;
   2.
   3. {$mode objfpc}{$H+}
   4.
   5. interface
   6.
   7. uses
   8.   Windows,Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,ExtCtrls
   9.   ,CE_Series;
  10.
  11. type
  12.
  13.   { TForm1 }
  14.
  15.   TForm1 = class(TForm)
  16.     btn_OpenPort: TButton;
  17.     btn_ClosePort: TButton;
  18.     btn_Send: TButton;
  19.     edt_Receive: TMemo;
  20.     GroupBox1: TGroupBox;
  21.     edt_Send: TMemo;
  22.     GroupBox2: TGroupBox;
  23.     Timer1: TTimer;
  24.     procedure btn_ClosePortClick(Sender: TObject);
  25.     procedure btn_OpenPortClick(Sender: TObject);
  26.     procedure btn_SendClick(Sender: TObject);
  27.     procedure Timer1Timer(Sender: TObject);
  28.   private
  29.     { private declarations }
  30.   public
  31.     { public declarations }
  32.   end;
  33.
  34. var
  35.   Form1: TForm1;
  36.   myseries:TCE_Series;
  37.  
  38. implementation
  39.
  40. { TForm1 }
  41.
  42. procedure TForm1.btn_OpenPortClick(Sender: TObject);
  43. begin
  44.   myseries:=TCE_Series.Create;
  45.   myseries.OpenPort('COM1:',CBR_9600,8,NOPARITY,ONESTOPBIT);
  46.   Timer1.Enabled:=true;
  47. end;
  48.
  49. procedure TForm1.btn_SendClick(Sender: TObject);
  50. begin
  51.   myseries.Send(edt_Send.Text);
  52. end;
  53.
  54. procedure TForm1.Timer1Timer(Sender: TObject); //用Timer定时接收数据
  55. var
  56.   receive:string;
  57. begin
  58.    receive:=myseries.Receive();
  59.    if receive<>'' then
  60.    begin
  61.       edt_Receive.Lines.Add(receive);   // 将数据显示于edt_Receive 上
  62.    end;
  63. end;
  64.
  65. procedure TForm1.btn_ClosePortClick(Sender: TObject);
  66. begin
  67.    Timer1.Enabled:=false;
  68.    myseries.ClosePort();
  69.    close;
  70. end;
  71.
  72. initialization
  73.   {$I unit1.lrs}
  74.
  75. end.
  76.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值