[英]Reading a serial PORT for data extraction
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.我在 IMU 传感器上工作,我用 Termite(用于从串行端口读取数据的软件)对其进行了测试,以查看我是否接收到数据,并且确实传感器正常工作,并且我通过 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另一方面,在 linux 下,如果我执行命令 cat /dev/ttyACM0,我只会在 10 秒内收到数据,从那里决定编写代码
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 ps:我把代码发给朋友看看是否正确,他测试了一下,收到了数据
不要轮询计时器,而是将 QSerialPort::readyRead 连接到一个插槽,并在插槽内使用 QSerialPort::readAll() 直到 QSerialPort::bytesAvailable() 为 0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.