PDA

View Full Version : QtSerialport Inconsistant Connection Issue



awpitt13
10th December 2013, 17:28
I have used this library in the past for Qt4 but I am currently attempting to use it on a project that involves a lot more incoming data. When I attempt to connect to the correct COM port in my program, it shows no packets being received and throws no errors saying it failed to connect. When I use another terminal program it shows the constant flow of data. After several attempts to connect to the COM port my program finally shows data flowing correctly. I need my program to be able to consistently connect to a COM port when commanded. If there is anyone that has any ideas about what could be wrong with my code I would really appreciate your assistance.

#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>

void MainWindow::comportSelected()
{
// If some other serial port is open then close it.
if(serial.isOpen())
serial.close();

if(ui->comportList->currentIndex() != 0)
{
serial.setDataBits(QSerialPort::Data8);
serial.setFlowControl(QSerialPort::NoFlowControl);
serial.setBaudRate(QSerialPort::Baud115200);
serial.setParity(QSerialPort::NoParity);
serial.setPort(comPortList.at(ui->comportList->currentIndex()-1));

if(!serial.open(QIODevice::ReadWrite))
{
QMessageBox::critical(this, tr("Error"), serial.errorString());
ui->console->setEnabled(false);

}

else
{
connect((const QObject*)&serial, SIGNAL(readyRead()), this, SLOT(processPendingSerialData()));

}
}
else serial.close();
}I then read like:

void MainWindow::processPendingSerialData()
{
// While there are bytes in the buffer.
while(serial.bytesAvailable() > 0)
{
// Read a byte.
serial.read((char *)&ch, 1);

etc...

kuzulis
11th December 2013, 08:14
You have a wrong initialization sequence.

At first need to do open() and then do setup properties, please read documentation and look examples.