本文来自
http://blog.csdn.net/hellogv/ ,转载必须注明出处!
以下代码可到: 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:
二、写调用程序演示如何使用这个类,请自行加入控件,所用的控件不多:
以下代码可到: 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:
- unitCE_Series;
- interface
- uses
- Windows,Classes,SysUtils,LResources,StdCtrls,ExtCtrls;
- type
- TCE_Series=class(TObject)
- private
- hComm:THandle;
- public
- FunctionOpenPort(Port:LPCWSTR;BaudRate,ByteSize,Parity,StopBits:integer):String;
- procedureSend(str:String);
- FunctionReceive():String;
- procedureClosePort();
- end;
- implementation
- //===============================================================================================
- //语法格式:OpenPort(Port:LPCWSTR;BaudRate,ByteSize,Parity,StopBits:integer)
- //实现功能:打开串口
- //参数:port,串口号;例如wince下为从COM1:,COM2:.....win32下为COM1,COM2.......;其他略,顾名思义哈
- //返回值:错误信息
- //===============================================================================================
- functionTCE_Series.OpenPort(Port:LPCWSTR;BaudRate,ByteSize,Parity,StopBits:integer):String;
- var
- cc:TCOMMCONFIG;
- begin
- result:='';
- hComm:=CreateFile(port,GENERIC_READorGENERIC_WRITE,
- 0,nil,OPEN_EXISTING,0,0);//打开COM
- if(hComm=INVALID_HANDLE_VALUE)thenbegin//如果COM未打开
- result:='CreateFileError!';
- exit;
- end;
- GetCommState(hComm,cc.dcb);//得知目前COM的状态
- cc.dcb.BaudRate:=BaudRate;//设置波特率为BaudRate
- cc.dcb.ByteSize:=ByteSize;//字节为ByteSize(8bit)
- cc.dcb.Parity:=Parity;//Parity为None
- cc.dcb.StopBits:=StopBits;//1个Stopbit
- ifnotSetCommState(hComm,cc.dcb)thenbegin//设置COM的状态
- result:='SetCommStateError!';
- CloseHandle(hComm);
- exit;
- end;
- end;
- //===============================================================================================
- //语法格式:Send(str:String)
- //实现功能:发送数据
- //参数:str,数据
- //返回值:无
- //===============================================================================================
- procedureTCE_Series.Send(str:String);
- var
- lrc:LongWord;
- begin
- if(hComm=0)thenexit;//检查Handle值
- WriteFile(hComm,str,Length(str),lrc,nil);//送出数据
- end;
- //=====================================================================
- //语法格式:Receive()
- //实现功能:接收串口数据
- //参数:无
- //返回值:收到的字符串
- //=====================================================================
- FunctionTCE_Series.Receive():String;
- var
- inbuff:array[0..2047]ofChar;
- nBytesRead,dwError:LongWORD;
- cs:TCOMSTAT;
- begin
- ClearCommError(hComm,dwError,@CS);//取得状态
- //数据是否大于我们所准备的Buffer
- ifcs.cbInQue>sizeof(inbuff)thenbegin
- PurgeComm(hComm,PURGE_RXCLEAR);//清除COM数据
- exit;
- end;
- ReadFile(hComm,inbuff,cs.cbInQue,nBytesRead,nil);//接收COM的数据
- //转移数据到变量中
- result:=Copy(inbuff,1,cs.cbInQue);//返回数据
- end;
- //=====================================================================
- //语法格式:ClosePort()
- //实现功能:关闭串口
- //参数:无
- //返回值:无
- //=====================================================================
- procedureTCE_Series.ClosePort();
- begin
- SetCommMask(hcomm,$0);
- CloseHandle(hComm);
- end;
- end.
- unitUnit1;
- {$modeobjfpc}{$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;
- procedurebtn_ClosePortClick(Sender:TObject);
- procedurebtn_OpenPortClick(Sender:TObject);
- procedurebtn_SendClick(Sender:TObject);
- procedureTimer1Timer(Sender:TObject);
- private
- {privatedeclarations}
- public
- {publicdeclarations}
- end;
- var
- Form1:TForm1;
- myseries:TCE_Series;
- implementation
- {TForm1}
- procedureTForm1.btn_OpenPortClick(Sender:TObject);
- begin
- myseries:=TCE_Series.Create;
- myseries.OpenPort('COM1:',CBR_9600,8,NOPARITY,ONESTOPBIT);
- Timer1.Enabled:=true;
- end;
- procedureTForm1.btn_SendClick(Sender:TObject);
- begin
- myseries.Send(edt_Send.Text);
- end;
- procedureTForm1.Timer1Timer(Sender:TObject);//用Timer定时接收数据
- var
- receive:string;
- begin
- receive:=myseries.Receive();
- ifreceive<>''then
- begin
- edt_Receive.Lines.Add(receive);//将数据显示于edt_Receive上
- end;
- end;
- procedureTForm1.btn_ClosePortClick(Sender:TObject);
- begin
- Timer1.Enabled:=false;
- myseries.ClosePort();
- close;
- end;
- initialization
- {$Iunit1.lrs}
- end.