From 2dfeefd7b817e3b6ebe58c88d06e912afc0f9df9 Mon Sep 17 00:00:00 2001 From: Eric Katzfey <53063038+katzfey@users.noreply.github.com> Date: Tue, 9 Jun 2026 09:58:14 -0700 Subject: [PATCH] refactor(drivers/distance_sensor): use Serial in Lightware laser driver (#27619) --- .../lightware_laser_serial.cpp | 64 ++++++------------- .../lightware_laser_serial.hpp | 3 +- 2 files changed, 22 insertions(+), 45 deletions(-) diff --git a/src/drivers/distance_sensor/lightware_laser_serial/lightware_laser_serial.cpp b/src/drivers/distance_sensor/lightware_laser_serial/lightware_laser_serial.cpp index 648d5f649f9..7bba405adb5 100644 --- a/src/drivers/distance_sensor/lightware_laser_serial/lightware_laser_serial.cpp +++ b/src/drivers/distance_sensor/lightware_laser_serial/lightware_laser_serial.cpp @@ -33,12 +33,14 @@ #include "lightware_laser_serial.hpp" +#include #include -#include -#include +#include +#include /* Configuration Constants */ #define LW_TAKE_RANGE_REG 'd' +static constexpr uint32_t LW_READ_TIMEOUT_MS {10}; LightwareLaserSerial::LightwareLaserSerial(const char *port, uint8_t rotation) : ScheduledWorkItem(MODULE_NAME, px4::serial_port_to_wq(port)), @@ -148,7 +150,7 @@ int LightwareLaserSerial::measure() { // Send the command to begin a measurement. char cmd = LW_TAKE_RANGE_REG; - int ret = ::write(_fd, &cmd, 1); + int ret = _uart.write(&cmd, sizeof(cmd)); if (ret != sizeof(cmd)) { perf_count(_comms_errors); @@ -172,7 +174,7 @@ int LightwareLaserSerial::collect() /* read from the sensor (uart buffer) */ const hrt_abstime timestamp_sample = hrt_absolute_time(); - int ret = ::read(_fd, &readbuf[0], readlen); + int ret = _uart.readAtLeast(reinterpret_cast(&readbuf[0]), readlen, 1, LW_READ_TIMEOUT_MS); if (ret < 0) { PX4_DEBUG("read err: %d", ret); @@ -188,7 +190,7 @@ int LightwareLaserSerial::collect() } } else if (ret == 0) { - return -EAGAIN; + return -ETIMEDOUT; } _last_read = hrt_absolute_time(); @@ -256,64 +258,38 @@ void LightwareLaserSerial::start() void LightwareLaserSerial::stop() { ScheduleClear(); + _uart.close(); } void LightwareLaserSerial::Run() { - /* fds initialized? */ - if (_fd < 0) { - /* open fd */ - _fd = ::open(_port, O_RDWR | O_NOCTTY | O_NONBLOCK); - - if (_fd < 0) { - PX4_ERR("open failed (%i)", errno); + if (!_uart.isOpen()) { + if (!_uart.setPort(_port)) { + PX4_ERR("failed to configure port %s", _port); return; } - struct termios uart_config; - - int termios_state; - - /* fill the struct for the new configuration */ - tcgetattr(_fd, &uart_config); - - /* clear ONLCR flag (which appends a CR for every LF) */ - uart_config.c_oflag &= ~ONLCR; - - /* no parity, one stop bit */ - uart_config.c_cflag &= ~(CSTOPB | PARENB); - - /* if distance sensor model is SF11/C, then set baudrate 115200, else 9600 */ int32_t hw_model = 0; param_get(param_find("SENS_EN_SF0X"), &hw_model); - unsigned speed; + const uint32_t baudrate = (hw_model >= 5) ? 115200 : 9600; - if (hw_model >= 5) { - speed = B115200; - - } else { - speed = B9600; + if (!_uart.setBaudrate(baudrate)) { + PX4_ERR("failed to set baudrate %" PRIu32 " on %s", baudrate, _port); + return; } - /* set baud rate */ - if ((termios_state = cfsetispeed(&uart_config, speed)) < 0) { - PX4_ERR("CFG: %d ISPD", termios_state); - } - - if ((termios_state = cfsetospeed(&uart_config, speed)) < 0) { - PX4_ERR("CFG: %d OSPD", termios_state); - } - - if ((termios_state = tcsetattr(_fd, TCSANOW, &uart_config)) < 0) { - PX4_ERR("baud %d ATTR", termios_state); + if (!_uart.open()) { + PX4_ERR("failed to open %s", _port); + _uart.close(); + return; } // LW20: Enable serial mode by sending some characters if (hw_model == 8) { const char *data = "www\r\n"; - (void)!::write(_fd, data, strlen(data)); + (void)_uart.write(data, strlen(data)); } } diff --git a/src/drivers/distance_sensor/lightware_laser_serial/lightware_laser_serial.hpp b/src/drivers/distance_sensor/lightware_laser_serial/lightware_laser_serial.hpp index 6b1229d1a1e..513ab7e98c0 100644 --- a/src/drivers/distance_sensor/lightware_laser_serial/lightware_laser_serial.hpp +++ b/src/drivers/distance_sensor/lightware_laser_serial/lightware_laser_serial.hpp @@ -42,6 +42,7 @@ #pragma once #include +#include #include #include #include @@ -69,11 +70,11 @@ private: int collect(); PX4Rangefinder _px4_rangefinder; + device::Serial _uart; char _port[20] {}; int _interval{100000}; bool _collect_phase{false}; - int _fd{-1}; char _linebuf[10] {}; unsigned _linebuf_index{0}; enum LW_PARSE_STATE _parse_state {LW_PARSE_STATE0_UNSYNC};