Lazarus實戰開發之串口通訊(WINCE/WIN32)

來源:互聯網
上載者:User
本文來自 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:

  1. unit CE_Series;
  2. interface
  3. uses
  4.   Windows,Classes, SysUtils, LResources, StdCtrls,ExtCtrls;
  5.   
  6. type
  7.   TCE_Series = class(TObject)
  8.   
  9.   private
  10.     hComm: THandle;
  11.   public
  12.     Function OpenPort(Port:LPCWSTR;BaudRate,ByteSize,Parity,StopBits:integer):String;
  13.     procedure Send(str:String);
  14.     Function Receive():String;
  15.     procedure ClosePort();
  16.   end;
  17. implementation
  18. //===============================================================================================
  19. // 文法格式:OpenPort(Port:LPCWSTR;BaudRate,ByteSize,Parity,StopBits:integer)
  20. // 實現功能:開啟串口
  21. // 參數:port,串口號;例如wince下為從COM1:,COM2:.....win32下為COM1,COM2....... ;其他略,顧名思義哈
  22. // 傳回值:錯誤資訊
  23. //===============================================================================================
  24. function TCE_Series.OpenPort(Port:LPCWSTR;BaudRate,ByteSize,Parity,StopBits:integer):String;
  25. var
  26.   cc:TCOMMCONFIG;
  27. begin
  28.   result:='';
  29.   hComm:=CreateFile(port, GENERIC_READ or GENERIC_WRITE,
  30.        0, nil, OPEN_EXISTING, 0, 0); // 開啟COM
  31.   if (hComm = INVALID_HANDLE_VALUE) then begin  // 如果COM 未開啟
  32.     result:='CreateFile Error!';
  33.     exit;
  34.   end;
  35.   GetCommState(hComm,cc.dcb); // 得知目前COM 的狀態
  36.   cc.dcb.BaudRate:=BaudRate; // 設定傳輸速率為BaudRate
  37.   cc.dcb.ByteSize:=ByteSize;  // 位元組為 ByteSize(8 bit)
  38.   cc.dcb.Parity:=Parity; // Parity 為 None
  39.   cc.dcb.StopBits:=StopBits; // 1 個Stop bit
  40.   
  41.   if not SetCommState(hComm, cc.dcb) then begin// 設定COM 的狀態
  42.     result:='SetCommState Error!';
  43.     CloseHandle(hComm);
  44.     exit;
  45.   end;
  46. end;
  47. //===============================================================================================
  48. // 文法格式:Send(str:String)
  49. // 實現功能:發送資料
  50. // 參數:str,資料
  51. // 傳回值:無
  52. //===============================================================================================
  53. procedure TCE_Series.Send(str:String);
  54. var
  55.   lrc:LongWord;
  56. begin
  57.   if (hComm=0) then exit; //檢查Handle值
  58.   WriteFile(hComm,str,Length(str), lrc, nil); // 送出資料
  59. end;
  60. //=====================================================================
  61. //文法格式: Receive()
  62. //實現功能: 接收串口資料
  63. //參數:     無
  64. //傳回值:   收到的字串
  65. //=====================================================================
  66. Function TCE_Series.Receive():String;
  67. var
  68.   inbuff: array[0..2047] of Char;
  69.   nBytesRead, dwError:LongWORD ;
  70.   cs:TCOMSTAT;
  71. begin
  72.    ClearCommError(hComm,dwError,@CS);  //取得狀態
  73.        // 資料是否大於我們所準備的Buffer
  74.    if cs.cbInQue > sizeof(inbuff) then begin
  75.      PurgeComm(hComm, PURGE_RXCLEAR);  // 清除COM 資料
  76.      exit;
  77.    end;
  78.    ReadFile(hComm, inbuff,cs.cbInQue,nBytesRead,nil); // 接收COM 的資料
  79.    //轉移資料到變數中
  80.    result:=Copy(inbuff,1,cs.cbInQue);//返回資料
  81. end;                             
  82. //=====================================================================
  83. //文法格式: ClosePort()
  84. //實現功能:關閉串口
  85. //參數:  無
  86. //傳回值:  無
  87. //=====================================================================
  88. procedure TCE_Series.ClosePort();
  89. begin
  90.    SetCommMask(hcomm,$0);
  91.    CloseHandle(hComm);
  92. end;
  93. end.

二、寫調用程式示範如何使用這個類,請自行加入控制項,所用的控制項不多:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.