Skip to content

Commit

Permalink
Merge pull request #41 from meetgandhi-eic/master
Browse files Browse the repository at this point in the history
Added proper error handling
  • Loading branch information
gbmhunter authored Jul 22, 2023
2 parents 4039faa + 0850bda commit ae3c8f7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
5 changes: 5 additions & 0 deletions include/CppLinuxSerial/SerialPort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ namespace mn {
/// \brief Assigns the provided tty settings to the serial port pointed to by the file descriptor.
void SetTermios2(termios2 tty);

/// \brief checks whether the port is open or not
/// \param prettyFunc Function details obtained using __PRETTY_FUNCTION__
/// \throws CppLinuxSerial::Exception if port is not opened
void PortIsOpened(const std::string& prettyFunc);

/// \brief Keeps track of the serial port's state.
State state_;

Expand Down
43 changes: 17 additions & 26 deletions src/SerialPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,13 +484,7 @@ namespace CppLinuxSerial {
}

void SerialPort::Write(const std::string& data) {

if(state_ != State::OPEN)
THROW_EXCEPT(std::string() + __PRETTY_FUNCTION__ + " called but state != OPEN. Please call Open() first.");

if(fileDesc_ < 0) {
THROW_EXCEPT(std::string() + __PRETTY_FUNCTION__ + " called but file descriptor < 0, indicating file has not been opened.");
}
PortIsOpened(__PRETTY_FUNCTION__);

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

Expand All @@ -501,13 +495,7 @@ namespace CppLinuxSerial {
}

void SerialPort::WriteBinary(const std::vector<uint8_t>& data) {

if(state_ != State::OPEN)
THROW_EXCEPT(std::string() + __PRETTY_FUNCTION__ + " called but state != OPEN. Please call Open() first.");

if(fileDesc_ < 0) {
THROW_EXCEPT(std::string() + __PRETTY_FUNCTION__ + " called but file descriptor < 0, indicating file has not been opened.");
}
PortIsOpened(__PRETTY_FUNCTION__);

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

Expand All @@ -518,11 +506,7 @@ namespace CppLinuxSerial {
}

void SerialPort::Read(std::string& data) {
if(fileDesc_ == 0) {
//this->sp->PrintError(SmartPrint::Ss() << "Read() was called but file descriptor (fileDesc) was 0, indicating file has not been opened.");
//return false;
THROW_EXCEPT("Read() was called but file descriptor (fileDesc) was 0, indicating file has not been opened.");
}
PortIsOpened(__PRETTY_FUNCTION__);

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

void SerialPort::ReadBinary(std::vector<uint8_t>& data) {
if(fileDesc_ == 0) {
//this->sp->PrintError(SmartPrint::Ss() << "Read() was called but file descriptor (fileDesc) was 0, indicating file has not been opened.");
//return false;
THROW_EXCEPT("Read() was called but file descriptor (fileDesc) was 0, indicating file has not been opened.");
}
PortIsOpened(__PRETTY_FUNCTION__);

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

void SerialPort::PortIsOpened(const std::string& prettyFunc) {

if(state_ != State::OPEN) {
THROW_EXCEPT(std::string() + prettyFunc + " called but state != OPEN. Please call Open() first.");
}

if(fileDesc_ < 0) {
THROW_EXCEPT(std::string() + prettyFunc + " called but file descriptor < 0, indicating file has not been opened.");
}
}

void SerialPort::Close() {
if(fileDesc_ != -1) {
auto retVal = close(fileDesc_);
Expand All @@ -662,8 +653,8 @@ namespace CppLinuxSerial {
}

int32_t SerialPort::Available() {
if(state_ != State::OPEN)
THROW_EXCEPT(std::string() + __PRETTY_FUNCTION__ + " called but state != OPEN. Please call Open() first.");
PortIsOpened(__PRETTY_FUNCTION__);

int32_t ret = 0;
ioctl(fileDesc_, FIONREAD, &ret);
return ret;
Expand Down

0 comments on commit ae3c8f7

Please sign in to comment.