-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.cpp
63 lines (52 loc) · 2.13 KB
/
commands.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
//******** commands.cpp
#include "commands.h"
#include <QBigEndianStorageType>
// Helper function to convert uint32_t to big Endian QByteArray
QByteArray uint32ToBigEndian(uint32_t value) {
QByteArray byteArray;
byteArray.append(static_cast<char>((value >> 24) & 0xFF));
byteArray.append(static_cast<char>((value >> 16) & 0xFF));
byteArray.append(static_cast<char>((value >> 8) & 0xFF));
byteArray.append(static_cast<char>(value & 0xFF));
return byteArray;
}
Poke::Poke(QSerialPort* serial, QObject* parent) : QObject(parent), serial(serial) {}
Peek::Peek(QSerialPort* serial, QObject* parent) : QObject(parent), serial(serial) {}
Version::Version(QSerialPort* serial, QObject* parent) : QObject(parent), serial(serial) {}
void Poke::execute(uint32_t address, uint32_t data) {
QByteArray message;
message.append('W'); // Command character
// Convert address and data to big Endian and append
message.append(uint32ToBigEndian(address));
message.append(uint32ToBigEndian(data));
// Write the message to the serial port
serial->write(message);
serial->flush();
}
QByteArray Peek::execute(uint32_t address) {
QByteArray message;
message.append('R'); // Command character
message.append(uint32ToBigEndian(address)); // Convert address to big Endian
serial->write(message);
serial->flush();
if (serial->waitForReadyRead(1000)) {
QByteArray responseData = serial->readAll();
if (responseData.size() >= sizeof(uint32_t)) {
// Convert from big endian to host endianess
uint32_t responseValue = qFromBigEndian<quint32>(
reinterpret_cast<const uchar*>(responseData.constData()));
return QByteArray(reinterpret_cast<const char*>(&responseValue), sizeof(responseValue));
}
}
return QByteArray(); // Return empty if no response
}
QByteArray Version::execute() {
QByteArray message;
message.append('V'); // Command character
serial->write(message);
serial->flush();
if (serial->waitForReadyRead(1000)) {
return serial->readAll();
}
return QByteArray(); // Return empty if no response
}