3 Read/write data via serial I/O
5 This program is distributed under the GPL, version 2
15 static int exitRequested = 0;
19 * SIGINT handler, so we can gracefully exit when the user hits ctrl-C.
22 sigintHandler(int signum)
27 int main(int argc, char **argv)
29 struct ftdi_context *ftdi;
30 unsigned char buf[1024];
34 int baudrate = 115200;
35 int interface = INTERFACE_ANY;
37 unsigned int pattern = 0xffff;
38 int retval = EXIT_FAILURE;
40 while ((i = getopt(argc, argv, "i:v:p:b:w::")) != -1)
44 case 'i': // 0=ANY, 1=A, 2=B, 3=C, 4=D
45 interface = strtoul(optarg, NULL, 0);
48 vid = strtoul(optarg, NULL, 0);
51 pid = strtoul(optarg, NULL, 0);
54 baudrate = strtoul(optarg, NULL, 0);
59 pattern = strtoul(optarg, NULL, 0);
62 fprintf(stderr, "Please provide a 8 bit pattern\n");
67 fprintf(stderr, "usage: %s [-i interface] [-v vid] [-p pid] [-b baudrate] [-w [pattern]]\n", *argv);
73 if ((ftdi = ftdi_new()) == 0)
75 fprintf(stderr, "ftdi_new failed\n");
79 if (!vid && !pid && (interface == INTERFACE_ANY))
81 ftdi_set_interface(ftdi, INTERFACE_ANY);
82 struct ftdi_device_list *devlist;
84 if ((res = ftdi_usb_find_all(ftdi, &devlist, 0, 0)) < 0)
86 fprintf(stderr, "No FTDI with default VID/PID found\n");
91 f = ftdi_usb_open_dev(ftdi, devlist[0].dev);
94 fprintf(stderr, "Unable to open device %d: (%s)",
95 i, ftdi_get_error_string(ftdi));
98 ftdi_list_free(&devlist);
101 fprintf(stderr, "%d Devices found, please select Device with VID/PID\n", res);
102 /* TODO: List Devices*/
107 fprintf(stderr, "No Devices found with default VID/PID\n");
114 ftdi_set_interface(ftdi, interface);
117 f = ftdi_usb_open(ftdi, vid, pid);
121 fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(ftdi));
126 f = ftdi_set_baudrate(ftdi, baudrate);
129 fprintf(stderr, "unable to set baudrate: %d (%s)\n", f, ftdi_get_error_string(ftdi));
133 /* Set line parameters
135 * TODO: Make these parameters settable from the command line
137 * Parameters are choosen that sending a continous stream of 0x55
138 * should give a square wave
141 f = ftdi_set_line_property(ftdi, 8, STOP_BIT_1, NONE);
144 fprintf(stderr, "unable to set line parameters: %d (%s)\n", f, ftdi_get_error_string(ftdi));
149 for(i=0; i<1024; i++)
152 signal(SIGINT, sigintHandler);
153 while (!exitRequested)
156 f = ftdi_write_data(ftdi, buf,
157 (baudrate/512 >sizeof(buf))?sizeof(buf):
158 (baudrate/512)?baudrate/512:1);
160 f = ftdi_read_data(ftdi, buf, sizeof(buf));
163 else if(f> 0 && !do_write)
165 fprintf(stderr, "read %d bytes\n", f);
166 fwrite(buf, f, 1, stdout);
171 signal(SIGINT, SIG_DFL);
172 retval = EXIT_SUCCESS;
174 ftdi_usb_close(ftdi);