testSerial.c
1.02 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
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
// Lecture port série
typedef int serialPort;
serialPort SerialLib_open(const char * portPath);
int main(int argc, char **argv) {
serialPort p = SerialLib_open("/dev/ttyAMA0");
char buffer[1];
printf("ouverture reussi\n");
while(1){
printf("lecture d'un caractere\n");
read(p, buffer ,1);
printf("Message recu: %s \n",buffer);
}
return 0;
}
/**
* Ouverture du port
*
*
*/
serialPort SerialLib_open(const char * serialPortPath) {
int fd;
fd = open(serialPortPath, O_RDWR | O_NOCTTY);
if (fd == -1) {
printf("Could not open the serial port : %s - ", serialPortPath);
}else {
fcntl(fd, F_SETFL, 0);
}
return (serialPort)fd;
}