Skip to content

Commit ae3c8f7

Browse files
authored
Merge pull request #41 from meetgandhi-eic/master
Added proper error handling
2 parents 4039faa + 0850bda commit ae3c8f7

File tree

2 files changed

+22
-26
lines changed

2 files changed

+22
-26
lines changed

include/CppLinuxSerial/SerialPort.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ namespace mn {
209209
/// \brief Assigns the provided tty settings to the serial port pointed to by the file descriptor.
210210
void SetTermios2(termios2 tty);
211211

212+
/// \brief checks whether the port is open or not
213+
/// \param prettyFunc Function details obtained using __PRETTY_FUNCTION__
214+
/// \throws CppLinuxSerial::Exception if port is not opened
215+
void PortIsOpened(const std::string& prettyFunc);
216+
212217
/// \brief Keeps track of the serial port's state.
213218
State state_;
214219

src/SerialPort.cpp

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -484,13 +484,7 @@ namespace CppLinuxSerial {
484484
}
485485

486486
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__);
494488

495489
int writeResult = write(fileDesc_, data.c_str(), data.size());
496490

@@ -501,13 +495,7 @@ namespace CppLinuxSerial {
501495
}
502496

503497
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__);
511499

512500
int writeResult = write(fileDesc_, data.data(), data.size());
513501

@@ -518,11 +506,7 @@ namespace CppLinuxSerial {
518506
}
519507

520508
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__);
526510

527511
// Read from file
528512
// We provide the underlying raw array from the readBuffer_ vector to this C api.
@@ -552,11 +536,7 @@ namespace CppLinuxSerial {
552536
}
553537

554538
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__);
560540

561541
// Read from file
562542
// We provide the underlying raw array from the readBuffer_ vector to this C api.
@@ -639,6 +619,17 @@ namespace CppLinuxSerial {
639619
ioctl(fileDesc_, TCSETS2, &tty);
640620
}
641621

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+
642633
void SerialPort::Close() {
643634
if(fileDesc_ != -1) {
644635
auto retVal = close(fileDesc_);
@@ -662,8 +653,8 @@ namespace CppLinuxSerial {
662653
}
663654

664655
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+
667658
int32_t ret = 0;
668659
ioctl(fileDesc_, FIONREAD, &ret);
669660
return ret;

0 commit comments

Comments
 (0)