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. QTSerialPort read/write

QTSerialPort read/write

Scheduled Pinned Locked Moved General and Desktop
qtserialport
6 Posts 4 Posters 4.4k Views
  • 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.
  • D Offline
    D Offline
    desperatenewbie
    wrote on last edited by
    #1

    Hi,
    I'm using QT 4.8 with qtserialport and i am having some trouble communicating with the device. Here is my simple main function
    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
    qDebug() << "Name : " << info.portName();
    qDebug() << "Description : " << info.description();
    qDebug() << "Manufacturer: " << info.manufacturer();

            // Example use QSerialPort
            QSerialPort serial;
            serial.setPort(info);
            serial.setBaudRate(QSerialPort::Baud2400);
            serial.setDataBits(QSerialPort::Data8);
            serial.setParity(QSerialPort::NoParity);
            serial.setFlowControl(QSerialPort::SoftwareControl);
            if (serial.open(QIODevice::ReadWrite)){
                qDebug()<<"Opened successfully";
              }
    
    
            serial.write("PSN");
            if (!serial.waitForReadyRead(1000))
            {
                qDebug()<<"failed";
    
            }
            qDebug()<<serial.readAll();
    
            serial.close();
        }
    
    return a.exec();
    

    }

    PSN should return the product serial number and the device.
    i keep getting "failed" as an output and readAll() gives me "" in the console. Can anybody give me a piece of code I can use to make it work without redirecting me to other posts or examples that came with qtserialport, i wasted hours there.
    Reading/Writing to the device in MATLAB and putty works as expected. Its just in QT that i get no response.
    Thanks

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi and welcome to devnet

      this code

      qDebug() << QByteArray("\0 1 2 3 4 5 6 7");
      

      produces "".
      You must check if the received buffer contains or begins with 0x00

      check with

      qDebug() << serial.bytesAvailable();
      qDebug() << serial.readAll().size();
      

      if the buffer is not empty

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      D 1 Reply Last reply
      0
      • M mcosta

        Hi and welcome to devnet

        this code

        qDebug() << QByteArray("\0 1 2 3 4 5 6 7");
        

        produces "".
        You must check if the received buffer contains or begins with 0x00

        check with

        qDebug() << serial.bytesAvailable();
        qDebug() << serial.readAll().size();
        

        if the buffer is not empty

        D Offline
        D Offline
        desperatenewbie
        wrote on last edited by
        #3

        @mcosta Thanks for the help.
        qDebug() << serial.bytesAvailable();
        qDebug() << serial.readAll().size();
        both output 0 which means the buffer is empty.
        From past experience, the buffer remains empty for about 60-90 ms after a command is sent, i believe waiting for 1000ms using the waitForReadyRead(1000) would have given enough time for the buffer to fill.
        Is the problem thus with the .write() function?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on last edited by
          #4

          Sorry,

          I don't have so much experience with QtSerialPort. but if waitForReadyRead returns true means something is arrived on the port

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SysTech
            wrote on last edited by SysTech
            #5

            You didn't mention what device you are trying to talk to...

            I'm wondering if PSN is the command to get the product serial number but AFTER that command you need to send a CR or LF? So something like:

            serial.write("PSN\r");

            or

            serial.write("PSN");
            serial.putChar( 0x13 );

            The reason I bring this up is in putty you may have a CR going out without knowing it.

            The other thing you might try is connecting up the serial port readyRead() signal to a slot and see if it ever gets called. This is an indication data is coming in.

            1 Reply Last reply
            0
            • R Offline
              R Offline
              Rondog
              wrote on last edited by
              #6

              I have never tried using QSerialPort outside of the message loop (i.e. 'a.exec()' from main() in your program). I do get the impression that it relies on the message loop to function properly so this test may not work.

              If you modify your test program a bit I suspect it will work:

              • Create a class derived from QWidget
              • Add a 'Test' button with associated slot
              • Put all your test code in the test slot.

              In Qt4 you had to use the external QSerialDevice class (this, or some variation of it, was migrated into Qt5 but didn't exist internally as part of Qt4). There was something about the open command mode options that I ran into, I don't remember the details unfortunately. There is a namespace called 'AbstractSerial' with various options, at least in the version I have. You might want to use this instead:

                      if (serial.open(AbstractSerial::ReadWrite)){
                          qDebug()<<"Opened successfully";
                        }
              
              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