QSerialPort not getting readyRead signal from UART
-
I'm attempting to implement a serial communication object using QSerialPort on an ARM based processor running WEC7. I am able to send data, but I am not getting a readyRead signal, and any data on the port is not being seen. I have another application written in VS2008 which uses File IO with polling and it is seeing data on the port, so I know my hardware is working. I've reimplemented the VS2008 application to use SetCommMask to process the EV_RXCHAR event, and the WaitCommEvent function to capture the event. I am not seeing this event. Has anyone else run into this issue? I'm presently assuming the UART driver is not sending the event and therefore QSerialPort will not issue the readyRead signal. I'm also assuming that QSerialPort does not support polling, as it does not see data if the readyRead or waitForReadyRead are not processed (verified on a PC through Qt Creator). I've attached the code for my implementation of QSerialPort.
@
#include "serialcomm.h"SerialComm::SerialComm()
{
bDEBUG = false;
serial = new QSerialPort();connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(handleError(QSerialPort::SerialPortError))); connect(serial, SIGNAL(readyRead()), this, SLOT(readData())); Open();
}
void SerialComm::readData()
{
QString dataRead = serial->readAll();
inputBuffer.append(dataRead);if(dataRead.right(1)!= ">") // command terminator from device { inputReady = false; } else { inputReady = true; if(bDEBUG) qDebug() << inputBuffer; }
}
void SerialComm::handleError(QSerialPort::SerialPortError error)
{
if (error == QSerialPort::ResourceError)
{
//QMessageBox::critical(this->parent(), QString("Critical Error"), serial->errorString());
Close();
}
}// function to flush the buffers on the COM device
// RX - receive TX - transmit BOTH - both
void SerialComm::PurgeComm()
{
inputBuffer.clear();
inputReady = false;
}// function to open the stream tot he COM device
bool SerialComm::Open()
{
serial->setPortName("COM0"); // SoM portif (serial->open(QIODevice::ReadWrite)) { serial->setBaudRate(QSerialPort::Baud38400); // PC port serial->setDataBits(QSerialPort::Data8); serial->setParity(QSerialPort::NoParity); serial->setStopBits(QSerialPort::OneStop); serial->setFlowControl(QSerialPort::NoFlowControl); return true; } else { serial->close(); return false; //QMessageBox::critical(this, tr("Error"), serial->errorString()); }
}
// function to close the stream to the COM device
void SerialComm::Close()
{
serial->close();
}// function to send a string to the COM device transmit buffer
void SerialComm::Write(QString sBuf)
{
if(sentButNotRead)
{
QString sTemp;
Read(sTemp);
}
PurgeComm();
QByteArray dataSend = sBuf.toAscii();//QString(ui->command->currentText()+"\r").toAscii();
serial->write(dataSend);
if(bDEBUG)
qDebug() <<dataSend;
sentButNotRead = true;
}// function to load the contents of the COM device receive buffer into sBuf
void SerialComm::Read(QString &sBuf, int iMaxWaitTime)
{
sBuf = "";
QTime startTime;
startTime.start();
QTime nowTime = QTime::currentTime();
QTime endTime = nowTime.addMSecs(iMaxWaitTime);
while( (!inputReady) && (nowTime < endTime) )
{
Delay(3);
nowTime = QTime::currentTime();
}
if(inputReady)
{
sentButNotRead = false;
sBuf = inputBuffer;
}
if(nowTime >= endTime)
{
sBuf = QString("no response! after %1 ms").arg(startTime.elapsed());
//qDebug() << "\nCOM ERROR!\n";
}
return;
}// delay function---units in milliseconds
void SerialComm::Delay(int iMS)
{
QTime dieTime = QTime::currentTime().addMSecs(iMS);
while(QTime::currentTime() < dieTime)
{
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
}void SerialComm::ReadCommand(QString &sCom)
{
ReadCommand(sCom, 50);
}void SerialComm::ReadCommand(QString &sCom, int iMaxWaitTime)
{
Read(sCom, iMaxWaitTime);}
}
@[edit: added missing coding tags @ SGaist]
-
QSerialPort are not tested on WEC7.
-
The QSerialPort object is working on WEC7. It needs to be built separately and included. The issue turned out to be caused by trying to use QString as the input buffer, where QByteArray should have been used. Everything is working as expected now.
-
The QSerialPort object is working on WEC7. It needs to be built separately and included. The issue turned out to be caused by trying to use QString as the input buffer, where QByteArray should have been used. Everything is working as expected now.
-
Hmm.. It is strange that QtSerialPort works on WinCE, because its sources was broken (may broken, as I know). It is just magic.. :)
-
Hmm.. It is strange that QtSerialPort works on WinCE, because its sources was broken (may broken, as I know). It is just magic.. :)