From 4914e5113fbd4d6933e9d92e8ef794712dca170d Mon Sep 17 00:00:00 2001 From: tTh Date: Wed, 12 Dec 2018 14:38:32 +0100 Subject: [PATCH 1/5] getting serial code from an old project --- serial/README.md | 10 ++++- serial/serial.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ serial/serial.h | 6 +++ 3 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 serial/serial.c create mode 100644 serial/serial.h diff --git a/serial/README.md b/serial/README.md index bdb9934..9b62351 100644 --- a/serial/README.md +++ b/serial/README.md @@ -1,8 +1,14 @@ - # Serial Input But premier de ce module : recevoir les données fournies par l'automate -de contrôle du phytotron. +de contrôle du phytotron. Ayant déja pratiqué ce genre de chose pour un +déja ancien +[dfhdfgh](http://art.dinorama.fr/bdf/) +avec _MadPhoenix_, je me propose de reprendre quelques parties de ce code. + + + + diff --git a/serial/serial.c b/serial/serial.c new file mode 100644 index 0000000..5162508 --- /dev/null +++ b/serial/serial.c @@ -0,0 +1,98 @@ + +#include +#include +#include //Used for UART +#include //Used for UART +#include //Used for UART + +#include "serial.h" + +/* -------------------------------------------------------------------- */ +int baudrate2const(int bauds) +{ +int br; + +switch (bauds) + { + case 1200: br = B1200; break; + case 2400: br = B2400; break; + case 4800: br = B4800; break; + case 9600: br = B9600; break; + case 19200: br = B19200; break; + case 38400: br = B38400; break; + case 57600: br = B57600; break; + case 115200: br = B115200; break; + default: + fprintf(stderr, "baudrate %d invalide\n", bauds); + exit(1); + break; + } +return br; +} +/* -------------------------------------------------------------------- */ + +int prepare_UART(char *port, int baudrate) +{ +int uart0 = -1; +int baudbits; +struct termios options; + +#if DEBUG_LEVEL +fprintf(stderr, "%s ( %s %d )\n", __func__, port, baudrate); +#endif +//OPEN THE UART +//The flags (defined in fcntl.h): +// Access modes (use 1 of these): +// O_RDONLY - Open for reading only. +// O_RDWR - Open for reading and writing. +// O_WRONLY - Open for writing only. +// +// O_NDELAY / O_NONBLOCK (same function) - Enables nonblocking mode. +// When set read requests on the file can return immediately with a +// failure status +// if there is no input immediately available (instead of blocking). +// Likewise, write requests can also return +// immediately with a failure status if the output can't be written +// immediately. +// +// O_NOCTTY - When set and path identifies a terminal device, open() +// shall not cause the terminal device to become the controlling terminal +// for the process. + +uart0 = open(port, O_RDWR | O_NOCTTY); +if (uart0== -1) + { + perror("unable to open uart "); + return -1; + } + +// CONFIGURE THE UART +// The flags defined in /usr/include/termios.h - +// see http://pubs.opengroup.org/onlinepubs/007908799/xsh/termios.h.html +// Baud rate:- B1200, B2400, B4800, B9600, B19200, B38400, B57600, +// B115200, B230400, B460800, B500000, B576000, B921600, B1000000, +// B1152000, B1500000, B2000000, B2500000, B3000000, B3500000, B4000000 +// CSIZE:- CS5, CS6, CS7, CS8 +// CLOCAL - Ignore modem status lines +// CREAD - Enable receiver +// IGNPAR = Ignore characters with parity errors +// ICRNL - Map CR to NL on input (Use for ASCII comms where you want +// to auto correct end of line characters - don't use for bianry comms!) +// PARENB - Parity enable +// PARODD - Odd parity (else even) + + +baudbits = baudrate2const(baudrate); +#if DEBUG_LEVEL +fprintf(stderr, "%d -> %x\n", baudrate, baudbits); +#endif +tcgetattr(uart0, &options); +options.c_cflag = baudbits | CS8 | CLOCAL | CREAD; // Date: Wed, 12 Dec 2018 14:47:57 +0100 Subject: [PATCH 2/5] hop... --- serial/README.md | 14 ++++++++++---- serial/serial.c | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/serial/README.md b/serial/README.md index 9b62351..98293fc 100644 --- a/serial/README.md +++ b/serial/README.md @@ -2,10 +2,16 @@ # Serial Input But premier de ce module : recevoir les données fournies par l'automate -de contrôle du phytotron. Ayant déja pratiqué ce genre de chose pour un -déja ancien -[dfhdfgh](http://art.dinorama.fr/bdf/) -avec _MadPhoenix_, je me propose de reprendre quelques parties de ce code. +de contrôle du phytotron. + +Ayant déja pratiqué ce genre de chose pour un déja ancien +[projet](http://art.dinorama.fr/bdf/) +avec _MadPhoenix_, je me propose de reprendre quelques parties de ce code, +de le remettre au gout du jour et de le tester dès que possible. + + + + diff --git a/serial/serial.c b/serial/serial.c index 5162508..d38f6c8 100644 --- a/serial/serial.c +++ b/serial/serial.c @@ -8,7 +8,7 @@ #include "serial.h" /* -------------------------------------------------------------------- */ -int baudrate2const(int bauds) +static int baudrate2const(int bauds) { int br; From 0514cc5d10ef8223a784aa4193122369ba020ae7 Mon Sep 17 00:00:00 2001 From: tTh Date: Wed, 12 Dec 2018 16:06:18 +0100 Subject: [PATCH 3/5] added a small test prg for serial in --- .gitignore | 2 +- serial/Makefile | 9 +++++++++ serial/serial.c | 18 ++++++++++++++++++ serial/serial.h | 2 ++ serial/t.c | 21 +++++++++++++++++++++ 5 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 serial/Makefile create mode 100644 serial/t.c diff --git a/.gitignore b/.gitignore index b678af8..47d4df6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ a.out *.o fake-values -essai +essai serail/t doc/*.toc doc/*.log doc/*.aux doc/*.pdf diff --git a/serial/Makefile b/serial/Makefile new file mode 100644 index 0000000..3af2ad8 --- /dev/null +++ b/serial/Makefile @@ -0,0 +1,9 @@ + + + +serial.o: serial.c serial.h Makefile + gcc -Wall -c $< + +t: t.c serial.o Makefile + gcc -Wall $< serial.o -o $@ + diff --git a/serial/serial.c b/serial/serial.c index d38f6c8..87dd3d9 100644 --- a/serial/serial.c +++ b/serial/serial.c @@ -96,3 +96,21 @@ tcsetattr(uart0, TCSANOW, &options); return uart0; } + +/* -------------------------------------------------------------------- */ + +int getbyte(int fd) +{ +unsigned char byte; +int foo; + +foo = read(fd, &byte, 1); +if (1 != foo) + { + perror("read a byte"); + return -1; + } +return (int)byte; +} + +/* -------------------------------------------------------------------- */ diff --git a/serial/serial.h b/serial/serial.h index bfec248..3bafcb5 100644 --- a/serial/serial.h +++ b/serial/serial.h @@ -4,3 +4,5 @@ int prepare_UART(char *port, int bauds); +int getbyte(int fd); + diff --git a/serial/t.c b/serial/t.c new file mode 100644 index 0000000..b912596 --- /dev/null +++ b/serial/t.c @@ -0,0 +1,21 @@ + + +#include + +#include "serial.h" + +int main (int argc, char *argv[]) +{ +int serial_in; +int byte, foo; + +serial_in = prepare_UART("/dev/ttyS0", 9600); +fprintf(stderr, "prepare uart -> %d\n", serial_in); + +for (foo=0; foo<20; foo++) { + byte = getbyte(serial_in); + printf("%6d %02x\n", foo, byte); + } + +return 0; +} From f9b0f41b58330b1e9f8e1a39218fe8bc2fd413ad Mon Sep 17 00:00:00 2001 From: tTh Date: Wed, 12 Dec 2018 16:10:02 +0100 Subject: [PATCH 4/5] re hop... --- serial/serial.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/serial/serial.c b/serial/serial.c index 87dd3d9..1de14ce 100644 --- a/serial/serial.c +++ b/serial/serial.c @@ -40,8 +40,8 @@ struct termios options; #if DEBUG_LEVEL fprintf(stderr, "%s ( %s %d )\n", __func__, port, baudrate); #endif -//OPEN THE UART -//The flags (defined in fcntl.h): +// OPEN THE UART +// The flags (defined in fcntl.h): // Access modes (use 1 of these): // O_RDONLY - Open for reading only. // O_RDWR - Open for reading and writing. @@ -96,9 +96,10 @@ tcsetattr(uart0, TCSANOW, &options); return uart0; } - /* -------------------------------------------------------------------- */ - +/* + * maybe we have to check for timeout ? + */ int getbyte(int fd) { unsigned char byte; From 277363a3574ac55a6ef45cca609bf68896eb02c2 Mon Sep 17 00:00:00 2001 From: tTh Date: Wed, 12 Dec 2018 16:51:47 +0100 Subject: [PATCH 5/5] clavo in .gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 47d4df6..6688893 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ a.out *.o fake-values -essai serail/t +essai serial/t doc/*.toc doc/*.log doc/*.aux doc/*.pdf