Skip to content

Commit c2a33f1

Browse files
committed
✨ feat(serial): Implement readByte function
1 parent 512a48b commit c2a33f1

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

include/libserial/serial.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,13 @@ size_t read(std::shared_ptr<std::string> buffer, size_t max_length);
122122
/**
123123
* @brief Read a single byte from the serial port
124124
*
125-
* Reads one byte from the serial port and stores it in the provided buffer.
125+
* Reads one byte from the serial port.
126126
*
127-
* @param buffer Shared pointer to string where the byte will be stored
128-
* @return Number of bytes actually read (1 if successful, 0 on error)
127+
* @return The byte read from the serial port
129128
* @throws SerialException if read operation fails
130129
* @throws SerialException if buffer is null
131130
*/
132-
void readByte([[maybe_unused]] std::shared_ptr<std::string> buffer);
131+
char readByte();
133132

134133
/**
135134
* @brief Reads data until a specific terminator character is found

src/serial.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,13 @@ size_t Serial::read(std::shared_ptr<std::string> buffer, size_t max_length) {
6969
return static_cast<size_t>(bytes_read);
7070
}
7171

72-
void Serial::readByte([[maybe_unused]] std::shared_ptr<std::string> buffer) {
73-
// Implementation can be added here if needed
72+
char Serial::readByte() {
73+
char byte = 0;
74+
ssize_t bytes_read = ::read(fd_serial_port_, &byte, 1);
75+
if (bytes_read < 0) {
76+
throw SerialException("Error reading from serial port: " + std::string(strerror(errno)));
77+
}
78+
return byte;
7479
}
7580

7681
size_t Serial::readUntil(std::shared_ptr<std::string> buffer, char terminator) {

0 commit comments

Comments
 (0)