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. check SerialPort successfully written to
Forum Updated to NodeBB v4.3 + New Features

check SerialPort successfully written to

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 490 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.
  • A Offline
    A Offline
    Aeroplane123
    wrote on last edited by Aeroplane123
    #1

    I have a program where I am writing to a serial port, if the write was a success I want bool to return true or false.

    When this line runs...

    bool success = serialPort.write(someData);
    qDebug() << success;
    

    I get the debug output "true". The serial port is not even connected!, please can somebody explain why it returns true when the serial port is not open....

    QIODevice::read (QSerialPort): device not open
    
    jsulmJ 1 Reply Last reply
    0
    • A Aeroplane123

      I have a program where I am writing to a serial port, if the write was a success I want bool to return true or false.

      When this line runs...

      bool success = serialPort.write(someData);
      qDebug() << success;
      

      I get the debug output "true". The serial port is not even connected!, please can somebody explain why it returns true when the serial port is not open....

      QIODevice::read (QSerialPort): device not open
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @Aeroplane123 said in check SerialPort successfully written to:

      QIODevice::read (QSerialPort): device not open

      Here you are trying to read from serial port: is it the same QSerialPort instance as the one you're using for writing?
      https://doc.qt.io/qt-6/qiodevice.html#write returns number of bytes written, not a boolean. In case it fails it returns -1 (which converts to TRUE if you assign it to a boolean!), so you should check the return value properly.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Aeroplane123
        wrote on last edited by Aeroplane123
        #3

        Ah yes sorry, yes the serial port is definitely not open when I try to write. Yes it the same instance.

        so I want to do

        serialPort.write(someData);
        
        

        But then please how can I check if it did actually send via the port?

        jsulmJ 1 Reply Last reply
        0
        • A Aeroplane123

          Ah yes sorry, yes the serial port is definitely not open when I try to write. Yes it the same instance.

          so I want to do

          serialPort.write(someData);
          
          

          But then please how can I check if it did actually send via the port?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Aeroplane123 said in check SerialPort successfully written to:

          But then please how can I check if it did actually send via the port?

          Basic C/C++ stuff:

          bool success = serialPort.write(someData) != -1;
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Aeroplane123
            wrote on last edited by Aeroplane123
            #5
            serialPort.write(someData);
            
            bool success = serialPort.write(someData) != -1;
            
            qDebug() << success;
            

            output is "True", but thre is no port open.

            JonBJ 1 Reply Last reply
            0
            • A Aeroplane123
              serialPort.write(someData);
              
              bool success = serialPort.write(someData) != -1;
              
              qDebug() << success;
              

              output is "True", but thre is no port open.

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by
              #6

              @Aeroplane123
              Please just try qDebug() << serialPort.write(someData);.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Aeroplane123
                wrote on last edited by
                #7

                OK, oddly now it is giving me false and -1, which is what I expect.

                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