无法在c ++ windows窗体应用程序中使用readline()函数

时间:2014-06-16 11:07:45

标签: c++ visual-studio-2010 visual-c++ serial-port

我正在尝试制作一个Windows窗体应用程序,用于通过串口读取消息。为此,我使用函数'Ports()'来检测串行端口,并显示检测到的串口我使用了一个comboBox。 Ports()函数的主体在这里: -

void Ports(void)
{
 array<String^>^ serialPorts = nullptr;

 // Get a list of serial port names.
 serialPorts = SerialPort::GetPortNames();
//list detcted ports on combobox
 this->comboBox_Ports->Items->AddRange( serialPorts );

}

同样我使用另一个函数和一个ComboBox来设置Baudrate,这是波特率函数的代码: -

void BaudRate(void)
{
this->comboBox_BaudRate->Items->AddRange(gcnew cli::array< System::Object^  >(3) {L"9600",L"38400", L"115200"});
}

现在我使用Button设置COM端口,Baudrate并初始化串口,这是初始化按钮的代码: -

private: System::Void button_init_Click(System::Object^  sender, System::EventArgs^  e) {


                this->_serialPort->PortName=this->comboBox_Ports->Text;
                     this->textBox1->Text=this->comboBox_Ports->Text;
                this->_serialPort->BaudRate=Int32::Parse(this->comboBox_BaudRate->Text);
                     this->textBox2->Text=this->comboBox_BaudRate->Text;
            this->_serialPort->ReadTimeout = 1500;
            this->_serialPort->WriteTimeout = 1500;
                this->_serialPort->Open();

             }

现在最后,我使用一个按钮从串口读取值,这里是读取按钮的代码: -

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
             String^ name;

                      name=this->_serialPort->ReadLine();

              this->textBox_Read->Text=name;


         }

现在当我使用这个应用程序时,我的COM端口和BaudRate设置正常,但我在ReadLine()函数中收到此错误: -

A first chance exception of type 'System.TimeoutException' occurred in System.dll

所以,请告诉我为什么我没有得到我的价值观。

0 个答案:

没有答案