我对编程很陌生,我正在自学。我编写了一个应用程序来轮询来自控制单元的许多请求。我基本上连续发送各种读取命令到控制单元,并读取回响应.My程序工作,我成功地发送命令和接收答案。但是读取速度非常慢(我的代码中有100 ms超时以确保得到完整的答复),我有一个由C++的专业编码器编写的控制单元的程序,在他的程序中,他每隔30 ms轮询一次,而我总是在那个时间框架内得到完整的答案。我有相同的设置57K波特8位,1停止位和没有奇偶校验。然而,我的QT代码需要将近100 ms才能得到答案。在我的代码中,我读取了前两个字节(第一个字节是消息标识符,第二个字节是消息长度的剩余部分),然后在循环中读取,直到总消息长度等于消息长度字节+1 ( +1包含第一个字节)。当我知道QT中的硬件是限制因素时,我才明白为什么我的代码在QT中如此缓慢。请求总是3字节,应答从3字节到61字节不等。请帮助我指出我的错误。如果我删除超时,我总是有短读。到目前为止,我也尝试过阅读(全部),但结果是一样的。下面是我读取响应的代码的摘录。完整的代码在https://github.com/MarkusIppy/PowerTune
//Error handling
QTime startTime = QTime::currentTime();
int timeOut = 100; // timeout in milisec.
QByteArray recvData = m_serialport->read(2); // reading first two bytes of received message to determine lenght of ecpected message
int msgLen = recvData[1]; //Total message Lenght excluding the first byte
while ( recvData.size() <= (msgLen+1) )
{
if ( startTime.msecsTo(QTime::currentTime()) > timeOut ) break;
recvData += m_serialport->read(msgLen+1-recvData.size());
}
if (msgLen +1 == recvData.length()) //if the received data lenght equals the message lenght from lenght byte + identifier byte (correct message lenght received )
{
qDebug() << "Received data OK"<<msgLen +1;
if(requestIndex <= 61){requestIndex++;}
else{requestIndex = 58;}
readData(recvData);
}
else //if the lenght of the received message does not correspond with the expected lenght repeat the request
{
qDebug() << "Received data lenght NIO";
readData(recvData);
qDebug() << "Request Message again"<< requestIndex;
}发布于 2017-06-16 15:12:08
对不起,我没有足够的时间来检查您的项目,从您提供的代码中,我无法100%确定原因。不过,我最好的猜测是,在这种情况下,问题在于您显式地等待数据被接收,事件处理在某种程度上被延迟或根本没有发生。
总之,这里你有几个建议:
我通过将QSerialPort的bytesWritten(qint64 bytes)和readyRead()信号连接到程序的插槽中来使用on_bytesWritten(qint64 bytes)和on_readyRead()。然后,我向目标设备发送请求,并在on_readyRead()插槽中处理结果。使用每个发送命令,我启动一个QTimer,它的timeout()信号连接到应用程序的on_timeout()插槽。通过这种方式,我可以监控设备是否及时做出响应,并在数据出现时立即获得数据。您也可以使用errorOccurred(QSerialPort::SerialPortError error)信号的QSerialPort来检查是否有问题的传输。
发布于 2017-06-24 17:02:08
再次稍微修改我的代码,现在它在实际的硬件上很好地工作了,下面是我的读槽:
void Serial::readyToRead()
{
qDebug() << "ready read";
if(ecu == 0)
{
m_readData.append(m_serialport->readAll());
Bytesexpected = m_readData[1]+1;
qDebug() << "readdata current" <<m_readData.toHex();
if (Bytesexpected == m_readData.size())
{
m_timer.stop();
if(requestIndex <= 62){requestIndex++;}
else{requestIndex = 59;}
readData(m_readData);
Serial::clear();
m_readData.clear();
Serial::sendRequest(requestIndex);
}
if (Bytesexpected != m_readData.size())
{
qDebug() << "starting timer";
m_timer.start(5000);
}
}发布于 2017-06-23 07:54:35
到目前为止,这就是我所拥有的(仅仅发布了cpp文件的重要部分),这段代码现在与我的消息模拟器几乎完美地工作在一起。它现在像预期的那样轮询,但超时总是在5秒后触发(如果没有消息开始,我需要将其更改为触发器)。我只能在下周的实际硬件上测试它。到目前为止,这就是我所拥有的:
void Serial::initSerialPort()
{
if (m_serialport)
delete m_serialport;
m_serialport = new SerialPort(this);
connect(this->m_serialport,SIGNAL(readyRead()),this,SLOT(readyToRead()));
connect(m_serialport, static_cast<void (QSerialPort::*) (QSerialPort::SerialPortError)>(&QSerialPort::error),
this, &Serial::handleError);
connect(&m_timer, &QTimer::timeout, this, &Serial::handleTimeout);
m_timer.start(5000);
}
void Serial::readyToRead()
{
if(ecu == 0)
{
m_readData.append(m_serialport->readAll());
Bytesexpected = m_readData[1]+1;
if (Bytesexpected == m_readData.size())
{
if(requestIndex <= 62){requestIndex++;}
else{requestIndex = 59;}
readData(m_readData); // message for processing
Serial::clear();
m_readData.clear();
}
//Timeout
if (!m_timer.isActive())
m_timer.start(5000);
}
}
void Serial::handleTimeout()
{
qDebug() << "Timeout";
//request Data Again
QString fileName = "Errors.txt";
QFile mFile(fileName);
if(!mFile.open(QFile::Append | QFile::Text)){
qDebug() << "Could not open file for writing";
}
QTextStream out(&mFile);
out << "Timeout Request Index " << int(requestIndex)<< " lenght received "<< int(m_readData.length())<< " Bytes "<< " Expected Bytes "<< int(Bytesexpected)<< " bytes " <<" Message "<< QByteArray(m_readData.toHex()) <<endl;
mFile.close();
Serial::clear();
m_readData.clear();
Serial::sendRequest(requestIndex);
}
void Serial::handleError(QSerialPort::SerialPortError serialPortError)
{
if (serialPortError == QSerialPort::ReadError) {
QString fileName = "Errors.txt";
QFile mFile(fileName);
if(!mFile.open(QFile::Append | QFile::Text)){
qDebug() << "Could not open file for writing";
}
QTextStream out(&mFile);
out << "Serial Error " << (m_serialport->errorString()) <<endl;
mFile.close();
qDebug() <<"Serialport Error" <<(m_serialport->errorString());
}
}https://stackoverflow.com/questions/43111256
复制相似问题