3 Read data via serial I/O
5 This program is distributed under the GPL, version 2
14 int main(int argc, char **argv)
16 struct ftdi_context ftdic;
21 int baudrate = 115200;
22 int interface = INTERFACE_ANY;
24 while ((i = getopt(argc, argv, "i:v:p:b:")) != -1)
28 case 'i': // 0=ANY, 1=A, 2=B, 3=C, 4=D
29 interface = strtoul(optarg, NULL, 0);
32 vid = strtoul(optarg, NULL, 0);
35 pid = strtoul(optarg, NULL, 0);
38 baudrate = strtoul(optarg, NULL, 0);
41 fprintf(stderr, "usage: %s [-i interface] [-v vid] [-p pid] [-b baudrate]\n", *argv);
47 if (ftdi_init(&ftdic) < 0)
49 fprintf(stderr, "ftdi_init failed\n");
53 // Select first interface
54 ftdi_set_interface(&ftdic, interface);
57 f = ftdi_usb_open(&ftdic, vid, pid);
60 fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(&ftdic));
65 f = ftdi_set_baudrate(&ftdic, 115200);
68 fprintf(stderr, "unable to set baudrate: %d (%s)\n", f, ftdi_get_error_string(&ftdic));
73 while ((f = ftdi_read_data(&ftdic, buf, sizeof(buf))) >= 0) {
74 fprintf(stderr, "read %d bytes\n", f);
75 fwrite(buf, f, 1, stdout);
80 ftdi_usb_close(&ftdic);