由于我认为新学习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
- //---------------------------------------------------------------------------
- #ifndef Unit1H
- #define Unit1H
- //---------------------------------------------------------------------------
- #include <Classes.hpp>
- #include <Controls.hpp>
- #include <StdCtrls.hpp>
- #include <Forms.hpp>
- #include "CPort.hpp"
- //---------------------------------------------------------------------------
- class TForm1 : public TForm
- {
- __published: // IDE-managed Components
- TComPort *compt;
- TButton *btn_set;
- TButton *btn_open_close;
- TButton *btn_send;
- TMemo *memo_1;
- TEdit *edt_send;
- void __fastcall btn_setClick(TObject *Sender);
- void __fastcall btn_open_closeClick(TObject *Sender);
- void __fastcall btn_sendClick(TObject *Sender);
- // void __fastcall ComPortRxChar(TObject* Sender, int Count);
- void __fastcall comptRxChar(TObject *Sender, int Count);
- private: // User declarations
- int time;
- public: // User declarations
- __fastcall TForm1(TComponent* Owner);
- };
- //---------------------------------------------------------------------------
- extern PACKAGE TForm1 *Form1;
- //---------------------------------------------------------------------------
- #endif
2.com_port.cpp
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "Unit1.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma link "CPort"
- #pragma resource "*.dfm"
- TForm1 *Form1;
- //---------------------------------------------------------------------------
- __fastcall TForm1::TForm1(TComponent* Owner)
- : TForm(Owner)
- {
- memo_1->Text="";
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::btn_setClick(TObject *Sender)//设置串口
- {
- compt->ShowSetupDialog();
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::btn_open_closeClick(TObject *Sender) //打开&关闭串口
- {
- if(compt->Connected)
- {
- compt->Close();
- btn_open_close->Caption="打开串口";
- }
- else
- {
- compt->Open();
- btn_open_close->Caption="关闭串口";
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::btn_sendClick(TObject *Sender) //发送数据
- {
- AnsiString Str;
- Str=edt_send->Text;
- Str=Str+"/r/n";
- compt->WriteStr(Str);
- //memo_1->Text="yes,you are right!/r/n";
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::comptRxChar(TObject *Sender, int Count) //接收数据
- {
- AnsiString Str;
- compt->ReadStr(Str,Count);
- memo_1->Text=memo_1->Text+Str;
- }
- //---------------------------------------------------------------------------