I have written a program that reads data through a serial port.
I always get the following comment when I run my program:"Error, Uknown error".
I work on an IMU sensor, I tested it with Termite (software for reading data from serial port) to see if I receive the data, and indeed the sensor is functional and I receive the data perfectly through Termite.
On the other hand under linux, if I make the command cat /dev/ttyACM0, I receive the data only during 10 s, from where the decision to write a code
Imu::Imu() :
moving(false)
{
serialPort = new QSerialPort("COM3",this);
if (serialPort->open( QIODevice::ReadOnly))
{
qDebug()<<"SerialPort"<<serialPort<<"Opened successufly";
}
else
{
qDebug()<<serialPort->error();
QSerialPortInfo::availablePorts();
}
if (!serialPort->setBaudRate(57600))
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);
qDebug()<<"error"<<serialPort->errorString();
pollingTimer = new QTimer(this);
//QObject::connect(this->serialPort,SIGNAL(readyRead()),this,SLOT(pollSerialPort()));
QObject::connect(pollingTimer, SIGNAL(timeout()), this, SLOT(pollSerialPort()));
QObject::connect(serialPort, &::QSerialPort::errorOccurred,this,&Imu::handleError);
pollingTimer->start(10);
}
Imu::~Imu()
{
serialPort->close();
}
void Imu::handleError(QSerialPort::SerialPortError error)
{
if (error == QSerialPort::ResourceError){
qDebug()<<"Handle Error"<<error;
}
}
Ps : I sent the code to a friend to see if it is correct, he tested it and he received the data
不要轮询计时器,而是将 QSerialPort::readyRead 连接到一个插槽,并在插槽内使用 QSerialPort::readAll() 直到 QSerialPort::bytesAvailable() 为 0。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.