/* Fixe un device en mode RAW */
void raw_mode (fd, old_term)
int fd;
struct termios *old_term;
{
struct termios term;
tcgetattr(fd, &term);
/* Sauve l'ancienne config dans le paramètre */
tcgetattr(fd, old_term);
/* mode RAW, pas de mode canonique, pas d'echo */
term.c_iflag = IGNBRK;
term.c_lflag = 0;
term.c_oflag = 0;
/* Controle de flux hardware RTS/CTS)
term.c_cflag |= (CREAD | CRTSCTS);
/* 1 caractère suffit */
term.c_cc[VMIN] = 1;
/* Donnée disponible immédiatement */
term.c_cc[VTIME] = 0;
/* Inhibe le controle de flux XON/XOFF */
term.c_iflag &= ~(IXON|IXOFF|IXANY);
/* 8 bits de données, pas de parité */
term.c_cflag &= ~(PARENB | CSIZE);
term.c_cflag |= CS8;
/* Gestion des signaux modem */
term.c_cflag &= ~CLOCAL;
tcsetattr(fd, TCSANOW, &term);
}
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <fcntl.h>
main (int ac, char **av)
{
int fd;
struct termios tty, old;
/* Ouverture du device */
if ((fd = open (av[1], O_RDWR)) < 0) {
perror (av[1]);
exit (1);
}
/* Lecture des paramètres */
tcgetattr(fd, &tty);
tcgetattr(fd, &old);
/* On passe la vitesse à 0 ==> hangup */
cfsetospeed(&tty, B0);
cfsetispeed(&tty, B0);
/* On applique le nouveau paramétrage pendant 1s */
tcsetattr(fd, TCSANOW, &tty);
sleep(1);
/* On revient à l'ancien et on quitte */
tcsetattr(fd, TCSANOW, &old);
close (fd);
}
Changement de vitesse
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdarg.h>
#include <signal.h>
/*struct termios stdin_saved_attributes;*/
struct termios tty_saved_attributes;
int tty_fd;
int tty_open(char* tty_dev) {
struct termios new_attributes;
tty_fd = open(tty_dev,O_RDWR| O_NOCTTY | O_NONBLOCK);
if (tty_fd<0) {
return -1;
} else {
tcgetattr(tty_fd,&tty_saved_attributes);
tcgetattr(tty_fd,&new_attributes);
/*c_cflag */
new_attributes.c_cflag |= CREAD; /* Enable receiver */
new_attributes.c_cflag |= B9600; /* Set baud rate */
new_attributes.c_cflag |= CS8; /* 8 data bit */
/* c_iflag */
new_attributes.c_iflag |= IGNPAR; /* Ignore framing errors and parity errors. */
/* c_lflag */
new_attributes.c_lflag &= ~(ICANON); /* DISABLE canonical mode. */
/* Disables the special characters EOF, EOL, EOL2, ERASE, KILL, LNEXT, REPRINT, STATUS, and WERASE, and buffers by lines. */
new_attributes.c_lflag &= ~(ECHO); /* DISABLE this: Echo input characters. */
new_attributes.c_lflag &= ~(ECHOE);
/* DISABLE this: If ICANON is also set, the ERASE character erase the preceding input */
/* character, and WERASE erases the preceding word. */
new_attributes.c_lflag &= ~(ISIG); /* DISABLE this: When any of the characters INTR, QUIT, SUSP,*/
/* or DSUSP are received, generate the corresponding signal.*/
new_attributes.c_cc[VMIN]=1; /* Minimum number of characters for non-canonical read.*/
new_attributes.c_cc[VTIME]=0; /* Timeout in deciseconds for non-canonical read.*/
tcsetattr(tty_fd, TCSANOW, &new_attributes);
}
return tty_fd;
}
void close_tty()
{
tcsetattr(tty_fd, TCSANOW, &tty_saved_attributes);
}
int main(int argc, char *argv[])
{
if (argc!=2)
exit(EXIT_FAILURE);
printf("Lunartec\n");
tty_fd= tty_open(argv[1]);
if (tty_fd<0) {
fprintf (stderr,"tty open error %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
write(tty_fd,"Bonjour le monde ",18);
close_tty();
return EXIT_SUCCESS;
}