Skip to content

Commit

Permalink
SerialCommHub: silence warning about unused return value of write
Browse files Browse the repository at this point in the history
Ignoring the return code of write results in a compiler warning
as theoretically the write could return without having written
the complete packet. So handle it accordingly.

In case of error, we can throw an exception.

Signed-off-by: Michael Heimpold <[email protected]>
  • Loading branch information
mhei committed Jun 18, 2024
1 parent ba95caf commit a3e5cb4
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion modules/SerialCommHub/tiny_modbus_rtu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <system_error>
#include <type_traits>
#include <unistd.h>

Expand Down Expand Up @@ -480,7 +481,17 @@ std::vector<uint16_t> TinyModbusRTU::txrx_impl(uint8_t device_address, FunctionC

// write to serial port
rxtx_gpio.set(false);
write(fd, req.data(), req.size());

uint8_t* buffer = req.data();
ssize_t written = 0;

while (written < req.size()) {
ssize_t c = write(fd, &buffer[written], req.size() - written);
if (c == -1)
throw std::system_error(errno, std::generic_category(), "Could not send Modbus request");
written += c;
}

if (rxtx_gpio.is_ready()) {
// if we are using GPIO to switch between RX/TX, use the fast version of tcdrain with exact timing
fast_tcdrain(fd);
Expand Down

0 comments on commit a3e5cb4

Please sign in to comment.