TMSComm控件如何接收数据阿
为什么我用TMSComm控件不能接受数据呢?程序如下:
AnsiString str = " ";
if(MSComm1-> PortOpen == false)
{MSComm1-> PortOpen = true ;}
MSComm1-> InputLen = 1;
while(MSComm1-> InBufferCount > = 0)
{
str =str + MSComm1-> Input ;
}
ShowMessage(str);
}
接收到的是空的,还请高人指点,谢谢!!
[解决办法]
//---------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h "
//---------------------------------------
#pragma package(smart_init)
#pragma link "MSCommLib_OCX "
#pragma resource "*.dfm "
TForm1 *Form1;
//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//打开串口
if(!MSComm1-> PortOpen)
{
MSComm1-> PortOpen=true;
}
}
//---------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
//关闭串口
if(MSComm1-> PortOpen)
{
MSComm1-> PortOpen=false;
}
}
//---------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
try
{
//发送数据
MSComm1-> Output=Edit1-> Text;
Sleep(20);
//接收数据
Edit2-> Text=MSComm1-> Input;
}
catch(Exception &e)
{
ShowMessage(e.Message);
}
}
//---------------------------------------