From 4a686bb4c364e73e25bd6054724c2e4fa204cefb Mon Sep 17 00:00:00 2001 From: uvos Date: Wed, 21 Dec 2022 23:34:48 +0100 Subject: [PATCH] inital verison --- CMakeLists.txt | 14 ++++ main.cpp | 179 +++++++++++++++++++++++++++++++++++++++++++++++++ serial_io.cpp | 105 +++++++++++++++++++++++++++++ serial_io.h | 26 +++++++ 4 files changed, 324 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 main.cpp create mode 100644 serial_io.cpp create mode 100644 serial_io.h diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..b859ce7 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.0) + +project(serialterminal) + +set(SRC_FILES main.cpp serial_io.cpp ) +set(LIBS -pthread -lreadline) + +add_executable(${PROJECT_NAME} ${SRC_FILES}) + +target_link_libraries( ${PROJECT_NAME} ${LIBS}) +set_target_properties( ${PROJECT_NAME} PROPERTIES COMPILE_FLAGS -m64 LINK_FLAGS -m64) +add_definitions(" -std=c++11 -Wall ") + +install(TARGETS serialterminal RUNTIME DESTINATION bin) diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..6cd85b3 --- /dev/null +++ b/main.cpp @@ -0,0 +1,179 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "serial_io.h" + +sig_atomic_t stop = false; +constexpr int HISTORY_SIZE = 100; + +void intHandler(int dummy) +{ + stop = true; +} + +static void printUsage() +{ + std::cout<<"usage mulitplexer [option]\n\ + Available options:\n\ + -h, --help print this help\n\ + -p, --serialport serial port device to use\n\ + -P, --port tcp port to use\n\ + -b, --baud set baud rate with termios id\n\ + -r, --rates list Available baud rates\n\ + -s, --sinkless run without serial port\n"; +} + + +struct Config +{ + std::string portFileName = "/dev/ttyUSB0"; + unsigned short port = 6856; + int baud = B9600; + bool noSerial = false; +}; + +static int parseCmdArgs(int argc, char** argv, Config *config) +{ + for (int i = 1; i < argc; i++) + { + if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") + { + printUsage(); + return -1; + } + else if (std::string(argv[i]) == "--serialport" || std::string(argv[i]) == "-p") + { + if(argc > i) config->portFileName = argv[i+1]; + else return -1; + } + else if (std::string(argv[i]) == "--baud" || std::string(argv[i]) == "-b") + { + if(argc > i) config->baud = atoi(argv[i+1]); + else return -1; + } + else if (std::string(argv[i]) == "-r" || std::string(argv[i]) == "--rates") + { + printRates(); + return -1; + } + } + return 0; +} + +void recvThreadFunction(int serial, int signal) +{ + int pollQue = epoll_create1(0); + struct epoll_event ev = {}; + + ev.events = EPOLLIN; + ev.data.fd = serial; + epoll_ctl(pollQue, EPOLL_CTL_ADD, serial, &ev); + ev.events = EPOLLIN; + ev.data.fd = signal; + epoll_ctl(pollQue, EPOLL_CTL_ADD, signal, &ev); + + struct epoll_event event; + int ret; + while((ret = epoll_wait(pollQue, &event, 1, -1)) > 0) + { + if(event.data.fd == signal) + break; + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + char buffer[4096]; + int readlen = sRead(serial, buffer, 4096); + if(readlen > 0) + { + rl_clear_visible_line(); + std::cout< ", cb_linehandler); + + int pollQue = epoll_create1(0); + struct epoll_event ev = {}; + ev.events = EPOLLIN; + ev.data.fd = fileno(rl_instream); + epoll_ctl(pollQue, EPOLL_CTL_ADD, fileno(rl_instream), &ev); + ev.events = EPOLLIN; + ev.data.fd = sigfd; + epoll_ctl(pollQue, EPOLL_CTL_ADD, sigfd, &ev); + + struct epoll_event event; + int ret; + while((ret = epoll_wait(pollQue, &event, 1, -1)) > 0) + { + if(event.data.fd == sigfd) + break; + else + rl_callback_read_char(); + } + + readThread.join(); + close(pollQue); + close(sigfd); + std::cout< +#include +#include + +#ifdef __cplusplus +#include +#endif + +#define BAUDRATE B38400 + +void sWrite(int port, char string[], size_t length); + +void sWrite(int port, const char string[], size_t length); + +ssize_t sRead(int port, void *buf, size_t count); + +#ifdef __cplusplus +void printRates(); +#endif + +int serialport_init(const char* device, int baud = BAUDRATE); + + +#endif // SERIAL_H