I've been using the QtSerialPort add-on with no issues for about 9 months in one application. But, when I use what I think is the same code in another application, I don't get the readyRead() signal when bytes have been sent to the serial port.
I'm stuck on how to debug why it's not firing.
No message is issued when the connect statement is executed. When I have a typo in a signal or slot name, I've seen error messages. So, I think that means the connect was ok.
The open on the port succeeds along with configuration of the communication parameters, but a breakpoint on the first line of readData is never hit. Here's the relevant code:
in main.h:
class MainWindow
: public QMainWindow,
public Ui_MainWindow
{
Q_OBJECT
public:
SerialPort *serial;
...
private slots:
void readData();
...
}
class MainWindow : public QMainWindow, public Ui_MainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent=0);
SerialPort *serial;
...
private slots:
void writeData(const QByteArray &data);
void readData();
...
}
To copy to clipboard, switch view to plain text mode
in main.cpp:
MainWindow
::MainWindow(QWidget *parent
) :{
setupUi(this);
....
serial = new SerialPort(this);
if (serial_port_settings->setCurrentPort("COM47"))
openSerialPort();
QObject::connect(serial,
SIGNAL(readyRead
()),
this,
SLOT(readData
()));
}
void MainWindow::openSerialPort()
{
SettingsDialog::Settings p = serial_port_settings->settings();
serial->setPort(p.name);
if (serial->setRate(p.rate)
&& serial->setDataBits(p.dataBits)
&& serial->setParity(p.parity)
&& serial->setStopBits(p.stopBits)
&& serial->setFlowControl(p.flowControl)) {
statusbar->showMessage(tr("Connected to %1 : %2, %3, %4, %5, %6")
.arg(p.name).arg(p.stringRate).arg(p.stringDataBits)
.arg(p.stringParity).arg(p.stringStopBits).arg(p.stringFlowControl));
} else {
serial->close();
tr("Can't configure the serial port: %1,\n"
"error code: %2")
.arg(p.name).arg(serial->error()));
statusbar->showMessage(tr("Open error"));
}
} else {
tr("Can't opened the serial port: %1,\n"
"error code: %2")
.arg(p.name).arg(serial->error()));
statusbar->showMessage(tr("Configure error"));
}
}
void MainWindow::readData()
{
...
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
setupUi(this);
....
serial = new SerialPort(this);
if (serial_port_settings->setCurrentPort("COM47"))
openSerialPort();
QObject::connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
}
void MainWindow::openSerialPort()
{
SettingsDialog::Settings p = serial_port_settings->settings();
serial->setPort(p.name);
if (serial->open(QIODevice::ReadWrite)) {
if (serial->setRate(p.rate)
&& serial->setDataBits(p.dataBits)
&& serial->setParity(p.parity)
&& serial->setStopBits(p.stopBits)
&& serial->setFlowControl(p.flowControl)) {
statusbar->showMessage(tr("Connected to %1 : %2, %3, %4, %5, %6")
.arg(p.name).arg(p.stringRate).arg(p.stringDataBits)
.arg(p.stringParity).arg(p.stringStopBits).arg(p.stringFlowControl));
} else {
serial->close();
QMessageBox::critical(this, tr("Error"),
tr("Can't configure the serial port: %1,\n"
"error code: %2")
.arg(p.name).arg(serial->error()));
statusbar->showMessage(tr("Open error"));
}
} else {
QMessageBox::critical(this, tr("Error"),
tr("Can't opened the serial port: %1,\n"
"error code: %2")
.arg(p.name).arg(serial->error()));
statusbar->showMessage(tr("Configure error"));
}
}
void MainWindow::readData()
{
...
}
To copy to clipboard, switch view to plain text mode
I can run the old application that has the same code fragments and use the same method to send characters to the serial port. It still works fine.
More info--
The new application uses openCv with a Qt enhancement. cvWaitKey() is called periodically instead of entering an event loop using exec().
This has worked fine. The GUI is still responsive when the serial port should have received characters, yet no readyRead signal is generated. Is the fact that the GUI is still responsive (and my widget to widget signal/slots are working) sufficient to conclude the event loop isn't "broken", or is the mechanism somehow different for SerialPort?
Any suggestions would be greatly appreciated!
Windows 7
Thanks,
Dave Thomas
Added after 21 minutes:
I did a hack to my code and inserted a call to exec() before the main thread executed the loop with the cvWaitKey(10) command in it. The readyRead signal is issued!
So, it's definitely has something to do with the eventLoop processing.
Eventually, I need to rewrite the application to not use the Qt bolt-on that's and "experimental" bolt-on to openCv, but that's a big effort. Meanwhile, I'd like to get the SerialPort stuff working while using cvWaitKey().
So, I guess the question becomes what's magic about exec() that's not under cvWaitKey(). I'll try finding source for both.
Dave Thomas
Bookmarks