PDA

View Full Version : Opening a serial terminal based on COM port selection from QComboBox



anita.niharika@gmail.com
7th January 2021, 21:41
Hi,

OS - Windows 10
Qt version - Qt5

In my Qt GUI, I have a QComboBox for the COM port selection and "connect" push button. When I click on connect button, able to configure serial port properly and open() call is successful.

I am trying to build an serial port application between PC and a device.

But I am able to see a serial console terminal on clicking connect button. Can somebody help me out here!

Thanks in Advance,
Anita

ChrisW67
7th January 2021, 22:03
Help with what exactly?

anita.niharika@gmail.com
8th January 2021, 04:45
Small correction, I am not able to open a new terminal on clicking connect push button for the COM port selected through combo box.

d_stranz
9th January 2021, 00:31
Why do you think that opening a serial port should create a complete terminal application?

I suggest you study Qt's Terminal example (https://doc.qt.io/qt-5/qtserialport-terminal-example.html). If you use Qt Creator, it is one of the examples on the Examples part of the Welcome page.

anita.niharika@gmail.com
12th January 2021, 06:15
Thanks, It was my understanding that just by configuring serial port and clicking a button would open a serial port.
Yes, I have already executed Terminal example while exploring documents. For now, I think not necessary to open a new terminal.

My serial interface frame format is, SOF | command | Len | Data | Checksum | EOF. I would like to extract my read data based on command types with variable Len. Which is the better way to store frame data, buffer or structure?

connect(serial, &QSerialPort::readyRead, this, &MainWindow::serialReadyRead);

void MainWindow::serialReadyRead()
{

QByteArray data = serial->readAll();
QString str = QString(data);
qDebug()<<"Received data : "<<data;

/* SOF | command | Len | Data | Checksum | EOF */
/* parsing serial frame data takes place here */
}

Where serialReadyRead() function, parses serial frame received data from another PC. Is there any correction required in the above implementation?

Best Regards,
Anita

Lesiok
12th January 2021, 09:45
Yes, the readyRead signal is generated after a few (one, several, different) bytes, not after a packet. Serial port knows nothing about your data structure. You have to build the package in the buffer yourself and start its analysis after receiving the EOF.

anita.niharika@gmail.com
21st January 2021, 18:30
Thanks...

Just want to know, to build a package I am using QByteArray. But my Len and Checksum are of 2 bytes in size. How can I append 2 bytes to QByteArray?

Best Regards,
Anita

d_stranz
21st January 2021, 20:39
How can I append 2 bytes to QByteArray?
QByteArray::append() sounds like it could be useful for appending to a QByteArray.