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
| #include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
int main(void)
{
int port_serie;
struct termios options;
int reception;
char buffer[10] = "";
char data = 0xC1;
port_serie = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
if(port_serie < 0)
{
perror("Port serie ouvert");
exit(-1);
}
else
{
printf("Port serie bien ouvert. \n");
printf("Port serie %d. \n", port_serie);
tcgetattr(port_serie, &options);
cfsetospeed(&options, B9600);
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
tcsetattr(port_serie, TCSANOW, &options);
printf("Configuration OK !. \n");
// Envoie de donnees.
printf("Envoie des donnees en cours ... \n");
if (write(port_serie, &data, 1) < 0){perror("write:");}
printf("Envoie Termine. \n");
printf("Attente. \n");
sleep(1);
printf("Fin Attente. \n");
// Lecture des donnees.
printf("Lecture des donnees en cours ... \n");
fcntl(port_serie,F_SETFL,10);
reception=read(port_serie,buffer,100);
printf("J'ai lu %d charactères: %s\n",reception,buffer);
if (reception == -1)
printf("Erreur lecture port serie\n");
printf("Reception fini.\n" );
}
close(port_serie);
} |
Partager