Skip to content

Commit

Permalink
Add Write(const char* const , size_t) for crayzeewulf#177
Browse files Browse the repository at this point in the history
  • Loading branch information
naga-karupi committed Sep 22, 2023
1 parent ee1992d commit c854a32
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/SerialPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,14 @@ namespace LibSerial
*/
void Write(const std::string& dataString) ;

/**
* @brief Writes a char* to the serial port.
*
* @param dataCharArray The char type array to write to the serial port.
* @param size The size of dataCharArray.
*/
void Write(const char* const dataCharArray, size_t size) ;

/**
* @brief Writes a single byte to the serial port.
* @param charBuffer The byte to be written to the serial port.
Expand Down Expand Up @@ -764,6 +772,12 @@ namespace LibSerial
mImpl->Write(dataString) ;
}

void
SerialPort::Write(const char* const dataCharArray, size_t size)
{
mImpl->Write(dataCharArray, size) ;
}

void
SerialPort::WriteByte(const char charBuffer)
{
Expand Down Expand Up @@ -2657,6 +2671,57 @@ namespace LibSerial
}
}

inline
void
SerialPort::Implementation::Write(const char* const dataCharArray, size_t size)
{
// Throw an exception if the serial port is not open.
if (not this->IsOpen())
{
throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
}

size_t number_of_bytes = size ;

// Nothing needs to be done if there is no data in the string.
if (number_of_bytes <= 0)
{
return ;
}

// Local variables.
size_t number_of_bytes_written = 0 ;
size_t number_of_bytes_remaining = number_of_bytes ;

// Write the data to the serial port. Keep retrying if EAGAIN
// error is received and EWOULDBLOCK is not received.
ssize_t write_result = 0 ;

while (number_of_bytes_remaining > 0)
{
write_result = call_with_retry(write,
this->mFileDescriptor,
&dataCharArray[number_of_bytes_written],
number_of_bytes_remaining) ;

if (write_result >= 0)
{
number_of_bytes_written += write_result ;
number_of_bytes_remaining = number_of_bytes - number_of_bytes_written ;

if (number_of_bytes_remaining == 0)
{
break ;
}
}
else if (write_result <= 0 &&
errno != EWOULDBLOCK)
{
throw std::runtime_error(std::strerror(errno)) ;
}
}
}

inline
void
SerialPort::Implementation::WriteByte(const char charBuffer)
Expand Down
8 changes: 8 additions & 0 deletions src/libserial/SerialPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,14 @@ namespace LibSerial
*/
void Write(const std::string& dataString) ;

/**
* @brief Writes a char* to the serial port.
*
* @param dataCharArray The char type array to write to the serial port.
* @param size The size of dataCharArray.
*/
void Write(const char* const dataCharArray, size_t size) ;

/**
* @brief Writes a single byte to the serial port.
* @param charbuffer The byte to write to the serial port.
Expand Down

0 comments on commit c854a32

Please sign in to comment.