1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| #include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h> // settins du port serie
#include <stdio.h>
// baudrate settings are defined in <asm/termbits.h>,
// which is included by <termios.h>
#define BAUDRATE B1200
#define ADRESSE_PORT "/dev/ttyS0" // numero du port
#define _POSIX_SOURCE 1 // POSIX compliant source
#define FALSE 0
#define TRUE 1
volatile int STOP=FALSE;
main()
{
int fd, c, res;
struct termios oldtio, newtio;
char buf[255]; // nombre max de caracteres ds le buffuer pr une lecture
// Open modem device for reading and writing and not as controlling tty
// because we don't want to get killed if linenoise sends CTRL-C.
fd = open(ADRESSE_PORT, O_RDWR | O_NOCTTY );
// O_NOCTTY flag tells UNIX that this program doesn't want to be the "controlling terminal" for that port. If you don't specify this then any input (such as keyboard abort signals and so forth) will affect your process. Programs like getty(1M/8) use this feature when starting the login process, but normally a user program does not want this behavior.
if (fd <0) {perror(ADRESSE_PORT); exit(-1); }
tcgetattr(fd,&oldtio); // save current port settings
bzero(&newtio, sizeof(newtio)); // clear struct for new port settings
newtio.c_cflag = BAUDRATE | CRTSCTS | CS7 | CLOCAL | CREAD;
// BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed.
// CRTSCTS : output hardware flow control (only used if the cable has
// all necessary lines. See sect. 7 of Serial-HOWTO)
// CS8 : 8n1 (8bit,no parity,1 stopbit)
// CLOCAL : local connection, no modem contol
// CREAD : enable receiving characters
newtio.c_iflag = IGNPAR;
// IGNPAR : ignore bytes with parity errors
// ICRNL : map CR to NL (otherwise a CR input on the other computer
// will not terminate input)
// otherwise make device raw (no other input processing)
newtio.c_oflag = 0;
// set input mode (non-canonical, no echo,...)
newtio.c_lflag = 0; // Raw output
// initialize all control characters
// default values can be found in /usr/include/termios.h, and are given
// in the comments, but we don't need them here
newtio.c_cc[VTIME] = 0; // inter-character timer --> sets the character timer
newtio.c_cc[VMIN] = 6; // blocking read until 5 chars received. sets the minimum number of characters to receive before satisfying the read
// now clean the modem line and activate the settings for the port
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
// terminal settings done, now handle input
// In this example, inputting a 'z' at the beginning of a line will
// exit the program.
while (STOP==FALSE) { // loop until we have a terminating condition
// read blocks program execution until a line terminating character is
// input, even if more than 255 chars are input. If the number
// of characters read is smaller than the number of chars available,
// subsequent reads will return the remaining chars. res will be set
// to the actual number of characters actually read
res = read(fd,buf,255); // returns after 5 chars have been input
buf[res]=0;
printf("%s\n", buf);
if (buf[0]=='z') STOP=TRUE;
}
tcsetattr(fd,TCSANOW,&oldtio); // restore the old port settings
} |
Partager