-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathexample.c
48 lines (42 loc) · 1.19 KB
/
example.c
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
/*
* Copyright 2014 Thomas Buck <[email protected]>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "src/serial.h"
int fd = -1;
void exithandler() {
serialClose(fd);
}
int main(int argc, char **argv) {
if ((argc == 2) && (strcmp(argv[1], "-l") == 0)) {
char **ports = getSerialPorts();
char **tmp = ports;
printf("Available serial ports:\n");
while (*tmp != NULL)
printf(" %s\n", *(tmp++));
free(ports);
} else if (argc == 3) {
fd = serialOpen(argv[1], atoi(argv[2]));
if (fd == -1) {
printf("Could not open %s with %d!\n", argv[1], atoi(argv[2]));
return 1;
}
atexit(exithandler);
printf("Connected! Data received will be printed...\n");
printf("Use CTRL+C to quit.\n");
for (;;) {
if (serialHasChar(fd)) {
char c;
serialReadChar(fd, &c);
printf("%c", c);
}
}
} else {
printf("Usage:\n");
printf(" %s -l --> list available ports\n", argv[0]);
printf(" %s PORT BAUD --> monitor port\n", argv[0]);
}
return 0;
}