cport串口控件的应用

cport是一个很强大的串口控件。支持delphi/bcb.最新版本是v4.10.官网下载地址是:http://sourceforge.net/projects/comport/files/comport/

,配有详细的说明文档。在这我仅仅作为一个总结,方便以后自己查看。

由于我认为新学习pascal语言是件很头疼的事情!所以最终我决定使用bcb作为windows上的编程工具了。

安装好cport控件后在bcb的控件栏中将会多出如图示的cport控件。

常用的有ComPort和ComDataPacket控件,本例中我只用了ComPort控件实现简单的串口发送接收功能。

(1).将ComPort拖至窗体上,设置属性页中的DiscardNull为true,ControlDTR为dtrEnable.

(2).添加三个按钮控件到窗体,分别作为“设置串口”“打开/关闭串口”“发送”。

(3).添加Edit控件作为输入将要发送的数据,添加memo作为显示接收的数据。

如图示:

(4).分别双击三个按钮事件,“设置串口”“打开/关闭串口”“发送”函数。

(5).接收数据关键的一步:选中cport控件,进入其Events页,双击OnRxChar编写接收函数。(之前我一直是手动添加的该函数,导致最后没有与控件的events关联起来,从而无法正确接收数据,纠结的2天!)如图示:

 

代码:

1.com_pro.h

[cpp] view plain copy
 
 print?
  1. //---------------------------------------------------------------------------  
  2.   
  3. #ifndef Unit1H  
  4. #define Unit1H  
  5. //---------------------------------------------------------------------------  
  6. #include <Classes.hpp>  
  7. #include <Controls.hpp>  
  8. #include <StdCtrls.hpp>  
  9. #include <Forms.hpp>  
  10. #include "CPort.hpp"  
  11. //---------------------------------------------------------------------------  
  12. class TForm1 : public TForm  
  13. {  
  14. __published:    // IDE-managed Components  
  15.         TComPort *compt;  
  16.         TButton *btn_set;  
  17.         TButton *btn_open_close;  
  18.         TButton *btn_send;  
  19.         TMemo *memo_1;  
  20.         TEdit *edt_send;  
  21.         void __fastcall btn_setClick(TObject *Sender);  
  22.         void __fastcall btn_open_closeClick(TObject *Sender);  
  23.         void __fastcall btn_sendClick(TObject *Sender);  
  24.  //       void __fastcall ComPortRxChar(TObject* Sender, int Count);  
  25.         void __fastcall comptRxChar(TObject *Sender, int Count);  
  26.   
  27. private:    // User declarations  
  28.         int time;  
  29. public:     // User declarations  
  30.         __fastcall TForm1(TComponent* Owner);  
  31. };  
  32. //---------------------------------------------------------------------------  
  33. extern PACKAGE TForm1 *Form1;  
  34. //---------------------------------------------------------------------------  
  35. #endif  

2.com_port.cpp

[cpp] view plain copy
 
 print?
  1. //---------------------------------------------------------------------------  
  2.   
  3. #include <vcl.h>  
  4. #pragma hdrstop  
  5.   
  6. #include "Unit1.h"  
  7. //---------------------------------------------------------------------------  
  8. #pragma package(smart_init)  
  9. #pragma link "CPort"  
  10. #pragma resource "*.dfm"  
  11. TForm1 *Form1;  
  12. //---------------------------------------------------------------------------  
  13. __fastcall TForm1::TForm1(TComponent* Owner)  
  14.         : TForm(Owner)  
  15. {  
  16.   memo_1->Text="";  
  17. }  
  18. //---------------------------------------------------------------------------  
  19. void __fastcall TForm1::btn_setClick(TObject *Sender)//设置串口  
  20. {  
  21.   compt->ShowSetupDialog();  
  22. }  
  23. //---------------------------------------------------------------------------  
  24. void __fastcall TForm1::btn_open_closeClick(TObject *Sender) //打开&关闭串口  
  25. {  
  26.   if(compt->Connected)  
  27.     {  
  28.      compt->Close();  
  29.      btn_open_close->Caption="打开串口";  
  30.     }  
  31.   else  
  32.     {  
  33.      compt->Open();  
  34.      btn_open_close->Caption="关闭串口";  
  35.     }  
  36. }  
  37. //---------------------------------------------------------------------------  
  38. void __fastcall TForm1::btn_sendClick(TObject *Sender) //发送数据  
  39. {  
  40.   AnsiString Str;  
  41.   
  42.   Str=edt_send->Text;  
  43.   Str=Str+"/r/n";  
  44.   compt->WriteStr(Str);  
  45.   //memo_1->Text="yes,you are right!/r/n";  
  46. }  
  47. //---------------------------------------------------------------------------  
  48. void __fastcall TForm1::comptRxChar(TObject *Sender, int Count) //接收数据  
  49. {  
  50.   AnsiString Str;  
  51.   compt->ReadStr(Str,Count);  
  52.   memo_1->Text=memo_1->Text+Str;  
  53. }  
  54. //---------------------------------------------------------------------------  
原文地址:https://www.cnblogs.com/h2zZhou/p/5305353.html