From c854a3225e2e66211c6de699751016dd13ad632f Mon Sep 17 00:00:00 2001 From: naga Date: Sat, 23 Sep 2023 01:31:27 +0900 Subject: [PATCH] Add Write(const char* const , size_t) for #177 --- src/SerialPort.cpp | 65 ++++++++++++++++++++++++++++++++++++++ src/libserial/SerialPort.h | 8 +++++ 2 files changed, 73 insertions(+) diff --git a/src/SerialPort.cpp b/src/SerialPort.cpp index 4b77912..d6bd144 100644 --- a/src/SerialPort.cpp +++ b/src/SerialPort.cpp @@ -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. @@ -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) { @@ -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) diff --git a/src/libserial/SerialPort.h b/src/libserial/SerialPort.h index 09ed2bd..a7b38fb 100644 --- a/src/libserial/SerialPort.h +++ b/src/libserial/SerialPort.h @@ -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.