-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialendpoint.cpp
More file actions
83 lines (70 loc) · 2.14 KB
/
Copy pathserialendpoint.cpp
File metadata and controls
83 lines (70 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "serialendpoint.h"
#include "settingsdialog.h"
#include <QMessageBox>
#include <QQueue>
SerialEndpoint::SerialEndpoint(QObject *parent) :
Endpoint(parent),
m_settings(new SettingsDialog),
m_serial(new QSerialPort(this))
{
connect(m_serial, &QSerialPort::errorOccurred, this, &SerialEndpoint::handleError);
connect(m_serial, &QSerialPort::readyRead, this, &SerialEndpoint::readData);
max_burst_length = 16;
max_packet_length = 1;
}
SerialEndpoint::~SerialEndpoint()
{
close();
delete m_settings;
delete m_serial;
}
bool SerialEndpoint::open()
{
const SettingsDialog::Settings p = m_settings->settings();
QString port = p.name;
#if (defined Q_OS_WIN)
if (!port.startsWith("\\\\.\\"))
port = "\\\\.\\" + port;
#endif
m_serial->setPortName(port);
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)) {
emit statusMessage(tr("Connected to %1 : %2, %3, %4, %5, %6")
.arg(p.name).arg(p.stringBaudRate).arg(p.stringDataBits)
.arg(p.stringParity).arg(p.stringStopBits).arg(p.stringFlowControl));
} else {
emit statusMessage(tr("Open error"));
QMessageBox::critical(static_cast<QWidget*>(this->parent()), tr("Error"), m_serial->errorString());
}
return m_serial->isOpen();
}
void SerialEndpoint::close()
{
if (m_serial->isOpen())
m_serial->close();
}
void SerialEndpoint::showDialog()
{
m_settings->show();
}
bool SerialEndpoint::writeData(const QByteArray &data)
{
qint64 written = m_serial->write(data);
return written == data.length();
}
void SerialEndpoint::readData()
{
const QByteArray data = m_serial->readAll();
emit getData(data);
}
void SerialEndpoint::handleError(QSerialPort::SerialPortError error)
{
if (error == QSerialPort::ResourceError) {
QMessageBox::critical(static_cast<QWidget*>(this->parent()), tr("Critical Error"), m_serial->errorString());
close();
}
}