用C++Builder MSComm编写的串口程序接收数据时出现问题,请大侠指点,不胜感激!
我编写的接收程序如下:
bool __fastcall TForm1::ComRec(unsigned char *str,unsigned long* len)
{
if(MSComm1->PortOpen==true)
{
AnsiString ss;
OleVariant RxBuff;
int bytenum;
RxBuff=MSComm1->Input;
bytenum=RxBuff.ArrayHighBound(1)+1;
*len=bytenum;
int i=0;
for(i=0;i<bytenum;i++)
{
str[i]=RxBuff.GetElement(i);
}
ss = AnsiString(str);
Edit1->Text = ss ;
return true;
}
else
{
Application->MessageBoxA("端口没打开","信息",MB_OK);
*len=0;
return false;
}
}
运行时出现下列问题:
Project Project1.exeraised exception class EVariantInvalidArgError with message "Invalid argument" Process stopped. Use Step or Run to continue.
问题出在这条语句上:
bytenum=RxBuff.ArrayHighBound(1)+1;
请各位高手指点,十分感谢!
[解决办法]
bytenum=RxBuff.ArrayHighBound(1)+1;
用
bytenum= Comm1->InBufferCount ;
[解决办法]