国产成人精品a视频一区www_国产区视频在线观看_99色视频_欲色av_亚洲一区电影_亚洲综合视频一区

Lazarus實戰(zhàn)開發(fā)之串口通信(WINCE/WIN32)

來源:網(wǎng)絡(luò)

點擊:1867

A+ A-

所屬頻道:新聞中心

關(guān)鍵詞: WINCE,WIN32,串口通信,Lazarus

      Lazarus最吸引人的地方就是她的開發(fā)方式類似Delphi,支持超好用的RAD開發(fā)方式,并且最厲害的地方是她還支持多個平臺,多個CPU,例如ARM9的WINCE。

      本文要講述的就是“如何使用LAZARUS開發(fā)Wince上的串口程序”,并且,本文的串口程序同時支持WINCE和WINXP系統(tǒng),當(dāng)然編譯時要選擇平臺啦。WINCE與WINXP在本文中的代碼區(qū)別只是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)

      // 實現(xiàn)功能:打開串口

      // 參數(shù):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 的狀態(tài)

      cc.dcb.BaudRate:=BaudRate; // 設(shè)置波特率為BaudRate

      cc.dcb.ByteSize:=ByteSize; // 字節(jié)為 ByteSize(8 bit)

      cc.dcb.Parity:=Parity; // Parity 為 None

      cc.dcb.StopBits:=StopBits; // 1 個Stop bit

      if not SetCommState(hComm, cc.dcb) then begin// 設(shè)置COM 的狀態(tài)

      result:=‘SetCommState Error!’;

      CloseHandle(hComm);

      exit;

      end;

      end;

      //===============================================================================================

      // 語法格式:Send(str:String)

      // 實現(xiàn)功能:發(fā)送數(shù)據(jù)

      // 參數(shù):str,數(shù)據(jù)

      // 返回值:無

      //===============================================================================================

      procedure TCE_Series.Send(str:String);

      var

      lrc:LongWord;

      begin

      if (hComm=0) then exit; //檢查Handle值

      WriteFile(hComm,str,Length(str), lrc, nil); // 送出數(shù)據(jù)

      end;

      //=====================================================================

      //語法格式: Receive()

      //實現(xiàn)功能: 接收串口數(shù)據(jù)

      //參數(shù): 無

      //返回值: 收到的字符串

      //=====================================================================

      Function TCE_Series.Receive():String;

      var

      inbuff: array[0..2047] of Char;

      nBytesRead, dwError:LongWORD ;

      cs:TCOMSTAT;

      begin

      ClearCommError(hComm,dwError,@CS); //取得狀態(tài)

      // 數(shù)據(jù)是否大于我們所準(zhǔn)備的Buffer

      if cs.cbInQue 》 sizeof(inbuff) then begin

      PurgeComm(hComm, PURGE_RXCLEAR); // 清除COM 數(shù)據(jù)

      exit;

      end;

      ReadFile(hComm, inbuff,cs.cbInQue,nBytesRead,nil); // 接收COM 的數(shù)據(jù)

      //轉(zhuǎn)移數(shù)據(jù)到變量中

      result:=Copy(inbuff,1,cs.cbInQue);//返回數(shù)據(jù)

      end;

      //=====================================================================

      //語法格式: ClosePort()

      //實現(xiàn)功能:關(guān)閉串口

      //參數(shù): 無

      //返回值: 無

      //=====================================================================

      procedure TCE_Series.ClosePort();

      begin

      SetCommMask(hcomm,$0);

      CloseHandle(hComm);

      end;

      end.

      二、寫調(diào)用程序演示如何使用這個類,請自行加入控件,所用的控件不多:

      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定時接收數(shù)據(jù)

      var

      receive:string;

      begin

      receive:=myseries.Receive();

      if receive《》‘’ then

      begin

      edt_Receive.Lines.Add(receive); // 將數(shù)據(jù)顯示于edt_Receive 上

      end;

      end;

      procedure TForm1.btn_ClosePortClick(Sender: TObject);

      begin

      Timer1.Enabled:=false;

      myseries.ClosePort();

      close;

      end;

      initialization

      {$I unit1.lrs}

      end.

     

    (審核編輯: 智匯小新)

    聲明:除特別說明之外,新聞內(nèi)容及圖片均來自網(wǎng)絡(luò)及各大主流媒體。版權(quán)歸原作者所有。如認(rèn)為內(nèi)容侵權(quán),請聯(lián)系我們刪除。

    主站蜘蛛池模板: 亚洲激情在线 | 粉嫩高清一区二区三区精品视频 | 亚洲一区二区三区免费在线 | 欧美在线高清 | 中文字幕天堂 | 欧洲妇女成人淫片aaa视频 | 亚洲精品www久久久久久广东 | 在线观看不卡 | 91午夜在线| 久久精品久久久久久 | 久久噜噜噜精品国产亚洲综合 | 亚洲电影一级片 | 久久久久国产精品 | 国产亚洲女人久久久久毛片 | 涩涩鲁亚洲精品一区二区 | 国产欧美综合一区二区三区 | 91精品国产高清一区二区三区 | 成人99| 免费一级黄色电影 | 国产男女视频在线观看 | 欧美日韩国产中文 | 亚洲一区二区三区四区在线 | 欧美三级电影在线 | 欧美日韩第一 | 日本中文字幕一区 | 精品亚洲成人 | 99精品国产在热久久 | 亚洲午夜在线 | 日本精品一区 | 蜜桃精品久久久久久久免费影院 | 日本一区二区精品 | 奇米精品一区二区三区在线观看 | 国产欧美日韩中文字幕 | 日本三级欧美三级 | av日韩一区 | 99r在线| 91小视频| 久久亚洲一区 | 国产三级在线免费观看 | 涩涩亚洲 | 免费国产视频 |