#include "port/uart.h" #include #include #include #include #include #include #include #include #include #include #include #include drivers::UartDriver::UartDriver(const std::string& path, int baud) { fd = open(path.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK); if (fd < 0) throw std::runtime_error(std::string("UartDriver open error ") + path + ": " + std::strerror(errno)); struct termios2 tio{}; if (ioctl(fd, TCGETS2, &tio) != 0) { close(fd); throw std::runtime_error(std::string("UartDriver setup error: ") + std::strerror(errno)); } // 8bit tio.c_cflag &= ~CSIZE; tio.c_cflag |= CS8; // baud rate tio.c_ispeed = baud; tio.c_ospeed = baud; // other tio.c_iflag |= (INPCK|IGNBRK|IGNCR|ISTRIP); tio.c_cflag &= ~CBAUD; tio.c_cflag |= (BOTHER|CREAD|CLOCAL); if (ioctl(fd, TCSETS2, &tio) != 0) { close(fd); throw std::runtime_error(std::string("UartDriver setup error: ") + std::strerror(errno)); } events = POLLIN; } bool drivers::UartDriver::writeData(std::span data) { if (fd < 0) return false; auto w = write(fd, data.data(), data.size()); return w == data.size(); } size_t drivers::UartDriver::readChunk(std::vector& out) { if (fd < 0) return 0; out.resize(1024); auto r = read(fd, out.data(), out.size()); if (r <= 0) return 0; out.resize(r); return static_cast(r); } drivers::UartDriver::~UartDriver() { close(fd); }