3 Read data via serial I/O
5 This program is distributed under the GPL, version 2
13 int main(int argc, char **argv)
15 struct ftdi_context ftdic;
20 int baudrate = 115200;
21 int interface = INTERFACE_ANY;
23 while ((i = getopt(argc, argv, "i:v:p:b:")) != -1)
27 case 'i': // 0=ANY, 1=A, 2=B, 3=C, 4=D
28 interface = strtoul(optarg, NULL, 0);
31 vid = strtoul(optarg, NULL, 0);
34 pid = strtoul(optarg, NULL, 0);
37 baudrate = strtoul(optarg, NULL, 0);
40 fprintf(stderr, "usage: %s [-i interface] [-v vid] [-p pid] [-b baudrate]\n", *argv);
46 if (ftdi_init(&ftdic) < 0)
48 fprintf(stderr, "ftdi_init failed\n");
52 // Select first interface
53 ftdi_set_interface(&ftdic, interface);
56 f = ftdi_usb_open(&ftdic, vid, pid);
59 fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(&ftdic));
64 f = ftdi_set_baudrate(&ftdic, 115200);
67 fprintf(stderr, "unable to set baudrate: %d (%s)\n", f, ftdi_get_error_string(&ftdic));
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);
79 ftdi_usb_close(&ftdic);