data.c
1.77 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stropts.h>
int pt;
struct termios tty, old; /* Create the structure */
void reset_tty(){
tcsetattr(pt, TCSANOW, &old);
close(pt);
}
int main(){
int r=-1;
char * device = "/dev/ttyS3";
pt = open(device, O_RDWR | O_NOCTTY | O_SYNC);
if(pt == -1){
perror("open");
exit(-1);
}
printf("open : %d\n",pt);
ioctl(pt, I_SRDOPT, RMSGD);
tcgetattr(pt, &old);
atexit(reset_tty);
tcgetattr(pt, &tty); // Get the current attributes of the Serial port
//cfmakeraw(&tty);
tty.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON | IXOFF);
printf("iflag = %x\n",tty.c_iflag);
tty.c_oflag &= ~(OPOST);
printf("oflag = %x\n",tty.c_oflag);
tty.c_cflag |= (CS8);
tty.c_cflag &= ~(CSIZE|PARENB);
printf("cflag = %x\n",tty.c_cflag);
tty.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
printf("lflag = %x\n",tty.c_lflag);
tty.c_cc[VMIN] = 7;
tty.c_cc[VTIME] = 0;
cfsetispeed(&tty,B9600); // Setting the Baud rate
cfsetospeed(&tty,B9600);
/*Peut-être inutile si on est en liaison série et pas en USB : le TCSAFLUSH devrait suffir*/
sleep(1);
if(tcflush(pt, TCIFLUSH)==-1){
perror("tcflush");
exit(-1);
}
if(tcsetattr(pt, TCSANOW, &tty)==-1){
perror("tcsetattr");
}
int i = 0;
char end[20];
if(cfgetispeed(&tty)!=B9600)
{
perror("cfgetispeed");
exit(-1);
}
int count = 0;
char line[20];
memset(line,0,sizeof(line));
while(count<14){
r = read(pt ,line,1);
printf("c = %c (characters read : %d)\n",line[0],r);
//printf("l = %c\n",line[0]);
count++;
memset(line,0,sizeof(line));
printf("\n");
}
//close(pt);
return 0;
}