-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCOM_Communication.cpp
More file actions
207 lines (152 loc) · 3.88 KB
/
COM_Communication.cpp
File metadata and controls
207 lines (152 loc) · 3.88 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "COM_Communication.h"
COM_Communication::COM_Communication()
{
status = "Initialized";
}
COM_Communication::~COM_Communication()
{
/*
if (!CloseHandle((HANDLE)hSerial))
{
throw std::runtime_error("Failed to close COM handle");
}
*/
}
void COM_Communication::ConnectHandle(LPCWSTR COM_PORT)
{
hSerial = CreateFileW(
COM_PORT,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0
);
if (hSerial == INVALID_HANDLE_VALUE)
{
if (GetLastError() == ERROR_FILE_NOT_FOUND)
{
SetStatus("Could not connect");
}
}
else
{
std::wstring result(COM_PORT);
SetStatus("Connected to " + result);
}
}
void COM_Communication::DisconnectHandle()
{
shouldRun.store(false);
threadHandler.join();
if(!CloseHandle((HANDLE)hSerial))
{
throw std::runtime_error("Failed to close COM handle");
}
}
wxString COM_Communication::GetStatus()
{
return status;
}
void COM_Communication::SetStatus(wxString newStatus)
{
status = newStatus + "\n";
}
void COM_Communication::ConfigureComPort(DWORD BaudRate)
{
DCB dcbSerialParams = { 0 };
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
if (hSerial != INVALID_HANDLE_VALUE)
{
if (!GetCommState(hSerial, &dcbSerialParams))
{
SetStatus("Error getting state");
}
dcbSerialParams.BaudRate = BaudRate;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONE5STOPBITS;
dcbSerialParams.Parity = NOPARITY;
if (!SetCommState(hSerial, &dcbSerialParams))
{
SetStatus("Error setting serial port state");
}
SetStatus("Handle worked!");
}
else
{
SetStatus("Error in handle");
}
}
void COM_Communication::SetTimeouts()
{
COMMTIMEOUTS timeouts = { 0 };
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
if (!SetCommTimeouts(hSerial, &timeouts))
{
SetStatus("Error in timeout settings");
}
else
{
SetStatus("Timeout settings set!");
}
}
void COM_Communication::WriteComPort(unsigned char& buff)
{
DWORD dwBytesRead = NULL;
if (!WriteFile(hSerial, &buff, sizeof(char), &dwBytesRead, NULL))
{
SetStatus("Error has occured in WriteComPort");
}
else
{
SetStatus("Data sent!");
}
}
void COM_Communication::InitCOMReader(dataPanel* rawPanel)
{
threadHandler = std::thread(&COM_Communication::ReadIncomingData, this, rawPanel);
}
void COM_Communication::ReadIncomingData(dataPanel* rawPanel)
{
unsigned char buff = ' ';
while (shouldRun.load())
{
std::unique_lock<std::mutex> lock(mutex_);
cv.wait(lock, [&] { return !shouldPause; });
DWORD dwBytesRead = NULL;
if (!ReadFile(hSerial, &buff, sizeof(char), &dwBytesRead, NULL))
{
SetStatus("Error has occured in ReadIncomingData");
}
else
{
std::ostringstream oss;
// Format the byte as hexadecimal and save it into the stringstream
oss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(buff);
// Extract the string from the stringstream
std::string hexString = oss.str();
rawPanel->AddTextToBox(hexString + " ");
}
}
}
void COM_Communication::pauseReadingData()
{
{
std::lock_guard<std::mutex> lock(mutex_);
shouldPause.store(true);
SetStatus("Thread paused");
}
cv.notify_all();
}
void COM_Communication::resumeReadingData()
{
{
std::lock_guard<std::mutex> lock(mutex_);
shouldPause.store(false);
SetStatus("Thread resumed");
}
cv.notify_all();
}