簡體   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