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

Trying to read write data from com port using c++

本文关键字:读取 数据 com c++      更新时间:2023-10-16

我正在LM4F232H5QD板上工作,想读取com端口数据。每当我使用Tera术语或任何其他串行终端时,我都会从com端口获取数据。

但是当我使用此代码时,我收到超时异常错误。

// _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 引脚时,效果很好。

谁能告诉我如何解决这个问题?

如果您确定终端的数据看起来不错,则短路引脚将无济于事。您使用的唯一读取函数是 ReadLine() ,它将等到收到换行符。您确定您正确发送了换行符吗?另外,您应该检查_serialPort->NewLine的实际值,也许它已设置为 CRLF,并且您只收到 LF。