未发出qextserialport中的readyRead()信号

时间:2015-11-20 14:47:53

标签: c++ serial-port signals-slots qextserialport qiodevice

我正在使用qextserialport与Windows 8.1上的蓝牙设备进行通信。我定义了一个 ComPort 类。我需要首先在" COM5"上写一条消息{0xA9,0x55}。要求蓝牙设备开始传输数据。然后我就可以开始阅读数据了。我有一个终端应用程序,显示我已经写了消息,数据可以在" COM5"。

问题定义:

comport.cpp 中,waitForBytesWritten()waitForReadyRead()都不会返回true。可用字节数为零,不调用onReadyRead()槽。我的代码有什么问题?

comport.h

#ifndef COMPORT_H
#define COMPORT_H

#include <QObject>
#include <QDebug>
#include "qextserialport.h"
#include "qextserialenumerator.h"

class QTimer;

class ComPort : public QObject
{
    Q_OBJECT

public:
    ComPort(const QString &portName);
    ~ComPort();

private:
    QextSerialPort* port;

    void     setupPort();


private slots:
    void onReadyRead();
};

#endif // COMPORT_H

comport.cpp

#include "comport.h"
ComPort::ComPort(const QString &portName)
{
    QString strPort;
    this->port = new QextSerialPort(portName);
    this->setupPort();

    connect(port, SIGNAL(readyRead()), this, SLOT(onReadyRead()));

    if (port->open(QIODevice::ReadWrite) == true)
    {
        const char mydata[] = {static_cast<char>(0xA9),static_cast<char>(0x55)};
        QByteArray data = QByteArray::fromRawData(mydata, sizeof(mydata));
        port->write(data);
        if(port->waitForBytesWritten(100))
            qDebug() << "Wrote the message";

        QByteArray responseData = port->readAll();
        while (port->waitForReadyRead(2000))
               responseData += port->readAll();

        qDebug() << "Num of bytes: " << port->bytesAvailable();
    }
    else
    {
        qDebug() << "Device is not turned on";
    }
}

ComPort::~ComPort()
{
    delete port;
}

void ComPort::setupPort()
{
    port->setBaudRate(BAUD115200);
    port->setTimeout(100); // Does nothing in Eventdriven mode!
    port->setFlowControl(FLOW_OFF);
    port->setParity(PAR_NONE);
    port->setDataBits(DATA_8);
    port->setStopBits(STOP_1);
    port->setQueryMode(QextSerialPort::EventDriven);
}

void ComPort::onReadyRead()
{
    QByteArray bytes;
    int a = port->bytesAvailable();
    bytes.resize(a);
    port->read(bytes.data(), bytes.size());
    qDebug() << "bytes read:" << bytes.size();
    qDebug() << "bytes:" << bytes;
}

0 个答案:

没有答案