getting serial code from an old project
This commit is contained in:
parent
6bdfad1650
commit
4914e5113f
@ -1,8 +1,14 @@
|
|||||||
|
|
||||||
|
|
||||||
# Serial Input
|
# Serial Input
|
||||||
|
|
||||||
But premier de ce module : recevoir les données fournies par l'automate
|
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.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
98
serial/serial.c
Normal file
98
serial/serial.c
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h> //Used for UART
|
||||||
|
#include <fcntl.h> //Used for UART
|
||||||
|
#include <termios.h> //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; //<Set baud rate
|
||||||
|
options.c_iflag = IGNPAR;
|
||||||
|
options.c_oflag = 0;
|
||||||
|
options.c_lflag = 0;
|
||||||
|
tcflush(uart0, TCIFLUSH);
|
||||||
|
tcsetattr(uart0, TCSANOW, &options);
|
||||||
|
|
||||||
|
return uart0;
|
||||||
|
}
|
6
serial/serial.h
Normal file
6
serial/serial.h
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
/*
|
||||||
|
* serial.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
int prepare_UART(char *port, int bauds);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user