added serial port settings synchronisation

This commit is contained in:
Bernhard Trinnes 2018-06-14 01:14:26 +02:00 committed by Michael Zanetti
parent 66494c74f7
commit a9d6770709
4 changed files with 70 additions and 12 deletions

View File

@ -44,8 +44,9 @@ DeviceManager::DeviceSetupStatus DevicePluginSerialPortCommander::setupDevice(De
serialPort->setBaudRate(device->paramValue(serialPortInputBaudRateParamTypeId).toInt());
serialPort->setDataBits(QSerialPort::DataBits(device->paramValue(serialPortInputDataBitsParamTypeId).toInt()));
serialPort->setParity(QSerialPort::Parity(device->paramValue(serialPortInputParityParamTypeId).toInt()));
//TODO set parity
serialPort->setStopBits(QSerialPort::StopBits(device->paramValue(serialPortInputStopBitsParamTypeId).toInt()));
//TODO set flow control
if (!serialPort->open(QIODevice::ReadWrite)) {
qCWarning(dcSerialPortCommander()) << "Could not open serial port" << interface << serialPort->errorString();
@ -78,8 +79,9 @@ DeviceManager::DeviceSetupStatus DevicePluginSerialPortCommander::setupDevice(De
serialPort->setBaudRate(device->paramValue(serialPortInputBaudRateParamTypeId).toInt());
serialPort->setDataBits(QSerialPort::DataBits(device->paramValue(serialPortInputDataBitsParamTypeId).toInt()));
serialPort->setParity(QSerialPort::Parity(device->paramValue(serialPortInputParityParamTypeId).toInt()));
//TODO set parity
serialPort->setStopBits(QSerialPort::StopBits(device->paramValue(serialPortInputStopBitsParamTypeId).toInt()));
//TODO set flow control
if (!serialPort->open(QIODevice::ReadWrite)) {
qCWarning(dcSerialPortCommander()) << "Could not open serial port" << interface << serialPort->errorString();
@ -122,13 +124,13 @@ DeviceManager::DeviceError DevicePluginSerialPortCommander::discoverDevices(cons
parameters.append(Param(serialPortInputSerialPortParamTypeId, serialPort->portName()));
parameters.append(Param(serialPortInputBaudRateParamTypeId, serialPort->baudRate()));
parameters.append(Param(serialPortInputDataBitsParamTypeId, serialPort->dataBits()));
parameters.append(Param(serialPortInputFlowControlParamTypeId, serialPort->flowControl()));
//TODO set flow control
parameters.append(Param(serialPortInputStopBitsParamTypeId, serialPort->stopBits()));
parameters.append(Param(serialPortInputParityParamTypeId, serialPort->parity()));
//TODO set parity
}
if (deviceClassId == serialPortOutputDeviceClassId) {
if (serialPortCommander->hasOutputDevice()){
if (serialPortCommander->hasOutputDevice()) {
//only one output per port is allowed
continue;
}
@ -136,9 +138,9 @@ DeviceManager::DeviceError DevicePluginSerialPortCommander::discoverDevices(cons
parameters.append(Param(serialPortOutputSerialPortParamTypeId, serialPort->portName()));
parameters.append(Param(serialPortOutputBaudRateParamTypeId, serialPort->baudRate()));
parameters.append(Param(serialPortOutputDataBitsParamTypeId, serialPort->dataBits()));
parameters.append(Param(serialPortOutputFlowControlParamTypeId, serialPort->flowControl()));
//TODO set flow control
parameters.append(Param(serialPortOutputStopBitsParamTypeId, serialPort->stopBits()));
parameters.append(Param(serialPortOutputParityParamTypeId, serialPort->parity()));
//TODO set parity
}
descriptor.setParams(parameters);

View File

@ -26,7 +26,7 @@
"displayName": "Serial port",
"type": "QString",
"inputType": "TextLine",
"defaultValue": "/dev/ttyAMA0"
"defaultValue": "ttyAMA0"
},
{
"id": "45dfc828-f238-4263-89a3-9b35cf5dea39",
@ -114,7 +114,7 @@
"displayName": "Serial Port",
"type": "QString",
"inputType": "TextLine",
"defaultValue": "/dev/ttyAMA0"
"defaultValue": "ttyAMA0"
},
{
"id": "45dfc828-f238-4263-89a3-9b35cf5dea39",

View File

@ -26,6 +26,11 @@ SerialPortCommander::SerialPortCommander(QSerialPort *serialPort, QObject *paren
{
connect(m_serialPort, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(onSerialError(QSerialPort::SerialPortError)));
connect(m_serialPort, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
connect(m_serialPort, SIGNAL(baudRateChanged(qint32, QSerialPort::Direction)), this, SLOT(onBaudRateChanged(qint32, QSerialPort::Direction)));
connect(m_serialPort, SIGNAL(parityChanged(QSerialPort::Parity)), this, SLOT(onParityChanged(QSerialPort::Parity)));
connect(m_serialPort, SIGNAL(dataBitsChanged(QSerialPort::DataBits)), this, SLOT(onDataBitsChanged(QSerialPort::DataBits)));
connect(m_serialPort, SIGNAL(stopBitsChanged(QSerialPort::StopBits)), this, SLOT(onStopBitsChanged(QSerialPort::StopBits)));
connect(m_serialPort, SIGNAL(flowControlChanged(QSerialPort::FlowControl)), this, SLOT(onFlowControlChanged(QSerialPort::FlowControl)));
}
@ -39,7 +44,6 @@ SerialPortCommander::~SerialPortCommander()
void SerialPortCommander::addOutputDevice(Device* device)
{
m_outputDevice = device;
return;
}
@ -57,7 +61,7 @@ void SerialPortCommander::addInputDevice(Device* device)
void SerialPortCommander::removeInputDevice(Device* device)
{
m_inputDevices.removeOne(device);
m_inputDevices.removeAll(device);
}
@ -69,7 +73,7 @@ bool SerialPortCommander::isEmpty()
bool SerialPortCommander::hasOutputDevice()
{
if (m_outputDevice == NULL) {
if (m_outputDevice == nullptr) {
return false;
} else {
return true;
@ -127,6 +131,52 @@ void SerialPortCommander::onSerialError(QSerialPort::SerialPortError error)
Q_UNUSED(error);
}
void SerialPortCommander::onBaudRateChanged(qint32 baudRate, QSerialPort::Direction direction)
{
Q_UNUSED(direction);
foreach(Device *device, m_inputDevices) {
device->setParamValue(serialPortInputBaudRateParamTypeId, baudRate);
}
if(m_outputDevice != nullptr)
m_outputDevice->setParamValue(serialPortOutputBaudRateParamTypeId, baudRate);
}
void SerialPortCommander::onParityChanged(QSerialPort::Parity parity)
{
foreach(Device *device, m_inputDevices) {
device->setParamValue(serialPortInputParityParamTypeId, parity); //TODO Strings not int
}
if(m_outputDevice != nullptr)
m_outputDevice->setParamValue(serialPortOutputBaudRateParamTypeId, parity);
}
void SerialPortCommander::onDataBitsChanged(QSerialPort::DataBits dataBits)
{
foreach(Device *device, m_inputDevices) {
device->setParamValue(serialPortInputDataBitsParamTypeId, dataBits);
}
if(m_outputDevice != nullptr)
m_outputDevice->setParamValue(serialPortOutputDataBitsParamTypeId, dataBits);
}
void SerialPortCommander::onStopBitsChanged(QSerialPort::StopBits stopBits)
{
foreach(Device *device, m_inputDevices) {
device->setParamValue(serialPortInputStopBitsParamTypeId, stopBits);
}
if(m_outputDevice != nullptr)
m_outputDevice->setParamValue(serialPortOutputStopBitsParamTypeId, stopBits);
}
void SerialPortCommander::onFlowControlChanged(QSerialPort::FlowControl flowControl)
{
//foreach(Device *device, m_inputDevices) { //TODO enum to string
//device->setParamValue(serialPortInputFlowControlParamTypeId, QVariant::fromValue(QSerialPort::FlowControl).value<QString>());
//}
if(m_outputDevice != nullptr)
m_outputDevice->setParamValue(serialPortOutputFlowControlParamTypeId, flowControl);
}
void SerialPortCommander::sendCommand(QByteArray data)
{
m_serialPort->write(data);

View File

@ -60,8 +60,14 @@ signals:
void commandReceived(Device *device);
public slots:
void onReadyRead();
void onSerialError(QSerialPort::SerialPortError error);
void onBaudRateChanged(qint32 baudRate, QSerialPort::Direction direction);
void onParityChanged(QSerialPort::Parity parity);
void onDataBitsChanged(QSerialPort::DataBits dataBits);
void onStopBitsChanged(QSerialPort::StopBits stopBits);
void onFlowControlChanged(QSerialPort::FlowControl flowControl);
};