1
1
#include " Arduino.h"
2
- #include " Utils .h"
2
+ #include " Logger .h"
3
3
4
4
Arduino::~Arduino ()
5
5
{
6
- if (handle != INVALID_HANDLE_VALUE )
6
+ if (serial_port. is_open () )
7
7
{
8
- CloseHandle (handle );
9
- LOG (" Disconnected from Arduino." );
8
+ serial_port. close ( );
9
+ Logger::LogMessage (" Disconnected from Arduino." , boost:: log ::trivial::debug );
10
10
}
11
11
}
12
12
13
- Arduino::Arduino (LPCSTR name) : handle(INVALID_HANDLE_VALUE )
13
+ Arduino::Arduino (LPCSTR name) : io_context(), serial_port(io_context )
14
14
{
15
15
char port[100 ] = " \\ .\\ " ;
16
16
17
- LOG (" Searching for device..." );
17
+ Logger::LogMessage (" Searching for device..." );
18
18
while (!GetDevice (name, port))
19
19
{
20
- LOG (" Device not found. Retrying..." );
20
+ Logger::LogMessage (" Device not found. Retrying..." );
21
21
sleep_for (milliseconds (1000 ));
22
22
}
23
23
24
- LOG (string (" Device found: " ) + name + " (" + port + " )" );
24
+ Logger::LogMessage (string (" Device found: " ) + name + " (" + port + " )" );
25
25
26
- handle = CreateFile (port, GENERIC_READ | GENERIC_WRITE, 0 , nullptr , OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr );
27
-
28
- if (handle == INVALID_HANDLE_VALUE)
26
+ try
29
27
{
30
- DWORD error_code = GetLastError ();
31
- LOG (" Error opening port: " + to_string (error_code));
32
- cerr << " Error opening port: " << error_code << endl;
33
- return ;
34
- }
35
-
36
- LOG (" Port opened successfully." );
28
+ serial_port.open (port);
37
29
38
- DCB dcb = {};
39
- dcb.DCBlength = sizeof (dcb);
40
-
41
- if (!GetCommState (handle, &dcb))
30
+ if (!serial_port.is_open ())
31
+ {
32
+ Logger::LogMessage (" Error opening port: Serial port not open after attempt." );
33
+ cerr << " Error opening port: Serial port not open after attempt." << endl;
34
+ return ;
35
+ }
36
+ }
37
+ catch (boost::system ::system_error& error)
42
38
{
43
- LOG (" Failed to get port state." );
39
+ DWORD error_code = error.code ().value ();
40
+ Logger::LogMessage (" Error opening port: " + to_string (error_code) + " - " + error.what ());
41
+ cerr << " Error opening port: " << error_code << " - " << error.what () << endl;
44
42
return ;
45
43
}
46
44
47
- dcb.BaudRate = CBR_9600;
48
- dcb.ByteSize = 8 ;
49
- dcb.StopBits = ONESTOPBIT;
50
- dcb.Parity = NOPARITY;
45
+ Logger::LogMessage (" Port opened successfully." );
51
46
52
- if (! SetCommState (handle, &dcb))
47
+ try
53
48
{
54
- LOG (" Failed to set port state." );
55
- return ;
49
+ serial_port.set_option (serial_port::baud_rate (9600 ));
50
+ serial_port.set_option (serial_port::baud_rate (9600 ));
51
+ serial_port.set_option (serial_port::character_size (8 ));
52
+ serial_port.set_option (serial_port::stop_bits (serial_port::stop_bits::one));
53
+ serial_port.set_option (serial_port::parity (serial_port::parity::none));
54
+ serial_port.set_option (serial_port::flow_control (serial_port::flow_control::none));
56
55
}
57
-
58
- COMMTIMEOUTS cto = {};
59
- cto.ReadIntervalTimeout = 50 ;
60
- cto.ReadTotalTimeoutConstant = 100 ;
61
- cto.ReadTotalTimeoutMultiplier = 10 ;
62
- cto.WriteTotalTimeoutConstant = 50 ;
63
- cto.WriteTotalTimeoutMultiplier = 10 ;
64
-
65
- if (!SetCommTimeouts (handle, &cto))
56
+ catch (boost::system ::system_error& error)
66
57
{
67
- LOG (" Failed to set timeouts." );
58
+ Logger::LogMessage (" Failed to set port options: " + string (error.what ()));
59
+ cerr << " Failed to set port options: " << error.what () << endl;
60
+ serial_port.close ();
68
61
return ;
69
62
}
70
63
71
- LOG (" Port initialized successfully." );
64
+ Logger::LogMessage (" Port initialized successfully." );
72
65
cout << " Successfully connected!" << endl;
73
66
}
74
67
@@ -78,7 +71,7 @@ bool Arduino::GetDevice(LPCSTR name, LPSTR port)
78
71
79
72
if (device_info == INVALID_HANDLE_VALUE)
80
73
{
81
- LOG (" Failed to get device information (SetupDiGetClassDevs returned INVALID_HANDLE_VALUE)." );
74
+ Logger::LogMessage (" Failed to get device information (SetupDiGetClassDevs returned INVALID_HANDLE_VALUE)." );
82
75
return false ;
83
76
}
84
77
@@ -96,7 +89,7 @@ bool Arduino::GetDevice(LPCSTR name, LPSTR port)
96
89
bool config_loaded = Arduino::LoadConfiguration (" device_config.cfg" , config_device);
97
90
98
91
// Collect system devices
99
- LOG (" Enumerating connected devices..." );
92
+ Logger::LogMessage (" Enumerating connected devices..." );
100
93
DWORD count = 0 ;
101
94
SP_DEVINFO_DATA dev_info_data = {};
102
95
dev_info_data.cbSize = sizeof (dev_info_data);
@@ -108,13 +101,13 @@ bool Arduino::GetDevice(LPCSTR name, LPSTR port)
108
101
if (Arduino::ExtractProperties (device_info, dev_info_data, system_device))
109
102
{
110
103
devices.push_back (system_device);
111
- LOG (" Detected device: FRIENDLYNAME = " + system_device.friendly_name + " , HARDWAREID = " + system_device.hardware_id + " , PORT = " + system_device.port );
104
+ Logger::LogMessage (" Detected device: FRIENDLYNAME = " + system_device.friendly_name + " , HARDWAREID = " + system_device.hardware_id + " , PORT = " + system_device.port );
112
105
113
106
// Check against configuration file
114
107
if (config_loaded && system_device.friendly_name == config_device.friendly_name && system_device.hardware_id == config_device.hardware_id )
115
108
{
116
109
strncpy_s (port, 100 , system_device.port .c_str (), system_device.port .size ());
117
- LOG (" Using saved device: FRIENDLYNAME = " + system_device.friendly_name + " , HARDWAREID = " + system_device.hardware_id + " , PORT = " + system_device.port );
110
+ Logger::LogMessage (" Using saved device: FRIENDLYNAME = " + system_device.friendly_name + " , HARDWAREID = " + system_device.hardware_id + " , PORT = " + system_device.port );
118
111
found_device = true ;
119
112
break ;
120
113
}
@@ -123,7 +116,7 @@ bool Arduino::GetDevice(LPCSTR name, LPSTR port)
123
116
if (!found_device && name && system_device.friendly_name .find (name) != string::npos)
124
117
{
125
118
strncpy_s (port, 100 , system_device.port .c_str (), system_device.port .size ());
126
- LOG (" Device matched by FRIENDLYNAME: " + system_device.friendly_name );
119
+ Logger::LogMessage (" Device matched by FRIENDLYNAME: " + system_device.friendly_name );
127
120
found_device = true ;
128
121
break ;
129
122
}
@@ -133,7 +126,7 @@ bool Arduino::GetDevice(LPCSTR name, LPSTR port)
133
126
if (regex_search (system_device.hardware_id , pattern))
134
127
{
135
128
strncpy_s (port, 100 , system_device.port .c_str (), system_device.port .size ());
136
- LOG (" Device matched by VID/PID: " + system_device.hardware_id );
129
+ Logger::LogMessage (" Device matched by VID/PID: " + system_device.hardware_id );
137
130
found_device = true ;
138
131
break ;
139
132
}
@@ -151,17 +144,17 @@ bool Arduino::GetDevice(LPCSTR name, LPSTR port)
151
144
{
152
145
if (config_loaded)
153
146
{
154
- LOG (" Saved device not found in the system. Deleting configuration file." );
147
+ Logger::LogMessage (" Saved device not found in the system. Deleting configuration file." );
155
148
remove (" device_config.cfg" );
156
149
}
157
150
158
151
if (devices.empty ())
159
152
{
160
- LOG (" No devices found in the system." );
153
+ Logger::LogMessage (" No devices found in the system." );
161
154
return false ;
162
155
}
163
156
164
- LOG (" Prompting user to select a device..." );
157
+ Logger::LogMessage (" Prompting user to select a device..." );
165
158
return Arduino::SelectDevice (devices, port);
166
159
}
167
160
@@ -175,7 +168,7 @@ bool Arduino::LoadConfiguration(const string& file_name, DeviceInfo& config_devi
175
168
176
169
if (!config)
177
170
{
178
- LOG (" Configuration file not found." );
171
+ Logger::LogMessage (" Configuration file not found." , boost:: log ::trivial::debug );
179
172
return false ;
180
173
}
181
174
@@ -197,11 +190,11 @@ bool Arduino::LoadConfiguration(const string& file_name, DeviceInfo& config_devi
197
190
198
191
if (!config_device.friendly_name .empty () && !config_device.hardware_id .empty ())
199
192
{
200
- LOG (" Loaded device from configuration: FRIENDLYNAME = " + config_device.friendly_name + " , HARDWAREID = " + config_device.hardware_id + " , PORT = " + config_device.port );
193
+ Logger::LogMessage (" Loaded device from configuration: FRIENDLYNAME = " + config_device.friendly_name + " , HARDWAREID = " + config_device.hardware_id + " , PORT = " + config_device.port );
201
194
return true ;
202
195
}
203
196
204
- LOG (" Configuration file is incomplete or corrupted." );
197
+ Logger::LogMessage (" Configuration file is incomplete or corrupted." );
205
198
return false ;
206
199
}
207
200
@@ -250,7 +243,7 @@ bool Arduino::SelectDevice(const vector<DeviceInfo>& devices, LPSTR port)
250
243
{
251
244
const auto & selected_device = devices[choice - 1 ];
252
245
strncpy_s (port, 100 , selected_device.port .c_str (), selected_device.port .size ());
253
- LOG (" User selected device: " + selected_device.friendly_name );
246
+ Logger::LogMessage (" User selected device: " + selected_device.friendly_name );
254
247
255
248
ofstream config (" device_config.cfg" );
256
249
@@ -260,50 +253,37 @@ bool Arduino::SelectDevice(const vector<DeviceInfo>& devices, LPSTR port)
260
253
config << " HARDWAREID=" << selected_device.hardware_id << endl;
261
254
config << " PORT=" << selected_device.port << endl;
262
255
263
- LOG (" Device selection saved to configuration." );
256
+ Logger::LogMessage (" Device selection saved to configuration." );
264
257
}
265
258
266
259
return true ;
267
260
}
268
261
269
262
utils.PrintCenteredText (" No device selected by the user." );
270
- LOG (" No device selected by the user." );
263
+ Logger::LogMessage (" No device selected by the user." );
271
264
272
265
return false ;
273
266
}
274
267
275
- bool Arduino::WriteMessage (const string& message) const
268
+ bool Arduino::WriteMessage (const string& message)
276
269
{
277
- DWORD bytes = 0 ;
278
- BOOL result = WriteFile (handle, message.c_str (), message.size () + 1 , &bytes, nullptr );
279
-
280
- if (result == 0 || bytes != message.size () + 1 )
270
+ if (!serial_port.is_open ())
281
271
{
282
- LOG ( " Failed to send message: " + message );
283
- cerr << " Failed to send message! " << endl;
272
+ Logger::LogMessage ( " Attempt to write to a closed serial port " );
273
+ cerr << " Attempt to write to a closed serial port " << endl;
284
274
return false ;
285
275
}
286
276
287
- LOG (" Message sent: " + message);
288
- return true ;
289
- }
290
-
291
- void Arduino::LogMessage (const string& message)
292
- {
293
- static ofstream log_file (" arduino_debug.log" , ios::app);
294
-
295
- if (!log_file.is_open ())
277
+ try
296
278
{
297
- cerr << " Error opening the log file." << endl;
298
- return ;
279
+ write (serial_port, buffer (message.c_str (), message.size () + 1 ));
280
+ Logger::LogMessage (" Message sent: " + message);
281
+ return true ;
282
+ }
283
+ catch (boost::system ::system_error& error)
284
+ {
285
+ Logger::LogMessage (" Failed to send message: " + message + " - " + error.what ());
286
+ cerr << " Failed to send message: " << message << " - " << error.what () << endl;
287
+ return false ;
299
288
}
300
-
301
- tm local;
302
- time_t now = time (nullptr );
303
- localtime_s (&local, &now);
304
-
305
- char buffer[100 ];
306
- strftime (buffer, sizeof (buffer), " %Y-%m-%d %H:%M:%S" , &local);
307
-
308
- log_file << " [" << buffer << " ] " << message << std::endl;
309
289
}
0 commit comments