if you want to skip this part and jump to another part
Part 1 MainWindow
Part 2 readyRead signal
Part 4 closeSerialPort
Part 5 writeData
Part 6 readData
Clicking on the Connect button invokes the openSerialPort() slot:
void MainWindow::openSerialPort()
{
const SettingsDialog::Settings p = m_settings->settings();
m_serial->setPortName(p.name);
m_serial->setBaudRate(p.baudRate);
m_serial->setDataBits(p.dataBits);
m_serial->setParity(p.parity);
m_serial->setStopBits(p.stopBits);
m_serial->setFlowControl(p.flowControl);
if (m_serial->open(QIODevice::ReadWrite)) {
m_console->setEnabled(true);
m_console->setLocalEchoEnabled(p.localEchoEnabled);
m_ui->actionConnect->setEnabled(false);
m_ui->actionDisconnect->setEnabled(true);
m_ui->actionConfigure->setEnabled(false);
showStatusMessage(tr("Connected to %1 : %2, %3, %4, %5, %6")
.arg(p.name, p.stringBaudRate, p.stringDataBits,
p.stringParity, p.stringStopBits, p.stringFlowControl));
} else {
QMessageBox::critical(this, tr("Error"), m_serial->errorString());
showStatusMessage(tr("Open error"));
}
}
In this slot, the settings are read from SettingsDialog and an attempt is made to open and initialize the serial port accordingly. If successful, the status bar displays a message that the opening was successful with the given configuration; otherwise, a messagebox is displayed with the appropriate error code and message. If the serial port settings have never been called then the terminal attempts to open the port with the default settings: 9600 8N1.
'------------------------------------------------
SUB openSerialPort_cb()
'------------------------------------------------
'Clicking on the Connect button invokes the openSerialPort_cb
LOCAL BR ,DB ,PAR ,SB ,FC TYPE int
LOCAL NM TYPE QString
LOCAL mymessage TYPE QString
m_serial->setPortName(m_currentSettings->name)
m_serial->setBaudRate(m_currentSettings->baudRate)
m_serial->setDataBits(m_currentSettings->dataBits)
m_serial->setParity(m_currentSettings->parity)
m_serial->setStopBits(m_currentSettings->stopBits)
m_serial->setFlowControl(m_currentSettings->flowControl)
IF (m_serial->open(QIODevice::ReadWrite)) THEN
textEdit->setEnabled(true)
'm_serial->setLocalEchoEnabled(true)
actionConnect->setEnabled(false)
actionDisconnect->setEnabled(true)
actionConfigure->setEnabled(false)
'all this to get the status bar to display dynamically!
QString NM = m_currentSettings->name
BR = m_currentSettings->baudRate
DB = m_currentSettings->dataBits
PAR = m_currentSettings->parity
SB = m_currentSettings->stopBits
FC = m_currentSettings->flowControl
mymessage = QStringLiteral("Connected to %1,Baud %2 \
,Data bits %3 ,Parity %4, Stop bits %5 , \
Flow control %6 \
").arg(NM).arg(BR).arg(DB).arg(PAR).arg(SB).arg(FC)
PRINTOUT(mymessage)
statusBar->showMessage(mymessage)
ELSE
QMessageBox::critical(0, "Error", m_serial->errorString())
statusBar->showMessage("Open error")
END IF
END SUB
some added code for it to work remember we are not using any class header code!
so we have to handle some things differently
I converted the class header code
from settingsdialog.h
public:
struct Settings {
QString name;
qint32 baudRate;
QString stringBaudRate;
QSerialPort::DataBits dataBits;
QString stringDataBits;
QSerialPort::Parity parity;
QString stringParity;
QSerialPort::StopBits stopBits;
QString stringStopBits;
QSerialPort::FlowControl flowControl;
QString stringFlowControl;
bool localEchoEnabled;
};
Settings struct manually converted from a struct to a bacon CLASS below
now we can use the m_currentSettings to get the values from the
low level QSerialPort code detection !
```
CLASS Settings
public :
QString name;
qint32 baudRate;
QString stringBaudRate;
QSerialPort::DataBits dataBits;
QString stringDataBits;
QSerialPort::Parity parity;
QString stringParity;
QSerialPort::StopBits stopBits;
QString stringStopBits;
QSerialPort::FlowControl flowControl;
QString stringFlowControl;
bool localEchoEnabled;
END CLASS
```
'in BaCon we cant forget to DECLARE
DECLARE m_currentSettings TYPE Settings*
'and we make a copy of the Settings CLASS in memory as a pointer
'so that BaCon can read from the low level
m_currentSettings = new Settings()