22
22
// #include <asm/termios.h> // Terminal control definitions (struct termios)
23
23
#include < asm/ioctls.h>
24
24
#include < asm/termbits.h>
25
+ #include < algorithm>
26
+ #include < iterator>
25
27
26
28
// User includes
27
29
#include " CppLinuxSerial/Exception.hpp"
@@ -275,7 +277,7 @@ namespace CppLinuxSerial {
275
277
// This does no different than STANDARD atm, but let's keep
276
278
// them separate for now....
277
279
else if (baudRateType_ == BaudRateType::CUSTOM)
278
- {
280
+ {
279
281
tty.c_cflag &= ~CBAUD;
280
282
tty.c_cflag |= CBAUDEX;
281
283
// tty.c_cflag |= BOTHER;
@@ -285,7 +287,7 @@ namespace CppLinuxSerial {
285
287
286
288
// #include <linux/serial.h>
287
289
// // configure port to use custom speed instead of 38400
288
- // struct serial_struct ss;
290
+ // struct serial_struct ss;
289
291
// ioctl(fileDesc_, TIOCGSERIAL, &ss);
290
292
// ss.flags = (ss.flags & ~ASYNC_SPD_MASK) | ASYNC_SPD_CUST;
291
293
// ss.custom_divisor = (ss.baud_base + (baudRateCustom_ / 2)) / baudRateCustom_;
@@ -300,7 +302,7 @@ namespace CppLinuxSerial {
300
302
// cfsetispeed(&tty, B38400);
301
303
// cfsetospeed(&tty, B38400);
302
304
}
303
- else
305
+ else
304
306
{
305
307
// Should never get here, bug in this libraries code!
306
308
assert (false );
@@ -352,7 +354,7 @@ namespace CppLinuxSerial {
352
354
tty.c_lflag |= ECHO;
353
355
} else {
354
356
tty.c_lflag &= ~(ECHO);
355
- }
357
+ }
356
358
tty.c_lflag &= ~ECHOE; // Turn off echo erase (echo erase only relevant if canonical input is active)
357
359
tty.c_lflag &= ~ECHONL; //
358
360
tty.c_lflag &= ~ISIG; // Disables recognition of INTR (interrupt), QUIT and SUSP (suspend) characters
@@ -393,6 +395,23 @@ namespace CppLinuxSerial {
393
395
}
394
396
}
395
397
398
+ void SerialPort::WriteBinary (const std::vector<uint8_t >& data) {
399
+
400
+ if (state_ != State::OPEN)
401
+ THROW_EXCEPT (std::string () + __PRETTY_FUNCTION__ + " called but state != OPEN. Please call Open() first." );
402
+
403
+ if (fileDesc_ < 0 ) {
404
+ THROW_EXCEPT (std::string () + __PRETTY_FUNCTION__ + " called but file descriptor < 0, indicating file has not been opened." );
405
+ }
406
+
407
+ int writeResult = write (fileDesc_, data.data (), data.size ());
408
+
409
+ // Check status
410
+ if (writeResult == -1 ) {
411
+ throw std::system_error (EFAULT, std::system_category ());
412
+ }
413
+ }
414
+
396
415
void SerialPort::Read (std::string& data)
397
416
{
398
417
data.clear ();
@@ -431,6 +450,35 @@ namespace CppLinuxSerial {
431
450
// If code reaches here, read must of been successful
432
451
}
433
452
453
+ void SerialPort::ReadBinary (std::vector<uint8_t >& data)
454
+ {
455
+ data.clear ();
456
+
457
+ if (fileDesc_ == 0 ) {
458
+ // this->sp->PrintError(SmartPrint::Ss() << "Read() was called but file descriptor (fileDesc) was 0, indicating file has not been opened.");
459
+ // return false;
460
+ THROW_EXCEPT (" Read() was called but file descriptor (fileDesc) was 0, indicating file has not been opened." );
461
+ }
462
+
463
+ // Read from file
464
+ // We provide the underlying raw array from the readBuffer_ vector to this C api.
465
+ // This will work because we do not delete/resize the vector while this method
466
+ // is called
467
+ ssize_t n = read (fileDesc_, &readBuffer_[0 ], readBufferSize_B_);
468
+
469
+ // Error Handling
470
+ if (n < 0 ) {
471
+ // Read was unsuccessful
472
+ throw std::system_error (EFAULT, std::system_category ());
473
+ }
474
+
475
+ if (n > 0 ) {
476
+ copy (readBuffer_.begin (), readBuffer_.begin () + n, back_inserter (data));
477
+ }
478
+
479
+ // If code reaches here, read must of been successful
480
+ }
481
+
434
482
// termios SerialPort::GetTermios() {
435
483
// if(fileDesc_ == -1)
436
484
// throw std::runtime_error("GetTermios() called but file descriptor was not valid.");
@@ -469,16 +517,16 @@ namespace CppLinuxSerial {
469
517
termios2 SerialPort::GetTermios2 ()
470
518
{
471
519
struct termios2 term2;
472
-
520
+
473
521
ioctl (fileDesc_, TCGETS2, &term2);
474
522
475
523
return term2;
476
-
524
+
477
525
// term2.c_cflag &= ~CBAUD; /* Remove current BAUD rate */
478
526
// term2.c_cflag |= BOTHER; /* Allow custom BAUD rate using int input */
479
527
// term2.c_ispeed = speed; /* Set the input BAUD rate */
480
528
// term2.c_ospeed = speed; /* Set the output BAUD rate */
481
-
529
+
482
530
// ioctl(fd, TCSETS2, &term2);
483
531
}
484
532
0 commit comments