QSerialPort - transmition errors
-
Hello guys,
i have some problems in reading bytes using QSerialPort. I implemented readyRead() - readAll() signal-slot system but it seems to be working incorectly. I have to read about 3Mb of data (continuous transmition) but the bytes read by readAll() function are all zeros. I tested the device using some terminal application and it seems to work fine. Does anyone experienced similar problem in their work?simplified code here:
char datawrite[] = {0x01,0x02}; // device commands which i should get response from QByteArray dataread; serial->write(datawrite); ... // handle readyRead() void on_readyRead() { dataread.append(serial->readAll()); }
[edit: fixed coding tags ``` SGaist]
-
Hi,
Please post the un-simplified code. From what you posted you might be using two different QByteArrays with the same name so one would stay empty and not the other one.
-
HEADER:
#ifndef SESIONPROGRAMING_H #define SESIONPROGRAMING_H #include <QMainWindow> #include <QSerialPort> namespace Ui { class SesionPrograming; } class SesionPrograming : public QMainWindow { Q_OBJECT public: explicit SesionPrograming(QWidget *parent = 0); ~SesionPrograming(); private slots: void handleError(QSerialPort::SerialPortError error); void openSerialPort(); void closeSerialPort(); void writeData(const QByteArray &data); void readData(); private: QByteArray reciveddata; int readcalls; char charbuff[1]; Ui::SesionPrograming *ui; QSerialPort *serial; }; #endif // SESIONPROGRAMING_H
SOURCE:
#include <QMessageBox> #include <QDebug> #include <windows.h> SesionPrograming::SesionPrograming(QWidget *parent) : QMainWindow(parent), ui(new Ui::SesionPrograming) { ui->setupUi(this); serial = new QSerialPort(this); serial->setBaudRate(QSerialPort::Baud115200); serial->setDataBits(QSerialPort::Data8); serial->setParity(QSerialPort::NoParity); serial->setStopBits(QSerialPort::OneStop); serial->setFlowControl(QSerialPort::NoFlowControl); serial->setPortName("COM5"); connect(serial, SIGNAL(readyRead()), this, SLOT(readData())); connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(handleError(QSerialPort::SerialPortError))); openSerialPort(); const char commands[] = {0xb0}; QByteArray writeba(commands); writeData(writeba); } SesionPrograming::~SesionPrograming() { qDebug() << reciveddata.toHex(); qDebug() << reciveddata; qDebug() << reciveddata.size(); qDebug() << readcalls; closeSerialPort(); delete ui; } void SesionPrograming::handleError(QSerialPort::SerialPortError error) { if (error == QSerialPort::ResourceError) { QMessageBox::critical(this, tr("Critical Error"), serial->errorString()); closeSerialPort(); } } void SesionPrograming::writeData(const QByteArray &data) { serial->write(data); } void SesionPrograming::readData() { reciveddata.append(serial->readAll()); } void SesionPrograming::openSerialPort() { if (serial->open(QIODevice::ReadWrite)) { qDebug()<<"połączono z portem com4"; } else { QMessageBox::critical(this, tr("Error"), serial->errorString()); } } void SesionPrograming::closeSerialPort() { if (serial->isOpen()) serial->close(); }
-
Why are you calling
serial->clear()
?Please enclose your code with coding tags: three back-ticks before and after.
-
Another strange behaviour here:
The device has a list of commands i.e
0x02 - return name (8bytes)
0x04 - return firmware (12bytes)
When i write above commands it responds correctly but when i use this 0x80... command which should get me the measurment data (thousands of bytes) it sends only zeros (again, i tested the device in external terminal app and it worked fine...) so in my opinion the length of data is the problem. But why? -
Why you are think that it is correct?
const char commands[] = {0xb0}; QByteArray writeba(commands);
QByteArray is not the telepathist, so try this instead:
const char commands[] = {0xb0}; QByteArray writeba(commands, sizeof(commands));
and check that QByteArray contains desired bytes.