`
izuoyan
  • 浏览: 8928100 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Lazarus实战开发之串口通信(WINCE/WIN32)

阅读更多
本文来自 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. unitCE_Series;
  2. interface
  3. uses
  4. Windows,Classes,SysUtils,LResources,StdCtrls,ExtCtrls;
  5. type
  6. TCE_Series=class(TObject)
  7. private
  8. hComm:THandle;
  9. public
  10. FunctionOpenPort(Port:LPCWSTR;BaudRate,ByteSize,Parity,StopBits:integer):String;
  11. procedureSend(str:String);
  12. FunctionReceive():String;
  13. procedureClosePort();
  14. end;
  15. implementation
  16. //===============================================================================================
  17. //语法格式:OpenPort(Port:LPCWSTR;BaudRate,ByteSize,Parity,StopBits:integer)
  18. //实现功能:打开串口
  19. //参数:port,串口号;例如wince下为从COM1:,COM2:.....win32下为COM1,COM2.......;其他略,顾名思义哈
  20. //返回值:错误信息
  21. //===============================================================================================
  22. functionTCE_Series.OpenPort(Port:LPCWSTR;BaudRate,ByteSize,Parity,StopBits:integer):String;
  23. var
  24. cc:TCOMMCONFIG;
  25. begin
  26. result:='';
  27. hComm:=CreateFile(port,GENERIC_READorGENERIC_WRITE,
  28. 0,nil,OPEN_EXISTING,0,0);//打开COM
  29. if(hComm=INVALID_HANDLE_VALUE)thenbegin//如果COM未打开
  30. result:='CreateFileError!';
  31. exit;
  32. end;
  33. GetCommState(hComm,cc.dcb);//得知目前COM的状态
  34. cc.dcb.BaudRate:=BaudRate;//设置波特率为BaudRate
  35. cc.dcb.ByteSize:=ByteSize;//字节为ByteSize(8bit)
  36. cc.dcb.Parity:=Parity;//Parity为None
  37. cc.dcb.StopBits:=StopBits;//1个Stopbit
  38. ifnotSetCommState(hComm,cc.dcb)thenbegin//设置COM的状态
  39. result:='SetCommStateError!';
  40. CloseHandle(hComm);
  41. exit;
  42. end;
  43. end;
  44. //===============================================================================================
  45. //语法格式:Send(str:String)
  46. //实现功能:发送数据
  47. //参数:str,数据
  48. //返回值:无
  49. //===============================================================================================
  50. procedureTCE_Series.Send(str:String);
  51. var
  52. lrc:LongWord;
  53. begin
  54. if(hComm=0)thenexit;//检查Handle值
  55. WriteFile(hComm,str,Length(str),lrc,nil);//送出数据
  56. end;
  57. //=====================================================================
  58. //语法格式:Receive()
  59. //实现功能:接收串口数据
  60. //参数:无
  61. //返回值:收到的字符串
  62. //=====================================================================
  63. FunctionTCE_Series.Receive():String;
  64. var
  65. inbuff:array[0..2047]ofChar;
  66. nBytesRead,dwError:LongWORD;
  67. cs:TCOMSTAT;
  68. begin
  69. ClearCommError(hComm,dwError,@CS);//取得状态
  70. //数据是否大于我们所准备的Buffer
  71. ifcs.cbInQue>sizeof(inbuff)thenbegin
  72. PurgeComm(hComm,PURGE_RXCLEAR);//清除COM数据
  73. exit;
  74. end;
  75. ReadFile(hComm,inbuff,cs.cbInQue,nBytesRead,nil);//接收COM的数据
  76. //转移数据到变量中
  77. result:=Copy(inbuff,1,cs.cbInQue);//返回数据
  78. end;
  79. //=====================================================================
  80. //语法格式:ClosePort()
  81. //实现功能:关闭串口
  82. //参数:无
  83. //返回值:无
  84. //=====================================================================
  85. procedureTCE_Series.ClosePort();
  86. begin
  87. SetCommMask(hcomm,$0);
  88. CloseHandle(hComm);
  89. end;
  90. end.
二、写调用程序演示如何使用这个类,请自行加入控件,所用的控件不多:
  1. unitUnit1;
  2. {$modeobjfpc}{$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. procedurebtn_ClosePortClick(Sender:TObject);
  19. procedurebtn_OpenPortClick(Sender:TObject);
  20. procedurebtn_SendClick(Sender:TObject);
  21. procedureTimer1Timer(Sender:TObject);
  22. private
  23. {privatedeclarations}
  24. public
  25. {publicdeclarations}
  26. end;
  27. var
  28. Form1:TForm1;
  29. myseries:TCE_Series;
  30. implementation
  31. {TForm1}
  32. procedureTForm1.btn_OpenPortClick(Sender:TObject);
  33. begin
  34. myseries:=TCE_Series.Create;
  35. myseries.OpenPort('COM1:',CBR_9600,8,NOPARITY,ONESTOPBIT);
  36. Timer1.Enabled:=true;
  37. end;
  38. procedureTForm1.btn_SendClick(Sender:TObject);
  39. begin
  40. myseries.Send(edt_Send.Text);
  41. end;
  42. procedureTForm1.Timer1Timer(Sender:TObject);//用Timer定时接收数据
  43. var
  44. receive:string;
  45. begin
  46. receive:=myseries.Receive();
  47. ifreceive<>''then
  48. begin
  49. edt_Receive.Lines.Add(receive);//将数据显示于edt_Receive上
  50. end;
  51. end;
  52. procedureTForm1.btn_ClosePortClick(Sender:TObject);
  53. begin
  54. Timer1.Enabled:=false;
  55. myseries.ClosePort();
  56. close;
  57. end;
  58. initialization
  59. {$Iunit1.lrs}
  60. end.


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics