Newer
Older
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <wiringSerial.h>
#include <wiringPi.h>
#include <iostream>
using namespace std;
const char TURN_ON[] = "Turn Led On";
const char TURN_OFF[] = "Turn Led Off";
int main(int argc, char const *argv[])
{
int fd = serialOpen("/dev/ttyS0", 9600);
if (fd == -1 || wiringPiSetup() == -1) {
printf("UART PAS OK\n");
exit(EXIT_FAILURE);
}
serialPrintf(fd, TURN_ON);
delay(1000);
int response;
while (true) {
if (serialDataAvail(fd)) {
response = serialGetchar(fd);
break;
}
}
cout << response << endl;
printf("%c\n", response);
serialPrintf(fd, TURN_OFF);
delay(200);
while (true) {
if (serialDataAvail(fd) > 0) {
cout << serialDataAvail(fd) << endl;
for (int i = 0; i < serialDataAvail(fd); i++)
{
response = serialGetchar(fd);
}
break;
}
}
cout << response << endl;
serialClose(fd);
return 0;
}