本文來自 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:
- 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.