This repository was archived by the owner on Jan 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSerialThread.cpp
More file actions
166 lines (141 loc) · 4.95 KB
/
Copy pathSerialThread.cpp
File metadata and controls
166 lines (141 loc) · 4.95 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <wx/app.h>
#include <wx/event.h>
#include "serialib.h"
#include "SerialThread.h"
#include <filesystem>
#include "SerialPortEnumerator.h"
#include "Events.h"
#include <iostream>
#include <thread>
#include "MeasurementEvent.h"
enum FrameType { OSD_MODE_20V = 20, OSD_MODE_28V = 28 };
static int8_t hex2bin(const unsigned char c) {
if (c >= '0' && c <= '9') {
return c - '0'; // NOLINT(*-narrowing-conversions)
}
if (c >= 'A' && c <= 'F') {
return c - 'A' + 10; // NOLINT(*-narrowing-conversions)
}
if (c >= 'a' && c <= 'f') {
return c - 'a' + 10; // NOLINT(*-narrowing-conversions)
}
return 0;
}
static int16_t hex4_to_uint16(const char *buf) {
const int16_t val = 0 | (hex2bin(buf[0]) << 12) | (hex2bin(buf[1]) << 8) | // NOLINT(*-narrowing-conversions)
(hex2bin(buf[2]) << 4) | hex2bin(buf[3]);
return val;
}
void SerialThread::updateStatus(const wxString &status) {
auto *event = new wxThreadEvent(wxEVT_STATUS_UPDATE);
event->SetString(status);
if (!this->m_frame) {
std::cerr << "m_frame is NULL!!!" << std::endl;
return;
}
if (!TestDestroy()) {
wxQueueEvent(this->m_frame, event);
}
}
bool SerialThread::measure_loop(const std::string &device) {
auto port = new serialib();
char code;
if ((code = port->openDevice(device.c_str(), 9600)) != 1) {
std::cerr << "Failed to open" << device << " return code=" << code
<< std::endl;
return false;
}
// ReSharper disable once CppExpressionWithoutSideEffects
port->flushReceiver(); // Puffer leeren
std::this_thread::sleep_for(std::chrono::milliseconds(100));
char line[100];
int bytes_read = port->readString(line, '\n', 100, 5000);
if (bytes_read < 0) {
std::cerr << "Read error" << std::endl;
return false;
}
bytes_read = port->readString(line, '\n', 100, 1000);
if (bytes_read < 0) {
std::cerr << "Read error or timeout" << std::endl;
port->closeDevice();
return false;
}
// hexdump(line);
int frame_type;
if (bytes_read == 9) {
frame_type = OSD_MODE_20V;
if (line[8] == OSD_MODE_28V) {
frame_type = OSD_MODE_28V;
} else if (line[8] == OSD_MODE_20V) {
frame_type = OSD_MODE_20V;
}
} else if (bytes_read == 8) {
frame_type = OSD_MODE_20V;
} else {
std::cerr << "Cannot obtain frame type" << std::endl;
port->closeDevice();
return false;
}
while (true) {
double voltage_quanta;
double current_quanta; // with 100mR shunt
if (TestDestroy()) return false;
bytes_read = port->readString(line, '\n', 100, 250);
if (bytes_read < 0) {
std::cerr << "Read error or timeout" << std::endl;
break;
}
// hexdump(line);
if (frame_type == OSD_MODE_28V) {
voltage_quanta = 3.125;
current_quanta = 0.2; // with 50mR shunt
} else {
voltage_quanta = 4.0;
current_quanta = 0.06; // with 100mR shunt
}
// printf("len=%d\n", static_cast<int>(strlen(serial_buffer)));
if (strlen(line) < 8 || strlen(line) > 11) {
continue;
}
int shunt_voltage = hex4_to_uint16(line);
// printf("Shunt: %d\n", shunt_voltage);
auto bus_voltage = static_cast<double>(hex4_to_uint16(line + 4));
// if (frame_type == OSD_MODE_20V && (bus_voltage & 0x0001)) {
// std::cerr << "bad data?" << std::endl;
// break;
// }
if (frame_type == OSD_MODE_20V)
bus_voltage /= 8.0;
int milliamps = abs(static_cast<int>(static_cast<double>(shunt_voltage) * current_quanta));
int millivolts = static_cast<int>(bus_voltage * voltage_quanta);
if (TestDestroy()) break;
auto event = new MeasurementEvent(millivolts, milliamps);
wxQueueEvent(this->m_frame, event);
}
if (!TestDestroy()) {
wxQueueEvent(this->m_frame, new MeasurementEvent(0, 0));
}
port->closeDevice();
return true;
}
wxThread::ExitCode SerialThread::Entry() {
updateStatus("Thread started");
while (true) {
std::cout << "=== Starting port enumeration ===" << std::endl; // Add this
auto enumerator = new SerialPortEnumerator();
if (TestDestroy())
break;
auto ports = enumerator->GetPortNames(); // Store result
for (const auto &port: ports) {
std::cout << "Processing port: " << port.ToStdString() << std::endl; // Add this
updateStatus(wxString::Format("Trying %s",
std::filesystem::path(std::string(port.c_str())).filename().string()
));
this->measure_loop(std::string(port.c_str()));
}
delete enumerator;
updateStatus("Waiting for device");
Sleep(1000);
}
return nullptr;
}