尝试使用c ++从com端口读取写入数据

时间:2014-12-11 09:49:24

标签: c++ visual-studio-2010

我正在研究LM4F232H5QD板并想要读取COM端口数据。每当我使用Tera术语或任何其他串行终端时,我从com端口获取数据。

但是当我使用这个code时,我得到了Timeout Exception错误。

// _serialPort
            // 
            this->_serialPort->PortName = L"COM3";
            this->_serialPort->ReadTimeout = 5000;
        this->_serialPort->WriteTimeout = 5000;

.
.
.
.
// Read button --------------------------------------
    //this will start the asyn for backgroundwork
    private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {

          // check if port is ready for reading
          if(this->_serialPort->IsOpen){
              // Reset the text in the result label.
              this->textBox2->Text = String::Empty;

              // this will read manually
              try{
              this->textBox2->Text=this->_serialPort->ReadLine();

    //        printf("trying to read data");
              }


              catch(TimeoutException^){
               this->textBox2->Text="Timeout Exception";

              }
              // Disable the init button
              // the asynchronous operation is done.
              this->button2->Enabled = false;

              this->ovalShape1->FillColor= Color::Green;
          }
          else
              // give error warning
             this->textBox2->Text="Port Not Opened";

    }

但是当我缩短Rx和Tx引脚时,效果很好。

任何人都可以告诉我如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

如果您确定终端的数据看起来不错,那么缩短引脚就不会有帮助。您使用的唯一读取函数是ReadLine(),它将等到收到NewLine。您确定要正确发送NewLine吗?此外,您应该检查_serialPort->NewLine的实际值,也许它设置为CRLF并且您只接收LF。