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 ?
QtWS25 Last Chance

Cannot write a data to a virtual serial port ?

Scheduled Pinned Locked Moved Solved General and Desktop
qserialportcppserialport
16 Posts 4 Posters 1.6k 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 Offline
    E Offline
    elypheldia
    wrote on 13 Aug 2021, 09:34 last edited by
    #1

    I am new in QT developing. I opened a virtual serial port, but I cannot write a data to this. How can I write ?

    main.cpp

    #include <QCoreApplication>
    #include "myserialport.h"
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        MySerialPort s;
        s.openSerialPort();
    
    
        return a.exec();
    }
    
    

    myserialport.h

    #ifndef MYSERIALPORT_H
    #define MYSERIALPORT_H
    
    #include <QObject>
    #include <QSerialPort>
    
    class MySerialPort : public QObject
    {
        Q_OBJECT
    public:
        explicit MySerialPort(QObject *parent = nullptr);
        void openSerialPort();
        void closeSerialPort();
        void writeData(const QByteArray &);
    
    
    signals:
    
    public slots:
        void readData();
    private:
        QSerialPort *serial;
    };
    
    #endif // MYSERIALPORT_H
    
    

    myserialport.cpp

    #include "myserialport.h"
    #include <QSerialPort>
    #include <QDebug>
    
    MySerialPort::MySerialPort(QObject *parent) : QObject(parent)
    {
        serial=new QSerialPort(this);
        connect(serial,SIGNAL(readyRead()),this,SLOT(readData()));
    
    }
    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";
    
    
        }
        else
        {
            qDebug()<<QSerialPort::PermissionError;
        }
    
    }
    void MySerialPort::closeSerialPort()
    {
        if(serial->isOpen())
        {
            serial->close();
        }
    }
    void MySerialPort::writeData(const QByteArray &data)
    {
        serial->write(data);
    }
    void MySerialPort::readData()
    {
        QByteArray data="hello";
        writeData(data);
        serial->readAll();
        qDebug()<<data;
    }
    
    
    J 1 Reply Last reply 13 Aug 2021, 09:38
    0
    • E elypheldia
      13 Aug 2021, 11:47

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

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 13 Aug 2021, 11:50 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 13 Aug 2021, 12:38
      2
      • E elypheldia
        13 Aug 2021, 09:34

        I am new in QT developing. I opened a virtual serial port, but I cannot write a data to this. How can I write ?

        main.cpp

        #include <QCoreApplication>
        #include "myserialport.h"
        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv);
        
            MySerialPort s;
            s.openSerialPort();
        
        
            return a.exec();
        }
        
        

        myserialport.h

        #ifndef MYSERIALPORT_H
        #define MYSERIALPORT_H
        
        #include <QObject>
        #include <QSerialPort>
        
        class MySerialPort : public QObject
        {
            Q_OBJECT
        public:
            explicit MySerialPort(QObject *parent = nullptr);
            void openSerialPort();
            void closeSerialPort();
            void writeData(const QByteArray &);
        
        
        signals:
        
        public slots:
            void readData();
        private:
            QSerialPort *serial;
        };
        
        #endif // MYSERIALPORT_H
        
        

        myserialport.cpp

        #include "myserialport.h"
        #include <QSerialPort>
        #include <QDebug>
        
        MySerialPort::MySerialPort(QObject *parent) : QObject(parent)
        {
            serial=new QSerialPort(this);
            connect(serial,SIGNAL(readyRead()),this,SLOT(readData()));
        
        }
        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";
        
        
            }
            else
            {
                qDebug()<<QSerialPort::PermissionError;
            }
        
        }
        void MySerialPort::closeSerialPort()
        {
            if(serial->isOpen())
            {
                serial->close();
            }
        }
        void MySerialPort::writeData(const QByteArray &data)
        {
            serial->write(data);
        }
        void MySerialPort::readData()
        {
            QByteArray data="hello";
            writeData(data);
            serial->readAll();
            qDebug()<<data;
        }
        
        
        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 13 Aug 2021, 09:38 last edited by
        #2

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

        void MySerialPort::readData()
        {
        QByteArray data="hello";
        writeData(data);
        serial->readAll();
        qDebug()<<data;
        }

        Why do you write data in a port connected to readyRead signal?
        You should at least first read all the data and then write.

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

        E 1 Reply Last reply 13 Aug 2021, 09:47
        0
        • J jsulm
          13 Aug 2021, 09:38

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

          void MySerialPort::readData()
          {
          QByteArray data="hello";
          writeData(data);
          serial->readAll();
          qDebug()<<data;
          }

          Why do you write data in a port connected to readyRead signal?
          You should at least first read all the data and then write.

          E Offline
          E Offline
          elypheldia
          wrote on 13 Aug 2021, 09:47 last edited by
          #3

          @jsulm Hey, thx for reply. As I said that I am very new, and I know, I have terrible mistakes. Could you give me some example about write and read data to understand?

          J 1 Reply Last reply 13 Aug 2021, 09:51
          0
          • E elypheldia
            13 Aug 2021, 09:47

            @jsulm Hey, thx for reply. As I said that I am very new, and I know, I have terrible mistakes. Could you give me some example about write and read data to understand?

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 13 Aug 2021, 09:51 last edited by
            #4

            @elypheldia Well, simply change order:

            void MySerialPort::readData()
            {
                serial->readAll();
                QByteArray data="hello";
                writeData(data);
                qDebug()<<data;
            }
            

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

            E 1 Reply Last reply 13 Aug 2021, 09:58
            0
            • J jsulm
              13 Aug 2021, 09:51

              @elypheldia Well, simply change order:

              void MySerialPort::readData()
              {
                  serial->readAll();
                  QByteArray data="hello";
                  writeData(data);
                  qDebug()<<data;
              }
              
              E Offline
              E Offline
              elypheldia
              wrote on 13 Aug 2021, 09:58 last edited by
              #5

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

              J 1 Reply Last reply 13 Aug 2021, 10:02
              0
              • E elypheldia
                13 Aug 2021, 09:58

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

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 13 Aug 2021, 10:02 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 13 Aug 2021, 10:29
                3
                • J jsulm
                  13 Aug 2021, 10:02

                  @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 13 Aug 2021, 10:29 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.

                  J 1 Reply Last reply 13 Aug 2021, 10:32
                  0
                  • E elypheldia
                    13 Aug 2021, 10:29

                    @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.

                    J Offline
                    J Offline
                    JonB
                    wrote on 13 Aug 2021, 10:32 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 13 Aug 2021, 10:42
                    0
                    • J JonB
                      13 Aug 2021, 10:32

                      @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 13 Aug 2021, 10:42 last edited by
                      #9

                      @JonB number of written bytes

                      J J 2 Replies Last reply 13 Aug 2021, 10:55
                      0
                      • E elypheldia
                        13 Aug 2021, 10:42

                        @JonB number of written bytes

                        J Offline
                        J Offline
                        JonB
                        wrote on 13 Aug 2021, 10:55 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
                          13 Aug 2021, 10:42

                          @JonB number of written bytes

                          J Offline
                          J Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 13 Aug 2021, 11:07 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 13 Aug 2021, 11:24
                          0
                          • J jsulm
                            13 Aug 2021, 11:07

                            @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 13 Aug 2021, 11:24 last edited by
                            #12

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

                            J 1 Reply Last reply 13 Aug 2021, 11:39
                            0
                            • E elypheldia
                              13 Aug 2021, 11:24

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

                              J Offline
                              J Offline
                              J.Hilk
                              Moderators
                              wrote on 13 Aug 2021, 11:39 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 13 Aug 2021, 11:47
                              1
                              • J J.Hilk
                                13 Aug 2021, 11:39

                                @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 13 Aug 2021, 11:47 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 1 Reply Last reply 13 Aug 2021, 11:50
                                0
                                • E elypheldia
                                  13 Aug 2021, 11:47

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

                                  J Offline
                                  J Offline
                                  J.Hilk
                                  Moderators
                                  wrote on 13 Aug 2021, 11:50 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 13 Aug 2021, 12:38
                                  2
                                  • J J.Hilk
                                    13 Aug 2021, 11:50

                                    @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 13 Aug 2021, 12:38 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

                                    5/16

                                    13 Aug 2021, 09:58

                                    topic:navigator.unread, 11
                                    • Login

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