Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] serial port read problem
Forum Updated to NodeBB v4.3 + New Features

[solved] serial port read problem

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 4.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    laser2k
    wrote on last edited by
    #1

    Hello everybody,

    i have a little prblem with serial port respectively with the answer i got from my device.

    i write to the device serial.write("test\r\n");
    and the device answer "test ok".

    this works fine.

    i will analyze the answer in qt to find out which command should send next.
    and here is the problem my testvariable "s" is filled with both the write request and the answer:

    @test
    test ok
    @

    Is it possible to get only the answer?

    Here is my code:

    @
    using namespace std;
    QSerialPort serial;

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    serial.setPortName("COM2");
    serial.setBaudRate(QSerialPort::Baud9600);
    serial.setParity(QSerialPort::NoParity);
    serial.setDataBits(QSerialPort::Data8);
    serial.setStopBits(QSerialPort::OneStop);
    serial.setFlowControl(QSerialPort::NoFlowControl);
    
    
    
     if (!serial.open(QIODevice::ReadWrite))
    {
        qDebug() << "Fehler -- Verbindung konnte nicht hergestellt werden" << endl;
        return 0;
    } else {
         qDebug() << "OK -- Verbindung hergestellt"  << endl;
    
    
        serial.write("test\r\n");
    
            QByteArray DataReceive = serial.readLine();
            while(serial.waitForReadyRead(30))
            {
                     DataReceive += serial.readLine();
            }
            QByteArray response = DataReceive;
            QString s(response);
            qDebug() << "Antwort: " << s << "ende";
    
    }
    
    return a.exec(&#41;;
    

    }
    @

    Thanks for helping :-)
    Regards Alex

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kuzulis
      Qt Champions 2020
      wrote on last edited by
      #2

      @
      serial.setPortName("COM2");
      if (!serial.open(QIODevice::ReadWrite))
      {
      qDebug() << "Fehler -- Verbindung konnte nicht hergestellt werden" << endl;
      return 0;
      } else {
      bool success = serial.setBaudRate(QSerialPort::Baud9600)
      & serial.setParity(QSerialPort::NoParity)
      & serial.setDataBits(QSerialPort::Data8)
      & serial.setStopBits(QSerialPort::OneStop)
      & serial.setFlowControl(QSerialPort::NoFlowControl);
      qDebug() << success ? "OK" : "FAIL"<< endl;

          if (!success)
              return 0;
      
          serial.write("test\r\n");
          serial.waitForBytesWritten(1000); // or serial.flush()
      
          ...
          ...
      }
      

      @

      Please see examples and read documentaion.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        laser2k
        wrote on last edited by
        #3

        Hello kuzulis,

        thanks for you answer :-)

        this is a difrent way to get a serial connection. That is not the problem. My script is working.

        But my output is wrong
        when i write test i got in my anser bot my written test and the return value test ok

        and i have no idea to filter the serial.write command from the return value

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DerManu
          wrote on last edited by
          #4

          I think your device is simply echoing the command that it's acknowledging. I know from experience, that some devices do that.
          To test this hypothesis, connect to your serial device via some terminal application (e.g. "putty":http://www.putty.org/). If you see the "test ok" response too, then it indeed is just the device echoing the command.

          From there you have two options:
          Remember the command and literally remove it from the response, if it matches. Something like this:
          @if (s.startsWith(theCommand))
          s.remove(0, theCommand.size());@

          The other option might be to tell the device to stop echoing. Most devices that echo have a command for that.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            laser2k
            wrote on last edited by
            #5

            Hi Manu,

            no the device doesn`t echoing the command. I have programmed it by myself and test all with putty before i started programming in qt

            if i type down my commands via putty by hand all works.

            I think the problem is serial.write(); it echos the command to the commandline and serial.readAll(); reads it.

            Output Putty:
            test ok (but i see here what i wrote in by hand)

            your solution to remember the command is a good idea :-)
            but i thought someone knows why qt reads the write line

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kuzulis
              Qt Champions 2020
              wrote on last edited by
              #6

              Yes, also problem with an echo can be occurs from an "bad" of RS 422/485 converter. I mean that exists such converters where an input/output direction control is processed with an "manual" set/reset level on the DTR pin.

              But it is very cheap and very old converters. Usual, for these devices (good devices) the direction handles automatically by HW himself.

              Of course, this problem can be if you use an such converter.. :)

              UPD: QtSerialPort does not read back an write data! Somewhere you something do wrong... :)

              1 Reply Last reply
              0
              • L Offline
                L Offline
                laser2k
                wrote on last edited by
                #7

                I found the solution:

                in qt console i got quotes ("") throug in and output like that:

                INPUT:
                "test"

                and then the output is

                "test
                test ok"

                i had to play around with \r and \r\n on both sides.
                This brought me the Solution

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved