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. Cannot write a data to a virtual serial port ?
Forum Updated to NodeBB v4.3 + New Features

Cannot write a data to a virtual serial port ?

Scheduled Pinned Locked Moved Solved General and Desktop
qserialportcppserialport
16 Posts 4 Posters 2.2k 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.
  • E elypheldia

    @jsulm I tried your code, but I still can't see the data in the terminal. What is the reason of this ?

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

    @elypheldia What does serial->write(data); return?
    Also, connect a slot to https://doc.qt.io/qt-5/qserialport.html#errorOccurred and see whether there es any error.

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

    E 1 Reply Last reply
    3
    • jsulmJ jsulm

      @elypheldia What does serial->write(data); return?
      Also, connect a slot to https://doc.qt.io/qt-5/qserialport.html#errorOccurred and see whether there es any error.

      E Offline
      E Offline
      elypheldia
      wrote on last edited by
      #7

      @jsulm I added this slot to handle error.

      connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this,
          SLOT(handleError(QSerialPort::SerialPortError)));
      
      void MySerialPort::handleError(QSerialPort::SerialPortError error)
      {
          if (error == QSerialPort::NoError) {
              qDebug()<<"no error";
          }
      }
      

      And, I got "no error " output.
      I added to writeData function

      qDebug()<<"written";
      

      But, I did not get output. Port is opened, but couldn't write.

      JonBJ 1 Reply Last reply
      0
      • E elypheldia

        @jsulm I added this slot to handle error.

        connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this,
            SLOT(handleError(QSerialPort::SerialPortError)));
        
        void MySerialPort::handleError(QSerialPort::SerialPortError error)
        {
            if (error == QSerialPort::NoError) {
                qDebug()<<"no error";
            }
        }
        

        And, I got "no error " output.
        I added to writeData function

        qDebug()<<"written";
        

        But, I did not get output. Port is opened, but couldn't write.

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

        @elypheldia
        @jsulm asked you to

        What does serial->write(data); return?

        Can you just confirm/tell us what that write() returns?

        E 1 Reply Last reply
        0
        • JonBJ JonB

          @elypheldia
          @jsulm asked you to

          What does serial->write(data); return?

          Can you just confirm/tell us what that write() returns?

          E Offline
          E Offline
          elypheldia
          wrote on last edited by
          #9

          @JonB number of written bytes

          JonBJ jsulmJ 2 Replies Last reply
          0
          • E elypheldia

            @JonB number of written bytes

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

            @elypheldia
            Then we would suggest to you (I believe!) that the bytes are being sent. So what is your "proof" that they are not? For example, for all I know if you expect a "response" your write might not be correct, it might require a \n or something at the end.

            Further, you call serial->readAll(); immediately after executing the write(). I don't know about serial ports, but in general this is not right in Qt, the data sent back may arrive a while later. You are supposed to use readyRead() signal for asynchronous operation. [OK, I see you do that. Your protocol first waits for bytes to arrive initially, then writes something back; is your device then supposed to send something back to you? If so, it will then ping-pong endlessly?] Unless you/ @jsulm know better?

            I know Wireshark can be used on PC to view TCP/IP/socket traffic. Is there a tool you can use to verify whether your bytes are being sent to the Linux serial device?

            1 Reply Last reply
            1
            • E elypheldia

              @JonB number of written bytes

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

              @elypheldia said in Cannot write a data to a virtual serial port ?:

              number of written bytes

              Could you please tell us the number?

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

              E 1 Reply Last reply
              0
              • jsulmJ jsulm

                @elypheldia said in Cannot write a data to a virtual serial port ?:

                number of written bytes

                Could you please tell us the number?

                E Offline
                E Offline
                elypheldia
                wrote on last edited by
                #12

                @jsulm I cannot. Because, I dont know how to learn number of written bytes.

                J.HilkJ 1 Reply Last reply
                0
                • E elypheldia

                  @jsulm I cannot. Because, I dont know how to learn number of written bytes.

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #13

                  @elypheldia said in Cannot write a data to a virtual serial port ?:

                  @jsulm I cannot. Because, I dont know how to learn number of written bytes.

                  are you joking ?

                  you already use qDebug() in your code example, use it some more
                  qDebug() << "Bytes Written:" << serial->write(data);

                  that said, make sure you actually write something before you expect readData to be called.


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  E 1 Reply Last reply
                  1
                  • J.HilkJ J.Hilk

                    @elypheldia said in Cannot write a data to a virtual serial port ?:

                    @jsulm I cannot. Because, I dont know how to learn number of written bytes.

                    are you joking ?

                    you already use qDebug() in your code example, use it some more
                    qDebug() << "Bytes Written:" << serial->write(data);

                    that said, make sure you actually write something before you expect readData to be called.

                    E Offline
                    E Offline
                    elypheldia
                    wrote on last edited by
                    #14

                    @J-Hilk I already did this. But I get no output. I am very new, sorry. Thx to all for replies.

                    J.HilkJ 1 Reply Last reply
                    0
                    • E elypheldia

                      @J-Hilk I already did this. But I get no output. I am very new, sorry. Thx to all for replies.

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by J.Hilk
                      #15

                      @elypheldia said in Cannot write a data to a virtual serial port ?:

                      @J-Hilk I already did this. But I get no output. I am very new, sorry. Thx to all for replies.

                      thats why I said:

                      make sure you actually write something before you expect readData to be called

                      change your openSerialPort to this:

                      void MySerialPort::openSerialPort()
                      {
                          serial->setPortName("/dev/tnt0");
                          serial->setBaudRate(QSerialPort::Baud9600);
                          serial->setDataBits(QSerialPort::Data8);
                          serial->setFlowControl(QSerialPort::NoFlowControl);
                          serial->setStopBits(QSerialPort::OneStop);
                          serial->setParity(QSerialPort::NoParity);
                          if(serial->open(QIODevice::ReadWrite))
                          {
                              qDebug()<<"opened";
                             QByteArray data="hello";
                                 writeData(data);
                      
                          }
                          else
                          {
                              qDebug()<<QSerialPort::PermissionError;
                          }
                      
                      }
                      

                      it will, at the very, least attempt to write something, and should return a bytes written value and/or an error message


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      E 1 Reply Last reply
                      2
                      • J.HilkJ J.Hilk

                        @elypheldia said in Cannot write a data to a virtual serial port ?:

                        @J-Hilk I already did this. But I get no output. I am very new, sorry. Thx to all for replies.

                        thats why I said:

                        make sure you actually write something before you expect readData to be called

                        change your openSerialPort to this:

                        void MySerialPort::openSerialPort()
                        {
                            serial->setPortName("/dev/tnt0");
                            serial->setBaudRate(QSerialPort::Baud9600);
                            serial->setDataBits(QSerialPort::Data8);
                            serial->setFlowControl(QSerialPort::NoFlowControl);
                            serial->setStopBits(QSerialPort::OneStop);
                            serial->setParity(QSerialPort::NoParity);
                            if(serial->open(QIODevice::ReadWrite))
                            {
                                qDebug()<<"opened";
                               QByteArray data="hello";
                                   writeData(data);
                        
                            }
                            else
                            {
                                qDebug()<<QSerialPort::PermissionError;
                            }
                        
                        }
                        

                        it will, at the very, least attempt to write something, and should return a bytes written value and/or an error message

                        E Offline
                        E Offline
                        elypheldia
                        wrote on last edited by
                        #16

                        @J-Hilk Thank you all guys for your patient and sorry for my basic mistakes :). I fixed my issue. As you said, I expect the read without writing a data to serial port.

                        1 Reply Last reply
                        1

                        • Login

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