简体   繁体   中英

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

I am working on LM4F232H5QD board and want to read com port data. Whenever i use Tera term or any other serial terminal i got data from com port.

But when i use this code i got Timeout Exception error.

// _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";

    }

But when i short Rx and Tx pins it works good.

Can anyone please tell me how can i solve this problem?

If you are sure the data looks good from a terminal, shorting the pins won't help. The only read function you use is ReadLine() , which will wait until it receive a NewLine. Are you sure you are sending a NewLine correctly ? Also, you should check the actual value of _serialPort->NewLine , maybe it is set to CRLF and you are only receiving a LF.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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