mirror of
https://github.com/Serial-Studio/Serial-Studio.git
synced 2025-01-15 05:22:53 +08:00
Do not work with serial port pointer if it has been disconnected
This commit is contained in:
parent
385826a8ce
commit
8637a5b9fe
@ -21,6 +21,7 @@
|
||||
*/
|
||||
|
||||
import QtQuick 2.12
|
||||
import Qt.labs.settings 1.0
|
||||
import QtQuick.Layouts 1.12
|
||||
import QtQuick.Controls 2.12
|
||||
|
||||
@ -38,6 +39,14 @@ Control {
|
||||
property alias text: _console.text
|
||||
readonly property color consoleColor: Qt.rgba(142/255, 205/255, 157/255, 1)
|
||||
|
||||
//
|
||||
// Save settings
|
||||
//
|
||||
Settings {
|
||||
category: "Console"
|
||||
property alias writeEnabled: writeSw.checked
|
||||
}
|
||||
|
||||
//
|
||||
// Connections with serial manager
|
||||
//
|
||||
@ -105,6 +114,15 @@ Control {
|
||||
RowLayout {
|
||||
Layout.fillWidth: true
|
||||
|
||||
Switch {
|
||||
id: writeSw
|
||||
checked: true
|
||||
Layout.alignment: Qt.AlignHCenter
|
||||
onCheckedChanged: CppSerialManager.writeEnabled = checked
|
||||
opacity: CppSerialManager.readWrite ? 1 : (CppSerialManager.connected ? 0.5 : 1)
|
||||
Behavior on opacity {NumberAnimation{}}
|
||||
}
|
||||
|
||||
TextField {
|
||||
id: _tf
|
||||
height: 24
|
||||
@ -112,6 +130,7 @@ Control {
|
||||
Layout.fillWidth: true
|
||||
color: root.consoleColor
|
||||
font.family: app.monoFont
|
||||
opacity: enabled ? 1 : 0.5
|
||||
enabled: CppSerialManager.readWrite
|
||||
palette.base: Qt.rgba(18/255, 18/255, 24/255, 1)
|
||||
placeholderText: qsTr("Send data to device") + "..." + CppTranslator.dummy
|
||||
|
@ -130,6 +130,7 @@ Control {
|
||||
Label {
|
||||
text: qsTr("COM Port") + ":" + CppTranslator.dummy
|
||||
} ComboBox {
|
||||
id: portSelector
|
||||
Layout.fillWidth: true
|
||||
model: CppSerialManager.portList
|
||||
currentIndex: CppSerialManager.portIndex
|
||||
@ -137,6 +138,14 @@ Control {
|
||||
if (CppSerialManager.portIndex !== currentIndex)
|
||||
CppSerialManager.portIndex = currentIndex
|
||||
}
|
||||
|
||||
Connections {
|
||||
target: CppSerialManager
|
||||
function onAvailablePortsChanged() {
|
||||
if (portSelector.currentIndex !== CppSerialManager.portIndex)
|
||||
portSelector.currentIndex = CppSerialManager.portIndex
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
|
@ -210,6 +210,14 @@ int SerialManager::maxBufferSize() const
|
||||
return m_maxBufferSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns @c true if we want to open the port in RW mode
|
||||
*/
|
||||
bool SerialManager::writeEnabled() const
|
||||
{
|
||||
return m_writeEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the size of the data received (and successfully read) from the
|
||||
* serial device.
|
||||
@ -473,6 +481,8 @@ void SerialManager::disconnectDevice()
|
||||
|
||||
// Reset pointer
|
||||
m_port = nullptr;
|
||||
m_portIndex = 0;
|
||||
emit availablePortsChanged();
|
||||
|
||||
// Reset received bytes
|
||||
m_receivedBytes = 0;
|
||||
@ -554,8 +564,8 @@ void SerialManager::setPort(const quint8 portIndex)
|
||||
return;
|
||||
|
||||
// Update port index variable & disconnect from current serial port
|
||||
m_portIndex = portIndex;
|
||||
disconnectDevice();
|
||||
m_portIndex = portIndex;
|
||||
|
||||
// Ignore the first item of the list (Select Port)
|
||||
if (portIndex > 0)
|
||||
@ -587,28 +597,22 @@ void SerialManager::setPort(const quint8 portIndex)
|
||||
this, SLOT(handleError(QSerialPort::SerialPortError)));
|
||||
// clang-format on
|
||||
|
||||
// Try to open the serial port in R/W mode
|
||||
if (port()->open(QIODevice::ReadWrite))
|
||||
// Select open mode for serial port
|
||||
auto mode = QIODevice::ReadOnly;
|
||||
if (writeEnabled())
|
||||
mode = QIODevice::ReadWrite;
|
||||
|
||||
// Try to open the port in R/RW
|
||||
if (port()->open(mode))
|
||||
{
|
||||
emit connectedChanged();
|
||||
LOG_INFO() << Q_FUNC_INFO
|
||||
<< "Serial port opened successfully in RW mode";
|
||||
}
|
||||
|
||||
// Try to open the serial port only for reading
|
||||
else if (port()->open(QIODevice::ReadOnly))
|
||||
{
|
||||
emit connectedChanged();
|
||||
LOG_INFO() << Q_FUNC_INFO
|
||||
<< "Serial port opened successfully in READ mode";
|
||||
}
|
||||
|
||||
// Close serial port on error
|
||||
else
|
||||
{
|
||||
disconnectDevice();
|
||||
LOG_INFO() << "Serial port open error" << port()->errorString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -617,6 +621,23 @@ void SerialManager::setPort(const quint8 portIndex)
|
||||
LOG_INFO() << "Serial port selection set to" << portName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables/disables RW mode
|
||||
*/
|
||||
void SerialManager::setWriteEnabled(const bool enabled)
|
||||
{
|
||||
m_writeEnabled = enabled;
|
||||
|
||||
if (connected())
|
||||
{
|
||||
auto index = portIndex();
|
||||
disconnectDevice();
|
||||
setPort(index);
|
||||
}
|
||||
|
||||
emit writeEnabledChanged();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief SerialManager::setParity
|
||||
* @param parityIndex
|
||||
@ -992,7 +1013,7 @@ void SerialManager::handleError(QSerialPort::SerialPortError error)
|
||||
if (error != QSerialPort::NoError)
|
||||
{
|
||||
auto errorStr = port()->errorString();
|
||||
setPort(0);
|
||||
disconnectDevice();
|
||||
NiceMessageBox(tr("Critical serial port error"), errorStr);
|
||||
}
|
||||
}
|
||||
|
@ -55,6 +55,10 @@ class SerialManager : public QObject
|
||||
READ maxBufferSize
|
||||
WRITE setMaxBufferSize
|
||||
NOTIFY maxBufferSizeChanged)
|
||||
Q_PROPERTY(bool writeEnabled
|
||||
READ writeEnabled
|
||||
WRITE setWriteEnabled
|
||||
NOTIFY writeEnabledChanged)
|
||||
Q_PROPERTY(QString startSequence
|
||||
READ startSequence
|
||||
WRITE setStartSequence
|
||||
@ -115,6 +119,7 @@ signals:
|
||||
void stopBitsChanged();
|
||||
void connectedChanged();
|
||||
void flowControlChanged();
|
||||
void writeEnabledChanged();
|
||||
void textDocumentChanged();
|
||||
void maxBufferSizeChanged();
|
||||
void startSequenceChanged();
|
||||
@ -136,6 +141,7 @@ public:
|
||||
bool connected() const;
|
||||
QString portName() const;
|
||||
int maxBufferSize() const;
|
||||
bool writeEnabled() const;
|
||||
QString receivedBytes() const;
|
||||
QString startSequence() const;
|
||||
QString finishSequence() const;
|
||||
@ -167,6 +173,7 @@ public slots:
|
||||
void disconnectDevice();
|
||||
void sendData(const QString &data);
|
||||
void setPort(const quint8 portIndex);
|
||||
void setWriteEnabled(const bool enabled);
|
||||
void setParity(const quint8 parityIndex);
|
||||
void setBaudRate(const quint8 baudRateIndex);
|
||||
void setDataBits(const quint8 dataBitsIndex);
|
||||
@ -202,6 +209,7 @@ private:
|
||||
quint64 m_receivedBytes;
|
||||
quint8 m_flowControlIndex;
|
||||
|
||||
bool m_writeEnabled;
|
||||
int m_maxBufferSize;
|
||||
|
||||
QString m_startSeq;
|
||||
|
Loading…
x
Reference in New Issue
Block a user