3 Read/write data via serial I/O
5 This program is distributed under the GPL, version 2
12 #define sleep(x) Sleep(x)
18 static int exitRequested = 0;
22 * SIGINT handler, so we can gracefully exit when the user hits ctrl-C.
25 sigintHandler(int signum)
30 int main(int argc, char **argv)
32 struct ftdi_context *ftdi;
33 unsigned char buf[1024];
37 int baudrate = 115200;
38 int interface = INTERFACE_ANY;
40 unsigned int pattern = 0xffff;
41 int retval = EXIT_FAILURE;
43 while ((i = getopt(argc, argv, "i:v:p:b:w::")) != -1)
47 case 'i': // 0=ANY, 1=A, 2=B, 3=C, 4=D
48 interface = strtoul(optarg, NULL, 0);
51 vid = strtoul(optarg, NULL, 0);
54 pid = strtoul(optarg, NULL, 0);
57 baudrate = strtoul(optarg, NULL, 0);
62 pattern = strtoul(optarg, NULL, 0);
65 fprintf(stderr, "Please provide a 8 bit pattern\n");
70 fprintf(stderr, "usage: %s [-i interface] [-v vid] [-p pid] [-b baudrate] [-w [pattern]]\n", *argv);
76 if ((ftdi = ftdi_new()) == 0)
78 fprintf(stderr, "ftdi_new failed\n");
82 if (!vid && !pid && (interface == INTERFACE_ANY))
84 ftdi_set_interface(ftdi, INTERFACE_ANY);
85 struct ftdi_device_list *devlist;
87 if ((res = ftdi_usb_find_all(ftdi, &devlist, 0, 0)) < 0)
89 fprintf(stderr, "No FTDI with default VID/PID found\n");
94 f = ftdi_usb_open_dev(ftdi, devlist[0].dev);
97 fprintf(stderr, "Unable to open device %d: (%s)",
98 i, ftdi_get_error_string(ftdi));
101 ftdi_list_free(&devlist);
104 fprintf(stderr, "%d Devices found, please select Device with VID/PID\n", res);
105 /* TODO: List Devices*/
110 fprintf(stderr, "No Devices found with default VID/PID\n");
117 ftdi_set_interface(ftdi, interface);
120 f = ftdi_usb_open(ftdi, vid, pid);
124 fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(ftdi));
129 f = ftdi_set_baudrate(ftdi, baudrate);
132 fprintf(stderr, "unable to set baudrate: %d (%s)\n", f, ftdi_get_error_string(ftdi));
136 /* Set line parameters
138 * TODO: Make these parameters settable from the command line
140 * Parameters are choosen that sending a continous stream of 0x55
141 * should give a square wave
144 f = ftdi_set_line_property(ftdi, 8, STOP_BIT_1, NONE);
147 fprintf(stderr, "unable to set line parameters: %d (%s)\n", f, ftdi_get_error_string(ftdi));
152 for(i=0; i<1024; i++)
155 signal(SIGINT, sigintHandler);
156 while (!exitRequested)
159 f = ftdi_write_data(ftdi, buf,
160 (baudrate/512 >sizeof(buf))?sizeof(buf):
161 (baudrate/512)?baudrate/512:1);
163 f = ftdi_read_data(ftdi, buf, sizeof(buf));
166 else if(f> 0 && !do_write)
168 fprintf(stderr, "read %d bytes\n", f);
169 fwrite(buf, f, 1, stdout);
174 signal(SIGINT, SIG_DFL);
175 retval = EXIT_SUCCESS;
177 ftdi_usb_close(ftdi);