Windows 8.1 で Bluetooth デバイスと通信するためにqextserialportを使用しています。ComPortクラスを定義しました。最初に「COM5」にメッセージを書き込んで{0xA9,0x55}
、Bluetooth デバイスにデータの送信を開始するように依頼する必要があります。その後、データの読み取りを開始できます。メッセージを書き込んだことを示す端末アプリケーションがあり、データは「COM5」で利用可能です。
問題定義:
comport.cppでは、どちらwaitForBytesWritten()
もwaitForReadyRead()
returnもありません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;
}