I am writing a program to collect data from an IMU sensor through a SerialPort
The data of the IMU received via the software of the latter are in Hexadecimal
So I would like to extract them in Hexadecimal too in a first time
You will find attached my program, the problem is that I receive 0 at each data reception.
static const int FRAME_SIZE = 68;
Imu::Imu() :
moving(false)
{
serialPort = new QSerialPort("COM3",this);
if (!serialPort->open( QIODevice::ReadOnly))
{
log_warning("imu","failed to open device file \"%s\", IMU measures will be unavailable",qPrintable(serialPort->portName()));
return;
}
if (!serialPort->setBaudRate(115200))
log_error("imu","failed to set baudrate, error no %d",serialPort->error());
serialPort->setDataBits(QSerialPort::Data8);
serialPort->setParity(QSerialPort::NoParity);
serialPort->setStopBits(QSerialPort::OneStop); // One Stop bit
serialPort->setFlowControl(QSerialPort::NoFlowControl);
pollingTimer = new QTimer(this);
QObject::connect(pollingTimer, SIGNAL(timeout()), this, SLOT(pollSerialPort()));
pollingTimer->start(10);
}
Imu::~Imu()
{
serialPort->close();
}
void Imu::pollSerialPort()
{
static const unsigned char START_BYTES[2] = {0x55,0xAA};
static const QByteArray START_WORD((char*)START_BYTES,2);
static QTime startTime = QTime::currentTime();
static QByteArray data;
data.append(serialPort->readAll());
qDebug() << data.count() << data.toHex();
CodePudding user response:
Define two slots in your Imu
class, one to read data and another to handle error condition, then connected them to errorOccurred
and readyRead
signals of QSerialPort
like this.
serialPort = new QSerialPort("COM3",this);
connect(serialPort, &QSerialPort::errorOccurred, this, &Imu::handleError);
connect(serialPort, &QSerialPort::readyRead, this, &Imu::readData);
When data arrives on serial port, Qt will automatically call readData
slots. In readData
you can get all the data like this
void Imu::readData() {
const QByteArray data = serialPort->readAll();
qDebug() << data.count() << data.toHex();
}
In handleError
function, you can check different error conditions:
void Imu::handleError(QSerialPort::SerialPortError error)
{
if (error == QSerialPort::ResourceError) {
// handle error
}
}
Side Note: Don't use old style signal/slot connection, use new style. Below link provides nice explanation.
https://wiki.qt.io/New_Signal_Slot_Syntax
CodePudding user response:
Thank you for your answer,
I have done the readyread, however this time, I had no data reception. Not even the 0 that I had at the beginning.
Thanks again for your reply.
QObject::connect(serialPort, &QSerialPort::errorOccurred, this, &Imu::handleError);
QObject::connect(serialPort, &QSerialPort::readyRead, this, &Imu::pollSerialPort);
}
Imu::~Imu()
{
serialPort->close();
}
void Imu::handleError(QSerialPort::SerialPortError error)
{
if (error == QSerialPort::ResourceError) {
// qDebug()<<"handleerror" <<;
}
}
void Imu::pollSerialPort()
{
static const unsigned char START_BYTES[2] = {0x55,0xAA};
static const QByteArray START_WORD((char*)START_BYTES,2);
static QTime startTime = QTime::currentTime();
if (!pollingTimer->isActive()){
pollingTimer->start(10);
}
static QByteArray data;
data.append(serialPort->readAll());
qDebug() << data.count() << data.toHex();