Question

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. 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 connects and works 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...

Was it helpful?

Solution

First off, your code looks flawless. I see no issues.

That being said, I ran into a similar issue when using a virtual COM port. The issue is that the readyRead signal implementation is device driver specific. Try running a hard line connection and see if that clears up your issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top