Compare commits
No commits in common. "5de7877d74fded76954df070ca4c539113f487d8" and "e3b40bfc73627073cde4184833d3bd8fb60475af" have entirely different histories.
5de7877d74
...
e3b40bfc73
|
@ -1,15 +0,0 @@
|
||||||
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")
|
|
||||||
|
|
||||||
set(CMAKE_INSTALL_PREFIX /usr)
|
|
||||||
install(TARGETS serialterminal RUNTIME DESTINATION bin)
|
|
194
main.cpp
194
main.cpp
|
@ -1,194 +0,0 @@
|
||||||
#include <bits/types/sig_atomic_t.h>
|
|
||||||
#include <chrono>
|
|
||||||
#include <iostream>
|
|
||||||
#include <string.h>
|
|
||||||
#include <string>
|
|
||||||
#include <thread>
|
|
||||||
#include <readline/readline.h>
|
|
||||||
#include <readline/history.h>
|
|
||||||
#include <signal.h>
|
|
||||||
#include <sys/signalfd.h>
|
|
||||||
#include <sys/epoll.h>
|
|
||||||
|
|
||||||
#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 exitfd)
|
|
||||||
{
|
|
||||||
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 = exitfd;
|
|
||||||
epoll_ctl(pollQue, EPOLL_CTL_ADD, exitfd, &ev);
|
|
||||||
|
|
||||||
struct epoll_event event;
|
|
||||||
int ret;
|
|
||||||
while((ret = epoll_wait(pollQue, &event, 1, -1)) > 0)
|
|
||||||
{
|
|
||||||
if(event.data.fd == exitfd)
|
|
||||||
break;
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
||||||
char buffer[4096];
|
|
||||||
int readlen = sRead(serial, buffer, 4095);
|
|
||||||
if(readlen > 0)
|
|
||||||
{
|
|
||||||
buffer[readlen] = '\0';
|
|
||||||
rl_clear_visible_line();
|
|
||||||
std::cout<<buffer;
|
|
||||||
if(buffer[readlen-1] != '\n')
|
|
||||||
std::cout<<'\n';
|
|
||||||
rl_on_new_line();
|
|
||||||
rl_redisplay();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(ret < 0)
|
|
||||||
perror("epoll_wait: ");
|
|
||||||
close(pollQue);
|
|
||||||
}
|
|
||||||
|
|
||||||
int cb_serial;
|
|
||||||
static void cb_linehandler(char *line)
|
|
||||||
{
|
|
||||||
if(line != NULL)
|
|
||||||
{
|
|
||||||
if(*line)
|
|
||||||
{
|
|
||||||
add_history(line);
|
|
||||||
sWrite(cb_serial, line, strlen(line));
|
|
||||||
sWrite(cb_serial, "\n", 1);
|
|
||||||
}
|
|
||||||
free(line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
|
||||||
{
|
|
||||||
Config config;
|
|
||||||
|
|
||||||
if(parseCmdArgs(argc, argv, &config) != 0)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
std::cout<<"UVOS serial terminal v1.1\n";
|
|
||||||
|
|
||||||
int serial = -1;
|
|
||||||
std::cout<<"Using serial port: "<<config.portFileName<<" at "<<config.baud<<" baud\n";
|
|
||||||
serial = serialport_init(config.portFileName.c_str(), config.baud);
|
|
||||||
if(serial == -1)
|
|
||||||
return 1;
|
|
||||||
cb_serial = serial;
|
|
||||||
|
|
||||||
tcflush(serial, TCIOFLUSH);
|
|
||||||
|
|
||||||
sigset_t set;
|
|
||||||
sigisemptyset(&set);
|
|
||||||
sigaddset(&set, SIGINT);
|
|
||||||
sigaddset(&set, SIGTERM);
|
|
||||||
int sigfd = signalfd(-1, &set, 0);
|
|
||||||
signal(SIGINT, intHandler);
|
|
||||||
signal(SIGTERM, intHandler);
|
|
||||||
signal(SIGPIPE, SIG_IGN);
|
|
||||||
|
|
||||||
int exitPipe[2];
|
|
||||||
int ret = pipe(exitPipe);
|
|
||||||
if(ret)
|
|
||||||
{
|
|
||||||
std::cout<<"Could not create pipe\n";
|
|
||||||
return 3;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::thread readThread(recvThreadFunction, serial, exitPipe[0]);
|
|
||||||
|
|
||||||
rl_catch_signals = 0;
|
|
||||||
rl_callback_handler_install ("> ", 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;
|
|
||||||
while((ret = epoll_wait(pollQue, &event, 1, -1)) > 0)
|
|
||||||
{
|
|
||||||
if(event.data.fd == sigfd)
|
|
||||||
{
|
|
||||||
write(exitPipe[1], "\n", 1);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
rl_callback_read_char();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
readThread.join();
|
|
||||||
close(pollQue);
|
|
||||||
close(sigfd);
|
|
||||||
rl_callback_handler_remove();
|
|
||||||
std::cout<<std::endl;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
105
serial_io.cpp
105
serial_io.cpp
|
@ -1,105 +0,0 @@
|
||||||
#include "serial_io.h"
|
|
||||||
|
|
||||||
void sWrite(int port, char string[], size_t length)
|
|
||||||
{
|
|
||||||
if(port != -1) write(port, string, length);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void sWrite(int port, const char string[], size_t length)
|
|
||||||
{
|
|
||||||
if(port != -1) write(port, string, length);
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t sRead(int port, void *buf, size_t count)
|
|
||||||
{
|
|
||||||
return (port != -1) ? read(port, buf, count) : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
void printRates()
|
|
||||||
{
|
|
||||||
std::cout<<"Rates:\n"\
|
|
||||||
<<"Unchanged 0\n" \
|
|
||||||
<<"B50 "<<B50<<'\n'\
|
|
||||||
<<"B75 "<<B75<<'\n'\
|
|
||||||
<<"B110 "<<B110<<'\n'\
|
|
||||||
<<"B134 "<<B134<<'\n'\
|
|
||||||
<<"B150 "<<B150<<'\n'\
|
|
||||||
<<"B200 "<<B200<<'\n'\
|
|
||||||
<<"B300 "<<B300<<'\n'\
|
|
||||||
<<"B600 "<<B600<<'\n'\
|
|
||||||
<<"B1200 "<<B1200<<'\n'\
|
|
||||||
<<"B1800 "<<B1800<<'\n'\
|
|
||||||
<<"B2400 "<<B2400<<'\n'\
|
|
||||||
<<"B4800 "<<B4800<<'\n'\
|
|
||||||
<<"B9600 "<<B9600<<'\n'\
|
|
||||||
<<"B19200 "<<B19200<<'\n'\
|
|
||||||
<<"B38400 "<<B38400<<'\n'\
|
|
||||||
<<"B57600 "<<B57600<<'\n'\
|
|
||||||
<<"B115200 "<<B115200<<'\n'\
|
|
||||||
<<"B230400 "<<B230400<<'\n'\
|
|
||||||
<<"B460800 "<<B460800<<'\n'\
|
|
||||||
<<"B500000 "<<B500000<<'\n'\
|
|
||||||
<<"B576000 "<<B576000<<'\n'\
|
|
||||||
<<"B921600 "<<B921600<<'\n'\
|
|
||||||
<<"B1000000 "<<B1000000<<'\n'\
|
|
||||||
<<"B1152000 "<<B1152000<<'\n'\
|
|
||||||
<<"B1500000 "<<B1500000<<'\n';
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int serialport_init(const char* device, int baud)
|
|
||||||
{
|
|
||||||
int fd;
|
|
||||||
struct termios toptions;
|
|
||||||
fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
|
|
||||||
if (fd == -1)
|
|
||||||
{
|
|
||||||
perror("init_serialport: Unable to open port ");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tcgetattr(fd, &toptions) < 0)
|
|
||||||
{
|
|
||||||
perror("init_serialport: Couldn't get term attributes");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
// 8N1
|
|
||||||
toptions.c_cflag &= ~PARENB;
|
|
||||||
toptions.c_cflag &= ~CSTOPB;
|
|
||||||
toptions.c_cflag &= ~CSIZE;
|
|
||||||
toptions.c_cflag |= CS8;
|
|
||||||
|
|
||||||
// no flow control
|
|
||||||
toptions.c_cflag &= ~(CRTSCTS | CLOCAL);
|
|
||||||
|
|
||||||
//Make Raw
|
|
||||||
toptions.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
|
|
||||||
toptions.c_oflag &= ~OPOST; //hmm examine
|
|
||||||
toptions.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
|
|
||||||
toptions.c_cflag &= ~(CSIZE | PARENB);
|
|
||||||
toptions.c_cflag |= CS8;
|
|
||||||
|
|
||||||
if(baud != 0)
|
|
||||||
{
|
|
||||||
int error = cfsetispeed(&toptions, baud) | cfsetospeed(&toptions, baud);
|
|
||||||
|
|
||||||
if(error)
|
|
||||||
{
|
|
||||||
perror("init_serialport: Couldn't set baud rate");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// see: http://unixwiz.net/techtips/termios-vmin-vtime.html
|
|
||||||
toptions.c_cc[VMIN] = 0;
|
|
||||||
toptions.c_cc[VTIME] = 40;
|
|
||||||
fcntl(fd, F_SETFL, FNDELAY);
|
|
||||||
if( tcsetattr(fd, TCSANOW, &toptions) < 0)
|
|
||||||
{
|
|
||||||
perror("init_serialport: Couldn't set term attributes");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return fd;
|
|
||||||
}
|
|
||||||
|
|
26
serial_io.h
26
serial_io.h
|
@ -1,26 +0,0 @@
|
||||||
#ifndef SERIAL_H
|
|
||||||
#define SERIAL_H
|
|
||||||
#include <termios.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
#include <iostream>
|
|
||||||
#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
|
|
Loading…
Reference in a new issue