QSerialPort dont emitted readyRead if window is resizing or moving



  • Hello, help me, please.

    I have class for work with some device by COM port. But it dont emitted readyRead if i'm resizing or moving program's window. How solve this problem?

    @class Device : public QObject
    {
    Q_OBJECT

    public:
    explicit Device(QObject *parent = 0);
    ~Device();
    bool openPort(const QString &portName);
    void closePort();
    bool isPortOpen() const;
    bool sendCommand(unsigned char cmd, unsigned char arg = 0);
    bool connectDevice();

    private slots:
    void onReadData();
    void error(QSerialPort::SerialPortError error);

    private:
    QSerialPort *port;
    QByteArray buffer;

    signals:
    void newDataAvailbled(CurrentData* data);
    };@

    @Device::Device(QObject *parent) : QObject(parent)
    {
    ...
    this->port = new QSerialPort(this);
    connect(this->port, SIGNAL(readyRead()), this, SLOT(onReadData()));
    }

    Device::~Device()
    {
    this->closePort();
    delete this->port;
    }

    bool Device::openPort(const QString &portName)
    {
    this->closePort();
    this->port->setPortName(portName);
    const bool isOpen = this->port->open(QIODevice::ReadWrite);
    if (isOpen == true) {
    this->port->setBaudRate(QSerialPort::Baud115200);
    this->port->setDataBits(QSerialPort::Data8);
    this->port->setFlowControl(QSerialPort::NoFlowControl);
    this->port->setParity(QSerialPort::NoParity);
    this->port->setStopBits(QSerialPort::OneStop);
    }
    return isOpen;
    }

    void Device::closePort()
    {
    if (this->port->isOpen())
    this->port->close();
    }

    bool Device::isPortOpen() const
    {
    return this->port->isOpen();
    }

    bool Device::sendCommand(unsigned char cmd, unsigned char arg)
    {
    if (this->isPortOpen() == false)
    return false;
    ...
    QByteArray buf;
    if (this->port->write(buf) != buf.size())
    return false;
    return true;
    }

    bool Device::connectDevice()
    {
    return this->sendCommand(DeviceCommands::TurnOffSMode);
    }

    void Device::onReadData()
    {
    this->buffer.append(this->port->readAll());
    ...
    emit this->newDataAvailbled(newData);
    }@


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    I would say that the event loop is "blocked" processing the resizing hence the readyRead are not processed while your moving your window. You could move Device in another thread.

    @
    QThread *serialThread = new QThread(this);
    Device *device = new Device;
    device->moveToThread(serialThread);
    serialThread->start();
    // do connections etc...
    @

    Something like that

    Hope it helps



  • Yes, SGaist is right.

    At present the single decision is use of a separate thread. But in the future (for Qt >= 5.x only) is planned to use I/O completion port feature for resolve this problem (but it just ideas on the future).



  • I used moveToThread(), but problem was left((
    I'm use windows 7 and Qt5.1

    also, if connected readyRead to slot in Device by Qt:AutoConnection, then I have Heap Corruption on reading data. if connect by DirectConnection or BlockingQueuedConnection, then it's OK, but problem with moving of window was left



  • So...
    I moved method from signal from readyRead() to timerEvent...
    @void Device::timerEvent(QTimerEvent *)
    {
    int c = port->bytesAvailable();
    ...
    emit testSignal(QTime::currentTime().toString() + " " + QString::number(c));
    ...
    }@

    and if I'm moving window, then c is zero, but signal was emitted and I getting current time and zero



  • sorry, I forgot waitForReadyBytes


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.