Commit | Line | Data |
---|---|---|
6167b919 | 1 | /* serial_read.c |
d69dbd9e JP |
2 | |
3 | Read data via serial I/O | |
4 | ||
5 | This program is distributed under the GPL, version 2 | |
6 | */ | |
7 | ||
8 | #include <stdio.h> | |
9 | #include <unistd.h> | |
10 | #include <getopt.h> | |
11 | #include <ftdi.h> | |
12 | ||
13 | int main(int argc, char **argv) | |
14 | { | |
15 | struct ftdi_context ftdic; | |
16 | char buf[1024]; | |
17 | int f, i; | |
18 | int vid = 0x0403; | |
19 | int pid = 0x6001; | |
20 | int baudrate = 115200; | |
21 | int interface = INTERFACE_ANY; | |
22 | ||
23 | while ((i = getopt(argc, argv, "i:v:p:b:")) != -1) | |
24 | { | |
25 | switch (i) | |
26 | { | |
27 | case 'i': // 0=ANY, 1=A, 2=B, 3=C, 4=D | |
28 | interface = strtoul(optarg, NULL, 0); | |
29 | break; | |
30 | case 'v': | |
31 | vid = strtoul(optarg, NULL, 0); | |
32 | break; | |
33 | case 'p': | |
34 | pid = strtoul(optarg, NULL, 0); | |
35 | break; | |
36 | case 'b': | |
37 | baudrate = strtoul(optarg, NULL, 0); | |
38 | break; | |
39 | default: | |
40 | fprintf(stderr, "usage: %s [-i interface] [-v vid] [-p pid] [-b baudrate]\n", *argv); | |
41 | exit(-1); | |
42 | } | |
43 | } | |
44 | ||
45 | // Init | |
46 | if (ftdi_init(&ftdic) < 0) | |
47 | { | |
48 | fprintf(stderr, "ftdi_init failed\n"); | |
49 | return EXIT_FAILURE; | |
50 | } | |
51 | ||
52 | // Select first interface | |
53 | ftdi_set_interface(&ftdic, interface); | |
54 | ||
55 | // Open device | |
56 | f = ftdi_usb_open(&ftdic, vid, pid); | |
57 | if (f < 0) | |
58 | { | |
59 | fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(&ftdic)); | |
60 | exit(-1); | |
61 | } | |
62 | ||
63 | // Set baudrate | |
64 | f = ftdi_set_baudrate(&ftdic, 115200); | |
65 | if (f < 0) | |
66 | { | |
67 | fprintf(stderr, "unable to set baudrate: %d (%s)\n", f, ftdi_get_error_string(&ftdic)); | |
68 | exit(-1); | |
69 | } | |
70 | ||
71 | // Read data forever | |
72 | while ((f = ftdi_read_data(&ftdic, buf, sizeof(buf))) >= 0) { | |
73 | fprintf(stderr, "read %d bytes\n", f); | |
74 | fwrite(buf, f, 1, stdout); | |
75 | fflush(stderr); | |
76 | fflush(stdout); | |
77 | } | |
78 | ||
79 | ftdi_usb_close(&ftdic); | |
80 | ftdi_deinit(&ftdic); | |
81 | } |