mirror of
https://github.com/eddyem/snippets_library.git
synced 2026-03-20 00:30:56 +03:00
fixed bug with termio/termios
This commit is contained in:
@@ -29,15 +29,15 @@
|
||||
/*
|
||||
* here are global parameters initialisation
|
||||
*/
|
||||
int help;
|
||||
glob_pars G;
|
||||
static int help;
|
||||
static glob_pars G;
|
||||
|
||||
// default PID filename:
|
||||
#define DEFAULT_PIDFILE "/tmp/testcmdlnopts.pid"
|
||||
|
||||
// DEFAULTS
|
||||
// default global parameters
|
||||
glob_pars const Gdefault = {
|
||||
static glob_pars const Gdefault = {
|
||||
.device = NULL,
|
||||
.pidfile = DEFAULT_PIDFILE,
|
||||
.speed = 9600,
|
||||
@@ -48,13 +48,14 @@ glob_pars const Gdefault = {
|
||||
* Define command line options by filling structure:
|
||||
* name has_arg flag val type argptr help
|
||||
*/
|
||||
myoption cmdlnopts[] = {
|
||||
static myoption cmdlnopts[] = {
|
||||
// common options
|
||||
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&help), _("show this help")},
|
||||
{"device", NEED_ARG, NULL, 'd', arg_string, APTR(&G.device), _("serial device name")},
|
||||
{"speed", NEED_ARG, NULL, 's', arg_int, APTR(&G.speed), _("serial device speed (default: 9600)")},
|
||||
{"logfile", NEED_ARG, NULL, 'l', arg_string, APTR(&G.logfile), _("file to save logs")},
|
||||
{"pidfile", NEED_ARG, NULL, 'P', arg_string, APTR(&G.pidfile), _("pidfile (default: " DEFAULT_PIDFILE ")")},
|
||||
{"exclusive",NO_ARGS, NULL, 'e', arg_int, APTR(&G.exclusive), _("open serial device exclusively")},
|
||||
end_option
|
||||
};
|
||||
|
||||
|
||||
@@ -20,8 +20,8 @@
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#ifndef __CMDLNOPTS_H__
|
||||
#define __CMDLNOPTS_H__
|
||||
#ifndef CMDLNOPTS_H__
|
||||
#define CMDLNOPTS_H__
|
||||
|
||||
/*
|
||||
* here are some typedef's for global data
|
||||
@@ -32,9 +32,11 @@ typedef struct{
|
||||
char *logfile; // logging to this file
|
||||
int speed; // connection speed
|
||||
int rest_pars_num; // number of rest parameters
|
||||
int exclusive; // exclusive open port
|
||||
char** rest_pars; // the rest parameters: array of char*
|
||||
} glob_pars;
|
||||
|
||||
|
||||
glob_pars *parse_args(int argc, char **argv);
|
||||
#endif // __CMDLNOPTS_H__
|
||||
|
||||
#endif // CMDLNOPTS_H__
|
||||
|
||||
@@ -18,6 +18,10 @@
|
||||
#include <usefull_macros.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/*
|
||||
* Example of FIFO/LIFO usage
|
||||
*/
|
||||
|
||||
int main(int argc, char *argv[argc]) {
|
||||
List *f = NULL;
|
||||
printf("Available memory: %luMB\n", get_available_mem()/1024/1024);
|
||||
|
||||
@@ -16,13 +16,27 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "cmdlnopts.h"
|
||||
#include <signal.h> // signal
|
||||
#include <stdlib.h> // exit, free
|
||||
#include <stdio.h> // printf
|
||||
#include <stdlib.h> // exit, free
|
||||
#include <string.h> // strdup
|
||||
#include <unistd.h> // sleep
|
||||
#include <usefull_macros.h>
|
||||
#include "cmdlnopts.h"
|
||||
|
||||
|
||||
#include <termios.h> // tcsetattr
|
||||
#include <unistd.h> // tcsetattr, close, read, write
|
||||
#include <sys/ioctl.h> // ioctl
|
||||
#include <stdio.h> // printf, getchar, fopen, perror
|
||||
#include <stdlib.h> // exit
|
||||
#include <sys/stat.h> // read
|
||||
#include <fcntl.h> // read
|
||||
#include <signal.h> // signal
|
||||
#include <time.h> // time
|
||||
#include <string.h> // memcpy
|
||||
#include <stdint.h> // int types
|
||||
#include <sys/time.h> // gettimeofday
|
||||
|
||||
/**
|
||||
* This is an example of usage:
|
||||
@@ -34,8 +48,8 @@
|
||||
* The `cmdlnopts.[hc]` are intrinsic files of this demo.
|
||||
*/
|
||||
|
||||
TTY_descr *dev = NULL; // shoul be global to restore if die
|
||||
glob_pars *GP = NULL; // for GP->pidfile need in `signals`
|
||||
static TTY_descr *dev = NULL; // shoul be global to restore if die
|
||||
static glob_pars *GP = NULL; // for GP->pidfile need in `signals`
|
||||
|
||||
/**
|
||||
* We REDEFINE the default WEAK function of signal processing
|
||||
@@ -82,23 +96,31 @@ int main(int argc, char *argv[]){
|
||||
}
|
||||
if(GP->device){
|
||||
putlog("Try to open serial %s", GP->device);
|
||||
dev = tty_open(GP->device, GP->speed, 256);
|
||||
dev = new_tty(GP->device, GP->speed, 256);
|
||||
if(dev) dev = tty_open(dev, GP->exclusive);
|
||||
if(!dev){
|
||||
putlog("Can't open %s with speed %d. Exit.", GP->device, GP->speed);
|
||||
signals(0);
|
||||
}
|
||||
}
|
||||
|
||||
// main stuff goes here
|
||||
long seed = throw_random_seed();
|
||||
green("Now I will sleep for 10 seconds. Do whatever you want. Random seed: %ld\n", seed);
|
||||
double t0 = dtime();
|
||||
char b[2] = {0};
|
||||
while(dtime() - t0 < 10.){ // read data from port and print in into terminal
|
||||
if(dev){
|
||||
if(read_tty(dev)){
|
||||
printf("Data from port: %s\n", dev->buf);
|
||||
printf("Got %zd bytes from port: %s\n", dev->buflen, dev->buf);
|
||||
t0 = dtime();
|
||||
}
|
||||
char ch = read_console();
|
||||
if(ch) write_tty(dev->comfd, &ch, 1);
|
||||
int r = read_console();
|
||||
if(r < 1) continue;
|
||||
t0 = dtime();
|
||||
b[0] = (char) r;
|
||||
printf("send to tty: %d (%c)\n", r, b[0]);
|
||||
write_tty(dev->comfd, b, 1);
|
||||
}
|
||||
}
|
||||
// clean everything
|
||||
|
||||
Reference in New Issue
Block a user