Apart Concept 1 Serial Controller Program with modern UI
This commit is contained in:
commit
c4bde4c18c
103 changed files with 38385 additions and 0 deletions
103
src/transport/SerialTransport.cpp
Normal file
103
src/transport/SerialTransport.cpp
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
#include "SerialTransport.h"
|
||||
#include "protocol/Concept1Protocol.h"
|
||||
|
||||
#include <QSerialPort>
|
||||
#include <QSerialPortInfo>
|
||||
|
||||
SerialTransport::SerialTransport(const QString &portName, QObject *parent)
|
||||
: ITransport(parent)
|
||||
, m_port(new QSerialPort(this))
|
||||
, m_portName(portName)
|
||||
{
|
||||
m_port->setBaudRate(Concept1::kBaudRate);
|
||||
m_port->setDataBits(QSerialPort::Data8);
|
||||
m_port->setParity(QSerialPort::NoParity);
|
||||
m_port->setStopBits(QSerialPort::OneStop);
|
||||
m_port->setFlowControl(QSerialPort::NoFlowControl);
|
||||
|
||||
connect(m_port, &QSerialPort::readyRead, this, &SerialTransport::onReadyRead);
|
||||
connect(m_port, &QSerialPort::errorOccurred, this,
|
||||
[this](QSerialPort::SerialPortError e) {
|
||||
if (e == QSerialPort::NoError)
|
||||
return;
|
||||
emit errorOccurred(m_port->errorString());
|
||||
if (e == QSerialPort::ResourceError && m_port->isOpen()) {
|
||||
m_port->close();
|
||||
emit openedChanged(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
SerialTransport::~SerialTransport()
|
||||
{
|
||||
if (m_port->isOpen())
|
||||
m_port->close();
|
||||
}
|
||||
|
||||
void SerialTransport::setPortName(const QString &portName)
|
||||
{
|
||||
m_portName = portName;
|
||||
}
|
||||
|
||||
bool SerialTransport::open()
|
||||
{
|
||||
if (m_port->isOpen())
|
||||
return true;
|
||||
m_port->setPortName(m_portName);
|
||||
const bool ok = m_port->open(QIODevice::ReadWrite);
|
||||
if (ok) {
|
||||
m_port->setBaudRate(Concept1::kBaudRate);
|
||||
m_port->setDataBits(QSerialPort::Data8);
|
||||
m_port->setParity(QSerialPort::NoParity);
|
||||
m_port->setStopBits(QSerialPort::OneStop);
|
||||
m_port->setFlowControl(QSerialPort::NoFlowControl);
|
||||
emit openedChanged(true);
|
||||
} else {
|
||||
emit errorOccurred(m_port->errorString());
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
void SerialTransport::close()
|
||||
{
|
||||
if (m_port->isOpen()) {
|
||||
m_port->close();
|
||||
emit openedChanged(false);
|
||||
}
|
||||
}
|
||||
|
||||
bool SerialTransport::isOpen() const
|
||||
{
|
||||
return m_port->isOpen();
|
||||
}
|
||||
|
||||
void SerialTransport::write(const QByteArray &data)
|
||||
{
|
||||
if (m_port->isOpen())
|
||||
m_port->write(data);
|
||||
}
|
||||
|
||||
QString SerialTransport::endpointName() const
|
||||
{
|
||||
return m_portName;
|
||||
}
|
||||
|
||||
void SerialTransport::onReadyRead()
|
||||
{
|
||||
const QByteArray data = m_port->readAll();
|
||||
if (!data.isEmpty())
|
||||
emit bytesReceived(data);
|
||||
}
|
||||
|
||||
QStringList SerialTransport::availablePorts()
|
||||
{
|
||||
QStringList result;
|
||||
const auto ports = QSerialPortInfo::availablePorts();
|
||||
for (const QSerialPortInfo &info : ports) {
|
||||
QString entry = info.portName();
|
||||
if (!info.description().isEmpty())
|
||||
entry += QStringLiteral(" :: ") + info.description();
|
||||
result << entry;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue