From 5a789e95bb7f0d9f672fa047c37f49b16c460df0 Mon Sep 17 00:00:00 2001 From: wutno Date: Thu, 29 Feb 2024 19:47:43 -0500 Subject: [PATCH] Support other Linux ttys, socat, mkfifo, etc --- CMakeLists.txt | 1 + Includes/SerIo.cpp | 37 ++++++++++++++++++++++--------------- Includes/SerIo.h | 6 +++++- main.cpp | 4 ++-- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 377f81c..85fa3c2 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -70,6 +70,7 @@ include_directories( ${PROJECT_SOURCE_DIR}/3rdparty/mINI/src ${PROJECT_SOURCE_DIR}/3rdparty/gulfs/include ${PROJECT_SOURCE_DIR}/3rdparty/utf8 + ${PROJECT_SOURCE_DIR}/3rdparty/libserialport ${SDL2_INCLUDE_DIRS} ) diff --git a/Includes/SerIo.cpp b/Includes/SerIo.cpp index 7edee9f..0398c9f 100755 --- a/Includes/SerIo.cpp +++ b/Includes/SerIo.cpp @@ -30,13 +30,13 @@ SerIo::SerIo(SerIo::Settings *settings) SerIo::~SerIo() { +#ifdef _WIN32 if (m_isPipe) { -#ifdef _WIN32 CloseHandle(m_pipeHandle); + return; + } #endif - } else { - sp_close(m_portHandle); - } + sp_close(m_portHandle); } bool SerIo::Open() @@ -65,6 +65,16 @@ bool SerIo::Open() sp_get_port_by_name(m_portSettings->devicePath.c_str(), &m_portHandle); if (sp_open(m_portHandle, SP_MODE_READ_WRITE) != SP_OK) { +#ifdef __linux + g_logger->info("SerIo::Init: Failed to open as a serial tty -- attemping to open as regular FD", m_portSettings->devicePath); + m_portHandle = static_cast(std::malloc(sizeof(sp_port))); + m_portHandle->name = const_cast(m_portSettings->devicePath.c_str()); + m_portHandle->fd = open(m_portSettings->devicePath.c_str(), O_RDWR | O_NOCTTY | O_SYNC | O_NDELAY); + if (m_portHandle->fd > 0) + return true; + else + std::free(m_portHandle); +#endif g_logger->critical("SerIo::Init: Failed to open {}", m_portSettings->devicePath); return false; } @@ -104,16 +114,15 @@ SerIo::Status SerIo::Write() int ret = 0; - if (m_isPipe) { #ifdef _WIN32 + if (m_isPipe) { DWORD dwRet = 0; WriteFile(m_pipeHandle, &m_writeBuffer[0], static_cast(m_writeBuffer.size()), &dwRet, NULL); ret = dwRet; + } else #endif - } else { - ret = sp_blocking_write(m_portHandle, &m_writeBuffer[0], m_writeBuffer.size(), 0); - sp_drain(m_portHandle); - } + ret = sp_blocking_write(m_portHandle, &m_writeBuffer[0], m_writeBuffer.size(), 0); + sp_drain(m_portHandle); if (ret > 0) { if (ret < static_cast(m_writeBuffer.size())) { @@ -132,8 +141,8 @@ SerIo::Status SerIo::Read() { int bytes = 0; - if (m_isPipe) { #ifdef _WIN32 + if (m_isPipe) { DWORD dwBytes = 0; if (PeekNamedPipe(m_pipeHandle, 0, 0, 0, &dwBytes, 0) == 0) { DWORD error = ::GetLastError(); @@ -145,10 +154,9 @@ SerIo::Status SerIo::Read() return Status::ReadError; } bytes = dwBytes; + } else #endif - } else { bytes = sp_input_waiting(m_portHandle); - } if (bytes < 1) { return Status::ReadError; @@ -159,15 +167,14 @@ SerIo::Status SerIo::Read() int ret = 0; - if (m_isPipe) { #ifdef _WIN32 + if (m_isPipe) { DWORD dwRet = 0; BOOL bRet = ReadFile(m_pipeHandle, &m_readBuffer[originalSize], static_cast(bytes), &dwRet, NULL); ret = bRet; + } else #endif - } else { ret = sp_nonblocking_read(m_portHandle, &m_readBuffer[originalSize], static_cast(bytes)); - } if (ret <= 0) { return Status::ReadError; diff --git a/Includes/SerIo.h b/Includes/SerIo.h index 0bb43b1..282e43b 100755 --- a/Includes/SerIo.h +++ b/Includes/SerIo.h @@ -24,6 +24,11 @@ #ifdef _WIN32 #include +#else +#define SP_PRIV +#include "libserialport_internal.h" +#include +#include #endif #include @@ -69,7 +74,6 @@ class SerIo #ifdef _WIN32 HANDLE m_pipeHandle = INVALID_HANDLE_VALUE; #endif - bool m_isPipe = false; sp_port *m_portHandle = nullptr; sp_port_config *m_portConfig = nullptr; diff --git a/main.cpp b/main.cpp index d2dc8d3..5fb3346 100755 --- a/main.cpp +++ b/main.cpp @@ -41,8 +41,8 @@ // Globals struct Settings { - CardIo::Settings card{}; - SerIo::Settings serial{}; + CardIo::Settings card = {}; + SerIo::Settings serial = {}; std::string webListenHost; int webPort = 8080; };