Menu

Qt5 serial terminal porting

big-bass

everything is based off the original documentation Qt source

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

Part 1 MainWindow of several pages

if you want to skip this part and jump to another part
Part 2 readyRead signal
Part 3 openSerialPort
Part 4 closeSerialPort
Part 5 writeData
Part 6 readData

just to document some of the things I did to port to bacon before I forget
and may be useful if someone wants to port code from Qt
since it is not documented officially this is the best I can do for the moment
one section at a time and one app at a time

I will try to explain the main reason for what is done in the porting process
to get a different view point standing back and looking at it from a distance first
BaCon cant connect to the c++ low level widget codes events because
bacon is running on C based code and it also doesn't see namespaces that
are part of ( Qtcreator the GUI builder for Qt)
so we have to go around some of these problems another way
not to mention solving all the qmake linking whereas BaCon doesn't use qmake
BaCon by default uses make

so code like this UI::
wont work in BaCon
and I don't want to give a long winded explanation as to why
so I will keep it short as possible

UI:: code is in xml it is the same idea if you code using gtk's glade
and with the magic of a lot behind the scene parsing and conversions
and MOC compiling to connect the GUI UI code to the C++ code
(that is not part of BaCon and shouldnt be )

so I will remove all UI code and port it manually to pure c++ code
because we are not compiling code now using Qtcreator
but we have to make it all work without the tools Qt uses
such as qmake and the MOC compiler signals and slots ....

m_ui(new Ui::MainWindow)

simplified says m_u i= MainWindow
in the ported to bacon code below
the old variable m_u i wont be used anymore
we will rename it MainWindow
and all our code will be in this MainWindow

 MainWindow = new QMainWindow()

yes it is ugly but once converted its easy

the Ugly part is every time you see "::"
being used just remember it is low level c++ widget code
or some namespace that is using Class headers
connected to signals and slots that we will
convert to SUBS and FUNCTIONS callbacks
to make life a little bit easier

1.) Original UI code MainWindow

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    m_ui(new Ui::MainWindow),
    m_status(new QLabel),
    m_console(new Console),
      m_settings(new SettingsDialog(this)),
    m_serial(new QSerialPort(this))
{
    ...
    m_serial(new QSerialPort(this))

1.) Ported to bacon in two parts code and DECLARE's

MainWindow = new QMainWindow()
statusBar = new QStatusBar(MainWindow)
textEdit = new QPlainTextEdit(centralWidget)
m_settings = new QDialog()
m_serial = new QSerialPort()

notice I reworked this to non UI xml code
by removing all ui code

m_ui(new Ui::MainWindow),

becomes simplified to
MainWindow = new QMainWindow()

  m_status(new QLabel)

becomes
statusBar = new QStatusBar(MainWindow)
I used the status bar to update the info instead of a label

 m_console(new Console)

becomes

textEdit = new QPlainTextEdit(centralWidget)

because I rewrote the console.h and console.cpp code
and the console code is really based off the QPlainTextEdit widget
it is not a real console as we expect in linux such as bash

second part DECLARE

1.b Ported to bacon we need to add the DECLARE's

DECLARE MainWindow TYPE QMainWindow*
DECLARE statusBar TYPE QStatusBar*
DECLARE textEdit TYPE QPlainTextEdit*
DECLARE m_settings TYPE  QDialog*
DECLARE m_serial TYPE  QSerialPort*  

notice they are all pointers to the class widgets
but they are using the new command so we have a way
to manage the memory and point to the low level c++ widget code and its events
dont use dot member code because bacon wont be able to connect to the widgets!
dot member is fine in pure C code but not C++


Part2 readyRead signal