BCB下的mscomm控件的oncomm事件不能响应,急等!
void __fastcall TForm1::bt_OpenSerialClick(TObject *Sender)
{
if(MSComm1->PortOpen!=true)
{
MSComm1->CommPort=StrToInt(cb_ComNum->Text);//选择串口号
MSComm1->Settings=
cb_BaudRate->Text+","+
cb_Check->Text+","+
cb_DataBit->Text+","+
cb_StopBit->Text; //设置串口的属性波特率、奇偶校验、数据位和、停止位。
MSComm1->InputMode=0;//设置传入数据的格式,0表示文本形式
MSComm1->RTSEnable=true;
//MSComm1->RThreshold=1;
MSComm1->InputLen=0; //每次从缓冲区中读出来的数据个数。
MSComm1->PortOpen=true;//打开串口
//MSComm1->SThreshold = 0; //不触发发送事件
MSComm1->RThreshold = 4; //每一个字符到接收缓冲区都触发接收事件
MSComm1->InBufferCount = 0; //清除发送缓冲区数据
MSComm1->OutBufferCount = 0; //清除接收缓冲区数据
bt_OpenSerial->Enabled=false;
bt_CloseSerial->Enabled=true;
bt_SendData->Enabled=true;
bt_SaveData->Enabled=true;
//Shape1->Brush->Color=clGreen;
}
}
void __fastcall TForm1::MSComm1Comm(TObject *Sender)
{
AnsiString str; //声明一个AnsiString类型的变量
OleVariant s; //声明一个用于接收数据的OleVariant变量。
if(MSComm1->CommEvent==2) // 接收缓冲区中是否收到Rthreshold个字符。
{
//MSComm1->RThreshold=0;
if(MSComm1->InBufferCount)// 是否有字符驻留在接收缓冲区等待被取出
{
s=MSComm1->Input;//接收数据
str=s.AsType(varString); //把接收到的OleVariant变量转换成AnsiString类型
me_Receive->Text=me_Receive->Text+str;//把接收到的数据显示在Memo1中。
}
//MSComm1->RThreshold=1;
}
}
当单片机发送数据到PC时,程序不能响应oncomm事件,其中也设置过MSComm1->RThreshold=1,都不响应那位大侠碰到过类似问题,请尽快回复,非常感谢!
[解决办法]
void __fastcall TForm1::MSComm1Comm(TObject *Sender)
{ // 串口采用异步通迅,不同的端口可同时按收----------------------------------------
TMSComm *mscomm1=(TMSComm *)Sender;
int port=mscomm1->CommPort;
TChannel * c=Channels[port];
OleVariant buf0=mscomm1->Input;
if (!c->isIdle)
{
BYTE *buf1=c->buf;
int cn=buf0.ArrayHighBound()+1;
for (int i=0;i<cn;i++)
buf1[c->bufLen++]=buf0.GetElement(i);
if (c->chCount!=0 && c->bufLen>=c->chCount)
{ // 置点的状态
c->bufLen=c->chCount;
Points[c->pointIdx]->dataStat=3; // 完成,可以处理了
Points[c->pointIdx]->dataLen=c->bufLen;
Points[c->pointIdx]->dataTick1=GetTickCount();
c->isIdle=true; // 释放通道
}
}
}
我用异步通讯,有N个端口共享同一个串行事件,
[解决办法]
mscomm控件的属性可能不对,我看一看我的:
mscomm1->RThreshold=1
mscomm1->SThreshold=0
mscomm1->Settings="9600,n,8,1"
mscomm1->InputLen=0
mscomm1->InputMode=0
mscomm1->InBufferSize=1024
mscomm1->OutBufferSize=512