MSComm串口通信问题!!!
为什么串口初始化会出现Invalid port Number错误,发送数据也会出现 Invalid argument错误...求达人解答!!!到底哪里出错了..请详细指出来,谢谢!!!
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(MSComm1-> PortOpen==false)
{
MSComm1-> PortOpen=true;
}
AnsiString str;
str=ComboBox1-> Text+ ", "+ "0 "+ComboBox2-> Text+ ", "+ComboBox3-> Text;
MSComm1-> CommPort=2;
MSComm1-> Settings=str;
MSComm1-> InputMode=comInputModeText;
MSComm1-> SThreshold=1;
MSComm1-> RThreshold=1;
MSComm1-> InBufferCount=0;
}
//---------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
if(MSComm1-> PortOpen==false)
{
MSComm1-> PortOpen=true;
}
Application-> MessageBox( "串口打开成功 ", "信息提示框 ",MB_OK);
}
//---------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
if(MSComm1-> PortOpen==true)
{
MSComm1-> PortOpen=false;
}
}
//---------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
if(OpenDialog1-> Execute())
RichEdit1-> Lines-> LoadFromFile(OpenDialog1-> FileName);
}
//---------------------------------------
void __fastcall TForm1::Button7Click(TObject *Sender)
{
if(SaveDialog1-> Execute())
RichEdit2-> Lines-> SaveToFile(SaveDialog1-> FileName+ ".txt ");
}
//---------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
RichEdit1-> Lines-> Text= " ";
}
//---------------------------------------
void __fastcall TForm1::Button8Click(TObject *Sender)
{
RichEdit2-> Lines-> Text= " ";
}
//---------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
OleVariant varSend;
char cBuff[200];
varSend=RichEdit1-> Lines-> Text;
for(int i=0;i <=10;i++)
varSend.PutElement(cBuff[i],i);
MSComm1-> Output=varSend;
}
//---------------------------------------
void __fastcall TForm1::MSComm1Comm(TObject *Sender)
{
OleVariant varReseive;
String strReseive;
varReseive=MSComm1-> Input;
strReseive=varReseive.AsType(varString);
RichEdit2-> Lines-> Text=strReseive;
}
[解决办法]
MSComm1-> CommPort=2;com2不存在,也许坏了也有可能。
另代码中不少问题,自己改吧,比如没判断,顺序颠倒等。还有想当然的错误。很要不得
[解决办法]
Invalid port Number:打开串口不存在或者已被占用。