-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcpsteppirclient.cpp
More file actions
149 lines (124 loc) · 4.38 KB
/
tcpsteppirclient.cpp
File metadata and controls
149 lines (124 loc) · 4.38 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
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
// TcpSteppirClient.cpp
#include "TcpSteppirClient.h"
#include <QDebug>
TcpSteppirClient::TcpSteppirClient(const QString &host, quint16 port, QObject *parent)
: SteppirClient(parent), host(host), port(port)
{
connect(&socket, &QTcpSocket::readyRead, this, &TcpSteppirClient::onReadyRead);
connect(&socket, &QTcpSocket::disconnected, this, &TcpSteppirClient::onDisconnected);
connect(&socket, &QTcpSocket::connected, this, &TcpSteppirClient::onConnected);
connect(&socket, &QTcpSocket::errorOccurred, this, &TcpSteppirClient::onSocketError);
reconnectTimer.setInterval(3000);
reconnectTimer.setSingleShot(false);
pollTimer.setInterval(1500);
pollTimer.setSingleShot(false);
connect(&reconnectTimer, &QTimer::timeout, this, &TcpSteppirClient::tryReconnect);
connect(&pollTimer, &QTimer::timeout, this, &TcpSteppirClient::sendStatusRequest);
}
void TcpSteppirClient::onSocketError(QAbstractSocket::SocketError socketError) {
Q_UNUSED(socketError); // optional if you only use `errorString()`
qWarning() << "TcpSteppirClient socket error:" << socket.errorString();
reconnectTimer.start();
QString errMsg = socket.errorString();
emit connectionError(errMsg);
}
void TcpSteppirClient::connectToDevice()
{
qDebug() << "Connecting to Steppir at" << host << ":" << port;
socket.connectToHost(host, port);
}
void TcpSteppirClient::disconnectFromDevice()
{
qDebug() << "Disconnect";
reconnectTimer.stop();
socket.disconnectFromHost();
}
void TcpSteppirClient::sendCommand(const QByteArray &cmd)
{
if (socket.state() == QAbstractSocket::ConnectedState) {
socket.write(cmd);
}
}
bool TcpSteppirClient::isConnected() const
{
return socket.state() == QAbstractSocket::ConnectedState;
}
void TcpSteppirClient::onReadyRead() {
buffer.append(socket.readAll());
int endIdx;
while ((endIdx = buffer.indexOf('\r')) != -1) {
QByteArray frame = buffer.left(endIdx);
buffer.remove(0, endIdx + 1); // remove processed frame and '\r'
emit receivedStatus(QString::fromUtf8(frame));
if (frame.length() == 10 && frame[0] == '@' && frame[1] == 'A') {
SteppirStatus status;
// Parse frequency (bytes 3-5)
quint32 freq10 = (quint8)frame[3] << 16 |
(quint8)frame[4] << 8 |
(quint8)frame[5];
status.frequencyMHz = freq10 / 100000.0;
// Active motor flags (byte 6)
status.activeMotors = (quint8)frame[6];
quint8 byte = static_cast<quint8>(frame[7]);
// Direction (byte 7)
quint8 dir = (byte & 0xe0) >> 5;//(quint8)frame[7];
if (dir == 2) //(dir & 0x02)
status.direction = "180";
else if (dir == 4) //(dir & 0x01)
status.direction = "Bi-directional";
else if (dir == 3) //(dir & 0x04)
status.direction = "3/4 Wave";
else
status.direction = "Normal";
quint8 track = byte >> 2;
status.trackFlag = track;
// qDebug() << frame;
emit receivedParsedStatus(status);
}
}
}
void TcpSteppirClient::onDisconnected()
{
emit connectionStateChanged(false);
reconnectTimer.start();
pollTimer.stop();
}
void TcpSteppirClient::tryReconnect()
{
qDebug() << "Trying to reconnect...";
if (socket.state() != QAbstractSocket::ConnectedState) {
qDebug() << "Trying to reconnect...";
socket.abort();
socket.connectToHost(host, port);
}
}
void TcpSteppirClient::onConnected() {
// emit connectionChanged(true);
qDebug() << "Connected to Steppir at" << host << ":" << port;
pollTimer.start(); // 👈 Start polling
reconnectTimer.stop();
}
void TcpSteppirClient::sendStatusRequest() {
if (socket.isOpen()) {
QByteArray cmd;
cmd.append(0x3F);
cmd.append(0x41);
cmd.append(0x0D); // CR
socket.write(cmd);
socket.flush();
}
}
void TcpSteppirClient::close() {
if (socket.isValid()) {
if (socket.isOpen()) {
socket.disconnectFromHost();
if (socket.state() != QAbstractSocket::UnconnectedState) {
socket.waitForDisconnected(1000); // wait max 1s
}
}
//socket.deleteLater();
}
}
TcpSteppirClient::~TcpSteppirClient() {
close();
}