C++builder串口通信设计(一)-串口接收数据

一、安装mscomm32.ocx控件

1、复制mscomm32.ocx到windows\system32\下

2、注册

二、在c++builder下引入mscomm32.ocx

选择菜单component->Import ActiveX Control,

C++builder串口通信设计(一)-串口接收数据

C++builder串口通信设计(一)-串口接收数据
选择“Microsoft Comm Control 6.0(Version1.1)”,点击“install”

C++builder串口通信设计(一)-串口接收数据

然后可以在ActiveX控件组看到一个像电话的控件。

三、建立应用工程

1、设计界面

C++builder串口通信设计(一)-串口接收数据

引入了mscomm32控件,memo1控件,Button1,Button2,RadioButton1和RadioButton2

1) 其中memo1用于显示串口接收内容

2)Button1用于控制串口的开启和关闭,Button2用于终止应用程序

3)RadioButton1和RadioButton2用于选择串口接收方式(类型)

2、unit1.h内容,其中红色为引入的全局变量

#include
#include
#include "MSCommLib_OCX.h"
#include
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TMSComm *MSComm1;
TMemo *Memo1;
TButton *Button1;
TButton *Button2;
TGroupBox *GroupBox1;
TRadioButton *RadioButton1;
TRadioButton *RadioButton2;
void __fastcall Button1Click(TObject *Sender);
void __fastcall MSComm1Comm(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
void __fastcall RadioButton1Click(TObject *Sender);
void __fastcall RadioButton2Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
int type;//0--字符串,1---二进制
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

3、unit1.cpp代码


void __fastcall TForm1::Button1Click(TObject *Sender) // 打开
{
static st=0;
AnsiString s;
if(st==0)
{
try
{
Form1->MSComm1->_CommPort=3;//COM3
Form1->MSComm1->Settings="9600,n,8,1"; //初始化串口
Form1->MSComm1->InputMode=type; 设置传入数据的格式,0表示文本形式 ,1表示二进制格式
Form1->MSComm1->RThreshold=1;
Form1->MSComm1->PortOpen=true; //打开串口
Application->MessageBoxA("串口初始化成功","串口初始化",0);
}
catch(...)
//catch(EOleException&e)
{
Application->MessageBoxA("串口未连接好或已经打开","故障提示");
}

Button1->Caption="关闭";
st=1;
}
else
{
Form1->MSComm1->PortOpen=false;
Button1->Caption="打开";
st=0;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::MSComm1Comm(TObject *Sender) //串口接收事件
{
AnsiString str; //声明一个AnsiString类型的变量
OleVariant rec; //声明一个用于接收数据的OleVariant变量。
int count;
int j;
unsigned char buf[128];

switch(MSComm1->CommEvent)
{
case comEvReceive: //接收事件
if(type==0) //字符型接收
{
str=MSComm1->Input;//收到字符串;
Memo1->Text=Memo1->Text+str+" "; //显示收到的字符串
}
else //type=1 为二进制接收
{
count=MSComm1->InBufferCount; //字节数
rec=MSComm1->Input; //取出接收缓冲器内容
for(j=0;
j小于count; j++)
{
  buf[j]=rec.GetElement(j); //转换成字节类型
}
Memo1->Text=Memo1->Text+"\n";
for(j=0;
j小于count; j++)
{
  Memo1->Text=Memo1->Text+IntToHex(buf[j],2)+" "; //显示接收的字节(以十六进制显示)
}
}
break;
default: break;
} //switch(MSComm1->CommEvent)
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Application->Terminate();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender) //初始化
{
RadioButton1->Checked=false;
RadioButton2->Checked=true;
type=1; //0--字符,1---二进制
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RadioButton1Click(TObject *Sender) //字符型
{
RadioButton2->Checked=false;
RadioButton1->Checked=true;
type=0; //字符型
Form1->MSComm1->InputMode=type;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RadioButton2Click(TObject *Sender) //二进制
{
RadioButton1->Checked=false;
RadioButton2->Checked=true;
type=1;//二进制
Form1->MSComm1->InputMode=type;
}
//---------------------------------------------------------------------------

四、运行结果

C++builder串口通信设计(一)-串口接收数据

C++builder串口通信设计(一)-串口接收数据

可见,选择二进制模式或字符模式都可以完成串口接收通信。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值