Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QT QSerialPort and QTcpSocket working together



  • I'm writing a medium program where it is supposed to get some packages from a program in one computer (A) via TCP protocol and send it to another machine(B) using the serial port.

    This is generally what I did to have them all together:

    @class TcpSerial : public QObject
    {
    Q_OBJECT
    private:
        QSerialPort *serialGate;
        guiCommand *Command;
        tcpReceiver *TCP;  \\A seperate class I wrote which can do basic TCP actions
    public:
        antennaDataReceiver();
    public slots:
        void dataReceivedFromSerial();
        void send_to_serial();
    }
    

    @
    They need to have some sort of communication as to give Command to each other. Now I have a problem managing the Thread issue here. I tried different approaches but I got different errors each time:

    1.Defining a new Thread using QThread and moving one of them into a seperate thread

    @ socket notifiers cannot be disabled/enabled from another thread@
    2.Moving the tcpReceiver to my class thread.
    3.Leaving them alone to run on their own threads.

    @ QObject: Cannot create children for a parent that is in a different thread.
    //and the program hangs out, I checked in this situation both tcpReceiver and serialSender are running in the same thread@

    What is you suggestion for handling this situation? Thank you in advance.



  • Hi, as QSerialPort and QTcpSocket both provide
    asynchronous API, there is no need to create your own thread in normal cases.


Log in to reply