Important: Please read the Qt Code of Conduct -

Qt Serial not usable from another class

  • Hello to all.
    I'm trying to read and write with serial port. I have created a class , named serial , and connect it with readyRead on mainwindow.cpp.

    I'm reading the data when I received it by connecting signal in serial.cpp without any problem.

    I'm writing the data in serial.cpp without any problem too.

    But when I tried to write data from another class(manuel.cpp) "The program has unexpectedly finished".

    With my researches I found that the when I tried to access the serial device in manuel.cpp , I cant.
    I looked from manuel.cpp , isOpen,isReadable,isWriteable and they are both false.
    But when I looked from serial.cpp they are true.

    I think the problem is the serial device can reachable from another class. Is there any solvers?

    Here is my github link

  • Lifetime Qt Champion

    @bladekel From design point of view what you are doing is bad:

    • QSerialPort *arduino; - should be private, provide public read() and write() methods

    • const QString arduino_seri = "75533353637351110171"; -should be private + a public getter

    • QString arduino_port_name; - should be private + a public getter

    • bool arduino_is_available; - should be private + a public getter

    Your app is probably crashing because you did not call serial::ard_kontrol() before writing. In serial::ard_kontrol() you initialize arduino pointer, so if you do not call it you're dereferencing a dangling pointer. In general in such cases you should first debug to see where exactly it is crashing and why.

  • Lifetime Qt Champion


    You do all your initialisation in ard_kontrol. You never call that function thus nothing is initialised.

  • Moderators


    Your code is a tad difficult to understand. But from a quick browse of your code I think I found the culprit:

    Your have 2 objects of your class serial active. One created and maintained from mainwindow and one from manual.

    But you can open your serial port only once -> Error

    Remove the serial instance from your manual class and use signal slots in your mainwindow to connect manual and serial together and let them communicate that way

Log in to reply