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