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. SerialPort in a different Thread
Forum Updated to NodeBB v4.3 + New Features

SerialPort in a different Thread

Scheduled Pinned Locked Moved Solved General and Desktop
qthreadserial portqtimer
2 Posts 1 Posters 1.4k 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.
  • J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by
    #1

    H everyone,

    So I've written a class that handles the serialport and data-transfer via ModBus.

    The idea was to move the class to an other thread and run the data exchange idependet from the UI.

    Everything seemed to work, but a Qt::BlockingQueuedConnection resulted in a deadlock -> Obviously I did something wrong. So I reworked it to this:

    class HauptDesign : public QMainWindow
    {
        Q_OBJECT
        QThread modbusThread;
    ....
    ....
    }
    
    HauptDesign::HauptDesign(QWidget *parent) :
        QMainWindow(parent)
        ,ui(new Ui::HauptDesign)
    {    
        ui->setupUi(this);
    
        qRegisterMetaType<QList<int> >();
        qRegisterMetaType<QModbusDevice::State>();
        modbusDevice    = new Modbus;
        modbusDevice->moveToThread(&modbusThread);
    
    connect(modbusDevice, &Modbus::signalConnectionState, this, &HauptDesign::VerbindungConnection);
    connect(modbusDevice, &Modbus::signalNewDataRegs, this, &HauptDesign::receiveModbusRegister);
    ...
    connect(this, &HauptDesign::connectDevice, modbusDevice, &Modbus::connectDevice);
    
    modbusThread.start();
    

    It works, I can successfully exchange data, but my Debug screen is flooded with

    QObject::killTimer: timers cannot be stopped from another thread
    
    QObject::startTimer: timers cannot be started from another thread
    

    messages.
    I don't use Timers in that class, I have not even defined one in the' modbusDevice' class. QModbusClient obviously has a connection timeout what might cause this issue!?

    I'm at a loss how to do this the correct way.
    Any help is appreciated


    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.

    1 Reply Last reply
    0
    • J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      Never mind,

      I had code in my the constructor of the to be threaded class, that caused the conflict...

      connect(&modbusThread, &QThread::started, modbusDevice, &Modbus::setupClass);
      

      fixed the issue.


      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.

      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