Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Write(const char* const , size_t) for #177 #187

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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