@@ -37,8 +37,8 @@ static std::map<enum SerialPort::eFlowControl, serial_port_base::flow_control> c
37
37
struct SerialPort_Params
38
38
{
39
39
std::string device;
40
- uint32_t baudrate;
41
- enum SerialPort::eFlowControl flowControl;
40
+ uint32_t baudrate = 115200 ;
41
+ enum SerialPort::eFlowControl flowControl = SerialPort::eFlowControl::None ;
42
42
SerialPort::ISerialHandler* handler = nullptr ;
43
43
44
44
SerialPort_Params (const std::string& device, uint32_t baudrate, enum SerialPort::eFlowControl flowControl)
@@ -140,7 +140,6 @@ struct SerialPort_Private
140
140
// ExecuteCloseOperation(oError); //< todo: not sure if still necessary
141
141
}
142
142
}
143
-
144
143
}
145
144
146
145
@@ -248,7 +247,7 @@ static bool isSerialPortPresent(const std::string& deviceName)
248
247
#else
249
248
{
250
249
using namespace boost ;
251
- return ( filesystem::exists (deviceName) && filesystem::is_regular_file (deviceName) );
250
+ return filesystem::exists (deviceName);
252
251
}
253
252
#endif
254
253
}
@@ -276,6 +275,7 @@ SerialPort::~SerialPort() noexcept
276
275
{
277
276
this ->close ();
278
277
m_private.reset ();
278
+ m_params.reset ();
279
279
}
280
280
catch (...)
281
281
{
@@ -287,41 +287,48 @@ SerialPort& SerialPort::operator=(const SerialPort& rhs)
287
287
if (this != &rhs)
288
288
{
289
289
this ->m_private = rhs.m_private ;
290
+ this ->m_params = rhs.m_params ;
290
291
}
291
292
return *this ;
292
293
}
293
294
294
295
void SerialPort::awaitConnection (uint16_t waitMs)
295
296
{
296
- if (isSerialPortPresent ( m_params-> device ) )
297
+ if (nullptr != m_params)
297
298
{
298
- try
299
+ if ( isSerialPortPresent (m_params-> device ))
299
300
{
300
- m_private = std::shared_ptr<SerialPort_Private>(new SerialPort_Private (*m_params));
301
- }
302
- catch (...)
303
- {
304
- throw " Failed to open serial port!" ;
305
- }
301
+ try
302
+ {
303
+ m_private = std::shared_ptr<SerialPort_Private>(new SerialPort_Private (*m_params));
304
+ }
305
+ catch (...)
306
+ {
307
+ throw " Failed to open serial port!" ;
308
+ }
306
309
307
- if (nullptr != m_params->handler )
310
+ if (nullptr != m_params->handler )
311
+ {
312
+ m_params->handler ->onSerialConnected ();
313
+ }
314
+
315
+ std::cout << " Serial port connected" << std::endl;
316
+ }
317
+ else
308
318
{
309
- m_params-> handler -> onSerialConnected ( );
319
+ std::this_thread::sleep_for ( std::chrono::milliseconds (waitMs) );
310
320
}
311
-
312
- std::cout << " Serial port connected" << std::endl;
313
- }
314
- else
315
- {
316
- std::this_thread::sleep_for (std::chrono::milliseconds (waitMs));
317
321
}
318
322
}
319
323
320
324
bool SerialPort::start ()
321
325
{
322
- if (m_private-> m_serialPort . is_open () )
326
+ if (nullptr != m_private)
323
327
{
324
- return m_private->StartReading ();
328
+ if (m_private->m_serialPort .is_open ())
329
+ {
330
+ return m_private->StartReading ();
331
+ }
325
332
}
326
333
327
334
return false ;
@@ -330,39 +337,48 @@ bool SerialPort::start()
330
337
331
338
void SerialPort::setHandler (ISerialHandler* const handler)
332
339
{
333
- m_params->handler = handler;
340
+ if (nullptr != m_params)
341
+ {
342
+ m_params->handler = handler;
343
+ }
334
344
}
335
345
336
346
337
347
bool SerialPort::send (const char cMsg) noexcept
338
348
{
339
349
try
340
350
{
341
- m_private->m_ioService .post (boost::bind (&SerialPort_Private::sendChar, m_private.get (), cMsg));
351
+ if (nullptr != m_private)
352
+ {
353
+ m_private->m_ioService .post (boost::bind (&SerialPort_Private::sendChar, m_private.get (), cMsg));
354
+ return true ;
355
+ }
342
356
}
343
357
catch (...)
344
358
{
345
- return false ;
346
359
}
347
360
348
- return true ;
361
+ return false ;
349
362
}
350
363
351
364
352
365
bool SerialPort::send (const std::string& text)
353
366
{
354
367
try
355
368
{
356
- m_private->m_ioService .post (boost::bind (&SerialPort_Private::sendText,
357
- m_private.get (),
358
- text));
369
+ if (nullptr != m_private)
370
+ {
371
+ m_private->m_ioService .post (boost::bind (&SerialPort_Private::sendText,
372
+ m_private.get (),
373
+ text));
374
+ return true ;
375
+ }
359
376
}
360
377
catch (...)
361
378
{
362
- return false ;
363
379
}
364
380
365
- return true ;
381
+ return false ;
366
382
}
367
383
368
384
@@ -371,40 +387,51 @@ bool SerialPort::send(const uint8_t* const data, size_t length)
371
387
{
372
388
try
373
389
{
374
- m_private->m_ioService .post (boost::bind (&SerialPort_Private::sendBinary,
375
- m_private.get (),
376
- data,
377
- length));
390
+ if (nullptr != m_private)
391
+ {
392
+ m_private->m_ioService .post (boost::bind (&SerialPort_Private::sendBinary,
393
+ m_private.get (),
394
+ data,
395
+ length));
396
+ return true ;
397
+ }
378
398
}
379
399
catch (...)
380
400
{
381
- return false ;
382
401
}
383
402
384
- return true ;
403
+ return false ;
385
404
}
386
405
387
406
388
407
bool SerialPort::close () noexcept
389
408
{
390
409
try
391
410
{
411
+ if (nullptr != m_private)
412
+ {
392
413
m_private->m_ioService .post (boost::bind (&SerialPort_Private::close,
393
414
m_private.get (),
394
415
boost::system::error_code ()));
416
+ return true ;
417
+ }
395
418
}
396
419
catch (...)
397
420
{
398
- return false ;
399
421
}
400
422
401
- return true ;
423
+ return false ;
402
424
}
403
425
404
426
405
427
bool SerialPort::isActive () const
406
428
{
429
+ if (nullptr != m_private)
430
+ {
407
431
return m_private->m_active ;
432
+ }
433
+
434
+ return false ;
408
435
}
409
436
410
437
0 commit comments