-
Notifications
You must be signed in to change notification settings - Fork 0
/
xn.cpp
141 lines (119 loc) · 4.13 KB
/
xn.cpp
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "xn.h"
#include "xn-win-com-discover.h"
/* Global definitions & helpers of XpressNet class. Specific functions that
* do the real work are places in xn-*.cpp (logically divided into multiple
* shorter source codes.
*/
namespace Xn {
XpressNet::XpressNet(QObject *parent) : QObject(parent) {
m_serialPort.setReadBufferSize(256);
m_lastSent = QDateTime::currentDateTime();
QObject::connect(&m_serialPort, SIGNAL(readyRead()), this, SLOT(handleReadyRead()));
QObject::connect(&m_serialPort, SIGNAL(errorOccurred(QSerialPort::SerialPortError)), this,
SLOT(handleError(QSerialPort::SerialPortError)));
QObject::connect(&m_pending_timer, SIGNAL(timeout()), this, SLOT(m_pending_timer_tick()));
m_out_timer.setInterval(m_config.outInterval);
QObject::connect(&m_out_timer, SIGNAL(timeout()), this, SLOT(m_out_timer_tick()));
QObject::connect(&m_serialPort, SIGNAL(aboutToClose()), this, SLOT(sp_about_to_close()));
}
XpressNet::~XpressNet() {
try {
if (m_serialPort.isOpen())
m_serialPort.close();
} catch (...) {
// No exceptions in destructor
}
}
void XpressNet::sp_about_to_close() {
m_pending_timer.stop();
m_out_timer.stop();
while (!m_pending.empty()) {
if (nullptr != m_pending.front().callback_err)
m_pending.front().callback_err->func(this, m_pending.front().callback_err->data);
m_pending.pop_front();
}
while (!m_out.empty()) {
if (nullptr != m_out.front().callback_err)
m_out.front().callback_err->func(this, m_out.front().callback_err->data);
m_out.pop_front();
}
m_trk_status = TrkStatus::Unknown;
log("Disconnected", LogLevel::Info);
}
void XpressNet::log(const QString &message, const LogLevel loglevel) {
if (loglevel <= this->loglevel)
emit onLog(message, loglevel);
}
void XpressNet::handleError(QSerialPort::SerialPortError serialPortError) {
if (serialPortError != QSerialPort::NoError)
emit onError(m_serialPort.errorString());
}
QString XpressNet::xnReadCVStatusToQString(const ReadCVStatus st) {
if (st == ReadCVStatus::Ok)
return "Ok";
if (st == ReadCVStatus::ShortCircuit)
return "Short Circuit";
if (st == ReadCVStatus::DataByteNotFound)
return "Data Byte Not Found";
if (st == ReadCVStatus::CSbusy)
return "Command station busy";
if (st == ReadCVStatus::CSready)
return "Command station ready";
return "Unknown error";
}
bool XpressNet::liAcknowledgesSetAccState() const {
return (m_liType == LIType::uLI || m_liType == LIType::LIUSBEth);
}
LIType XpressNet::liType() const { return m_liType; }
LIType liInterface(const QString &name) {
if (name == "LI101")
return Xn::LIType::LI101;
if (name == "uLI")
return Xn::LIType::uLI;
if (name == "LI-USB-Ethernet")
return Xn::LIType::LIUSBEth;
return Xn::LIType::LI100;
}
QString liInterfaceName(const LIType &type) {
if (type == LIType::LI101)
return "LI101";
if (type == LIType::uLI)
return "uLI";
if (type == LIType::LIUSBEth)
return "LI-USB-Ethernet";
return "LI100";
}
QString flowControlToStr(QSerialPort::FlowControl fc) {
if (fc == QSerialPort::FlowControl::HardwareControl)
return "hardware";
if (fc == QSerialPort::FlowControl::SoftwareControl)
return "software";
if (fc == QSerialPort::FlowControl::NoFlowControl)
return "no";
return "unknown";
}
std::vector<QSerialPortInfo> XpressNet::ports(LIType litype) {
if (litype != LIType::uLI)
throw EUnsupportedInterface("Cannot autodetect port for "+liInterfaceName(litype));
#ifdef Q_OS_WIN
return winULIPorts();
#else
std::vector<QSerialPortInfo> result;
QList<QSerialPortInfo> ports(QSerialPortInfo::availablePorts());
for (const QSerialPortInfo &info : ports)
if (info.description().startsWith("uLI"))
result.push_back(info);
return result;
#endif
}
XNConfig XpressNet::config() const {
return m_config;
}
void XpressNet::setConfig(const XNConfig config) {
if ((config.outInterval < _OUT_TIMER_INTERVAL_MIN) || (config.outInterval > _OUT_TIMER_INTERVAL_MAX))
throw EInvalidConfig("outInterval="+QString::number(config.outInterval)+" is out of range ["+
QString::number(_OUT_TIMER_INTERVAL_MIN)+"-"+QString::number(_OUT_TIMER_INTERVAL_MAX)+"]");
m_config = config;
m_out_timer.setInterval(m_config.outInterval);
}
} // namespace Xn