@@ -484,13 +484,7 @@ namespace CppLinuxSerial {
484
484
}
485
485
486
486
void SerialPort::Write (const std::string& data) {
487
-
488
- if (state_ != State::OPEN)
489
- THROW_EXCEPT (std::string () + __PRETTY_FUNCTION__ + " called but state != OPEN. Please call Open() first." );
490
-
491
- if (fileDesc_ < 0 ) {
492
- THROW_EXCEPT (std::string () + __PRETTY_FUNCTION__ + " called but file descriptor < 0, indicating file has not been opened." );
493
- }
487
+ PortIsOpened (__PRETTY_FUNCTION__);
494
488
495
489
int writeResult = write (fileDesc_, data.c_str (), data.size ());
496
490
@@ -501,13 +495,7 @@ namespace CppLinuxSerial {
501
495
}
502
496
503
497
void SerialPort::WriteBinary (const std::vector<uint8_t >& data) {
504
-
505
- if (state_ != State::OPEN)
506
- THROW_EXCEPT (std::string () + __PRETTY_FUNCTION__ + " called but state != OPEN. Please call Open() first." );
507
-
508
- if (fileDesc_ < 0 ) {
509
- THROW_EXCEPT (std::string () + __PRETTY_FUNCTION__ + " called but file descriptor < 0, indicating file has not been opened." );
510
- }
498
+ PortIsOpened (__PRETTY_FUNCTION__);
511
499
512
500
int writeResult = write (fileDesc_, data.data (), data.size ());
513
501
@@ -518,11 +506,7 @@ namespace CppLinuxSerial {
518
506
}
519
507
520
508
void SerialPort::Read (std::string& data) {
521
- if (fileDesc_ == 0 ) {
522
- // this->sp->PrintError(SmartPrint::Ss() << "Read() was called but file descriptor (fileDesc) was 0, indicating file has not been opened.");
523
- // return false;
524
- THROW_EXCEPT (" Read() was called but file descriptor (fileDesc) was 0, indicating file has not been opened." );
525
- }
509
+ PortIsOpened (__PRETTY_FUNCTION__);
526
510
527
511
// Read from file
528
512
// We provide the underlying raw array from the readBuffer_ vector to this C api.
@@ -552,11 +536,7 @@ namespace CppLinuxSerial {
552
536
}
553
537
554
538
void SerialPort::ReadBinary (std::vector<uint8_t >& data) {
555
- if (fileDesc_ == 0 ) {
556
- // this->sp->PrintError(SmartPrint::Ss() << "Read() was called but file descriptor (fileDesc) was 0, indicating file has not been opened.");
557
- // return false;
558
- THROW_EXCEPT (" Read() was called but file descriptor (fileDesc) was 0, indicating file has not been opened." );
559
- }
539
+ PortIsOpened (__PRETTY_FUNCTION__);
560
540
561
541
// Read from file
562
542
// We provide the underlying raw array from the readBuffer_ vector to this C api.
@@ -639,6 +619,17 @@ namespace CppLinuxSerial {
639
619
ioctl (fileDesc_, TCSETS2, &tty);
640
620
}
641
621
622
+ void SerialPort::PortIsOpened (const std::string& prettyFunc) {
623
+
624
+ if (state_ != State::OPEN) {
625
+ THROW_EXCEPT (std::string () + prettyFunc + " called but state != OPEN. Please call Open() first." );
626
+ }
627
+
628
+ if (fileDesc_ < 0 ) {
629
+ THROW_EXCEPT (std::string () + prettyFunc + " called but file descriptor < 0, indicating file has not been opened." );
630
+ }
631
+ }
632
+
642
633
void SerialPort::Close () {
643
634
if (fileDesc_ != -1 ) {
644
635
auto retVal = close (fileDesc_);
@@ -662,8 +653,8 @@ namespace CppLinuxSerial {
662
653
}
663
654
664
655
int32_t SerialPort::Available () {
665
- if (state_ != State::OPEN)
666
- THROW_EXCEPT ( std::string () + __PRETTY_FUNCTION__ + " called but state != OPEN. Please call Open() first. " );
656
+ PortIsOpened (__PRETTY_FUNCTION__);
657
+
667
658
int32_t ret = 0 ;
668
659
ioctl (fileDesc_, FIONREAD, &ret);
669
660
return ret;
0 commit comments