Menu

Qt5 serial terminal porting2

big-bass

original documentation Qt source

https://doc.qt.io/qt-6/qtserialport-terminal-example.html

if you want to skip this part and jump to another part
Part 1 MainWindow

Part 3 openSerialPort
Part 4 closeSerialPort
Part 5 writeData
Part 6 readData

2.) QSerialPort

The only QSerialPort signal invoked in this example is readyRead(), which shows that new data has been received and hence available:

    connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::readData);

}

2.) ported and modified

CONNECT(m_serial, &QSerialPort::readyRead, readData_cb)

since I rewrote console.cpp and console.h

the callbacks work differently QPlainTextEdit is the console code now
and we get events from it in real time no callbacks are used here it is keypress event driven!

another part of the code is the real callback readData_cb()

'------------------------------------------------
SUB  QPlainTextEdit::keyPressEvent(QKeyEvent *event)
'------------------------------------------------
    'Typing characters in the console is handled here 
    'typed string commands converted to serial data 

    'modified code writeData_cb triggered by send icon
    'this allows the send icon
    'to simulate the ENTER key giving you two options to send data    
    m_serial->write(event->text().toLocal8Bit())
    textEdit->insertPlainText(event->text())

    IF  (event->key() == Qt::Key_Return) THEN
        qDebug() << "Key_Return pressed"
        bar->setValue(bar->maximum())
        'PRINTOUT(event->text().toLocal8Bit())
    ENDIF   
END SUB

'------------------------------------------------
SUB readData_cb()
'------------------------------------------------  
 'The only QSerialPort signal invoked in this code 
 'is readyRead(), which shows that new data 
 'has been received and is available
    const QByteArray data2 = m_serial->readAll()
    textEdit->appendPlainText(data2)
END SUB

there are two ways we can send data to the serial port
the first is the Enter key which is handled in
SUB QPlainTextEdit::keyPressEvent(QKeyEvent *event)

the second way to send data to the serial port is by using the Send icon in the Toolbar


'------------------------------------------------
SUB writeData_cb()
'------------------------------------------------
    'using the send icon
    'manually send from the menu icon simulate the enter key
     m_serial->write("\r")
END SUB
    'added another way to send the enter key using an icon
    CONNECT(actionSend, &QAction::triggered, writeData_cb)