首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

关于c++bulider的串口有关问题,请一下!

2012-04-11 
关于c++bulider的串口问题,请高手指点一下!!我的程序如下:__fastcall TForm1::TForm1(TComponent* Owner):

关于c++bulider的串口问题,请高手指点一下!!
我的程序如下: 
__fastcall TForm1::TForm1(TComponent* Owner) 
  : TForm(Owner) 

if(MSComm1->PortOpen==true) 

Button1->Enabled=false; 
Button2->Enabled=true; 
Button3->Enabled=true; 
Button4->Enabled=true; 
Shape1->Brush->Color=clGreen; 

else 

Button2->Enabled=true; 
Button2->Enabled=false; 
Button3->Enabled=false; 
Button4->Enabled=false; 
Shape1->Brush->Color=clRed; 

void __fastcall TForm1::Button1Click(TObject *Sender) 

if(MSComm1->PortOpen!=true) 

MSComm1->CommPort=StrToInt(ComboBox1->Text); 
MSComm1->Settings= 
ComboBox2->Text+","+ 
ComboBox3->Text+","+ 
ComboBox4->Text+","+ 
ComboBox5->Text; 
MSComm1->InputMode=0; 
MSComm1->PortOpen=true; 
Button1->Enabled=false; 
Button2->Enabled=true; 
Button3->Enabled=true; 
Button4->Enabled=true; 
Shape1->Brush->Color=clGreen; 

在调试中却出现以下错误:Project new002x.exe raised exception on class EConvertEorror with message ''com1' is not a valid integer value'.Process stopped.Use Step orRun to continue. 
请问应如何解决??

[解决办法]

C/C++ code
MSComm1->CommPort=StrToInt(ComboBox1->Text); //应该是这一句有问题,而且和串口没什么关系。。。MSComm1->CommProt=1;//这才表示要打开串口1,即“com1”//'com1' is not a valid integer value'//很明显,这是由于 StrToInt(ComboBox1->Text) 当中的 StrToInt()这个函数,抛出了异常。。。莫非你的 ComboBox1->Text="Com1"??//这样当然会错啦。。。你可以改成 StrToInt(ComboBox1->ItemIndex)。。。。。。。。。 

热点排行