繁体   English   中英

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

[英]Trying to read write data from com port using c++

我正在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() ,它将等待直到收到换行符。 您确定要正确发送NewLine吗? 另外,您应该检查_serialPort->NewLine的实际值,也许将其设置为CRLF,而您只收到LF。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM