forked from asmwarrior/FactoryTestApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RailtestClient.cpp
158 lines (123 loc) · 3.81 KB
/
RailtestClient.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "RailtestClient.h"
#include <QCoreApplication>
#include <QTime>
RailtestClient::RailtestClient(QObject *parent)
: QObject(parent)
{
connect(&m_serial, &QSerialPort::readyRead, this, &RailtestClient::onSerialPortReadyRead);
connect(&m_serial, &QSerialPort::errorOccurred, this, &RailtestClient::onSerialPortErrorOccurred);
}
RailtestClient::~RailtestClient()
{
close();
}
bool RailtestClient::open(const QString &portName)
{
close();
m_serial.setPortName(portName);
m_serial.setBaudRate(QSerialPort::Baud115200);
m_serial.setDataBits(QSerialPort::Data8);
m_serial.setParity(QSerialPort::NoParity);
m_serial.setStopBits(QSerialPort::OneStop);
m_serial.setFlowControl(QSerialPort::NoFlowControl);
return m_serial.open(QSerialPort::ReadWrite);
}
void RailtestClient::close()
{
if (m_serial.isOpen())
{
m_serial.flush();
m_serial.close();
m_recvBuffer.clear();
}
}
bool RailtestClient::waitCommandPrompt(int timeout)
{
if (!m_serial.isOpen())
return false;
m_syncCommand = "__waitCommandPrompt__";
QTime expire = QTime::currentTime().addMSecs(timeout);
m_serial.write("\r\n");
while (QTime::currentTime() <= expire)
{
QCoreApplication::processEvents();
if (m_syncCommand.isEmpty())
return true;
}
return false;
}
QVariantList RailtestClient::syncCommand(const QByteArray &cmd, const QByteArray &args, int timeout)
{
if (!m_serial.isOpen())
return QVariantList();
m_syncCommand = cmd;
m_syncReplies.clear();
QTime expire = QTime::currentTime().addMSecs(timeout);
m_serial.write(cmd + " " + args + "\r\n");
while (QTime::currentTime() <= expire)
{
QCoreApplication::processEvents();
if (m_syncCommand.isEmpty())
return m_syncReplies;
}
QVariantMap error;
error.insert("error", "timeout");
error.insert("errorCode", 1);
m_syncReplies.append(error);
return m_syncReplies;
}
void RailtestClient::decodeReply(const QByteArray &reply)
{
if (reply.startsWith("{{(") && reply.endsWith("}}"))
{
int idx = reply.indexOf(")}");
if (idx == -1)
return;
auto name = reply.mid(3, idx - 3);
auto params = reply.mid(idx + 2, reply.length() - idx - 3).split('}');
QVariantMap decodedParams;
foreach (auto param, params)
if (param.startsWith('{'))
{
idx = param.indexOf(':');
if (idx != -1)
decodedParams.insert(param.mid(1, idx - 1), param.mid(idx + 1));
}
if (m_syncCommand == name)
m_syncReplies.append(decodedParams);
else
emit replyReceived(name, decodedParams);
return;
}
if (reply.startsWith("{{") && reply.endsWith("}}"))
{
auto params = reply.mid(1, reply.length() - 2).split('}');
QVariantList decodedParams;
foreach (auto param, params)
if (param.startsWith('{'))
decodedParams.append(param.mid(1));
if (!m_syncCommand.isEmpty())
m_syncReplies.push_back(decodedParams);
return;
}
}
void RailtestClient::onSerialPortReadyRead() Q_DECL_NOTHROW
{
while (m_serial.bytesAvailable())
m_recvBuffer += m_serial.readAll();
int idx = m_recvBuffer.indexOf("\r\n");
while (idx != -1)
{
auto reply = m_recvBuffer.left(idx);
decodeReply(reply);
m_recvBuffer = m_recvBuffer.mid(idx + 2);
idx = m_recvBuffer.indexOf("\r\n");
}
if (m_recvBuffer == "> ")
m_syncCommand.clear();
}
void RailtestClient::onSerialPortErrorOccurred(QSerialPort::SerialPortError errorCode) Q_DECL_NOTHROW
{
if (errorCode != QSerialPort::NoError)
emit error(m_serial.errorString());
}