国产成人精品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)系我們刪除。

    主站蜘蛛池模板: 午夜精品一区二区三区免费视频 | 中文字幕av在线 | 精品国产乱码久久久久久闺蜜 | 四虎最新紧急更新地址 | 青青久 | 五月婷婷综合久久 | 日韩草比 | 91在线一区二区三区 | 国产97碰免费视频 | 欧美一二三区在线观看 | 色激情五月 | 免费视频二区 | 七龙珠z普通话国语版在线观看 | 日韩av免费在线观看 | 亚洲日韩欧美一区二区在线 | 999在线视频免费观看 | av超碰| 久久亚洲高清 | av网站在线免费观看 | 夜夜天天操 | 美国黄色毛片女人性生活片 | 欧美一区二区三区免费观看 | 欧美成人久久久免费播放 | 精品国产一区二区三区久久 | 国产suv精品一区二区六 | 欧美精品1区2区 | 国产婷婷精品 | 欧美精品一 | 国产成人在线不卡 | 欧美成人不卡 | 天天天插 | 色www精品视频在线观看 | 色综合网址 | 亚洲一区二区中文字幕 | 91视频免费观看网址 | 国产精品91av | 精品欧美乱码久久久久久 | 国产精品视频看看 | 亚洲精品亚洲人成人网 | 日韩视频中文 | 一区二区三区视频 |