以下代码可到: http://download.csdn.net/source/611385 下载
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:
- unit CE_Series;
- interface
- uses
- Windows,Classes, SysUtils, LResources, StdCtrls,ExtCtrls;
- type
- TCE_Series = class(TObject)
- private
- hComm: THandle;
- public
- Function OpenPort(Port:LPCWSTR;BaudRate,ByteSize,Parity,StopBits:integer):String;
- procedure Send(str:String);
- Function Receive():String;
- procedure ClosePort();
- end;
- implementation
- //===============================================================================================
- // 语法格式:OpenPort(Port:LPCWSTR;BaudRate,ByteSize,Parity,StopBits:integer)
- // 实现功能:打开串口
- // 参数:port,串口号;例如wince下为从COM1:,COM2:.....win32下为COM1,COM2....... ;其他略,顾名思义哈
- // 返回值:错误信息
- //===============================================================================================
- function TCE_Series.OpenPort(Port:LPCWSTR;BaudRate,ByteSize,Parity,StopBits:integer):String;
- var
- cc:TCOMMCONFIG;
- begin
- result:='';
- hComm:=CreateFile(port, GENERIC_READ or GENERIC_WRITE,
- 0, nil, OPEN_EXISTING, 0, 0); // 打开COM
- if (hComm = INVALID_HANDLE_VALUE) then begin // 如果COM 未打开
- result:='CreateFile Error!';
- exit;
- end;
- GetCommState(hComm,cc.dcb); // 得知目前COM 的状态
- cc.dcb.BaudRate:=BaudRate; // 设置波特率为BaudRate
- cc.dcb.ByteSize:=ByteSize; // 字节为 ByteSize(8 bit)
- cc.dcb.Parity:=Parity; // Parity 为 None
- cc.dcb.StopBits:=StopBits; // 1 个Stop bit
- if not SetCommState(hComm, cc.dcb) then begin// 设置COM 的状态
- result:='SetCommState Error!';
- CloseHandle(hComm);
- exit;
- end;
- end;
- //===============================================================================================
- // 语法格式:Send(str:String)
- // 实现功能:发送数据
- // 参数:str,数据
- // 返回值:无
- //===============================================================================================
- procedure TCE_Series.Send(str:String);
- var
- lrc:LongWord;
- begin
- if (hComm=0) then exit; //检查Handle值
- WriteFile(hComm,str,Length(str), lrc, nil); // 送出数据
- end;
- //=====================================================================
- //语法格式: Receive()
- //实现功能: 接收串口数据
- //参数: 无
- //返回值: 收到的字符串
- //=====================================================================
- Function TCE_Series.Receive():String;
- var
- inbuff: array[0..2047] of Char;
- nBytesRead, dwError:LongWORD ;
- cs:TCOMSTAT;
- begin
- ClearCommError(hComm,dwError,@CS); //取得状态
- // 数据是否大于我们所准备的Buffer
- if cs.cbInQue > sizeof(inbuff) then begin
- PurgeComm(hComm, PURGE_RXCLEAR); // 清除COM 数据
- exit;
- end;
- ReadFile(hComm, inbuff,cs.cbInQue,nBytesRead,nil); // 接收COM 的数据
- //转移数据到变量中
- result:=Copy(inbuff,1,cs.cbInQue);//返回数据
- end;
- //=====================================================================
- //语法格式: ClosePort()
- //实现功能:关闭串口
- //参数: 无
- //返回值: 无
- //=====================================================================
- procedure TCE_Series.ClosePort();
- begin
- SetCommMask(hcomm,$0);
- CloseHandle(hComm);
- end;
- end.
- unit Unit1;
- {$mode objfpc}{$H+}
- interface
- uses
- Windows,Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,ExtCtrls
- ,CE_Series;
- type
- { TForm1 }
- TForm1 = class(TForm)
- btn_OpenPort: TButton;
- btn_ClosePort: TButton;
- btn_Send: TButton;
- edt_Receive: TMemo;
- GroupBox1: TGroupBox;
- edt_Send: TMemo;
- GroupBox2: TGroupBox;
- Timer1: TTimer;
- procedure btn_ClosePortClick(Sender: TObject);
- procedure btn_OpenPortClick(Sender: TObject);
- procedure btn_SendClick(Sender: TObject);
- procedure Timer1Timer(Sender: TObject);
- private
- { private declarations }
- public
- { public declarations }
- end;
- var
- Form1: TForm1;
- myseries:TCE_Series;
- implementation
- { TForm1 }
- procedure TForm1.btn_OpenPortClick(Sender: TObject);
- begin
- myseries:=TCE_Series.Create;
- myseries.OpenPort('COM1:',CBR_9600,8,NOPARITY,ONESTOPBIT);
- Timer1.Enabled:=true;
- end;
- procedure TForm1.btn_SendClick(Sender: TObject);
- begin
- myseries.Send(edt_Send.Text);
- end;
- procedure TForm1.Timer1Timer(Sender: TObject); //用Timer定时接收数据
- var
- receive:string;
- begin
- receive:=myseries.Receive();
- if receive<>'' then
- begin
- edt_Receive.Lines.Add(receive); // 将数据显示于edt_Receive 上
- end;
- end;
- procedure TForm1.btn_ClosePortClick(Sender: TObject);
- begin
- Timer1.Enabled:=false;
- myseries.ClosePort();
- close;
- end;
- initialization
- {$I unit1.lrs}
- end.