Search for any devices with one of the default VID/PID, if no VID/PID if given
[libftdi] / examples / serial_test.c
1 /* serial_test.c
2
3    Read/write data via serial I/O
4
5    This program is distributed under the GPL, version 2
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <getopt.h>
12 #include <signal.h>
13 #include <ftdi.h>
14
15 static int exitRequested = 0;
16 /*
17  * sigintHandler --
18  *
19  *    SIGINT handler, so we can gracefully exit when the user hits ctrl-C.
20  */
21 static void
22 sigintHandler(int signum)
23 {
24     exitRequested = 1;
25 }
26
27 int main(int argc, char **argv)
28 {
29     struct ftdi_context *ftdi;
30     unsigned char buf[1024];
31     int f, i;
32     int vid = 0;
33     int pid = 0;
34     int baudrate = 115200;
35     int interface = INTERFACE_ANY;
36     int do_write = 0;
37     unsigned int pattern;
38     int retval = EXIT_FAILURE;
39
40     while ((i = getopt(argc, argv, "i:v:p:b:w::")) != -1)
41     {
42         switch (i)
43         {
44             case 'i': // 0=ANY, 1=A, 2=B, 3=C, 4=D
45                 interface = strtoul(optarg, NULL, 0);
46                 break;
47             case 'v':
48                 vid = strtoul(optarg, NULL, 0);
49                 break;
50             case 'p':
51                 pid = strtoul(optarg, NULL, 0);
52                 break;
53             case 'b':
54                 baudrate = strtoul(optarg, NULL, 0);
55                 break;
56             case 'w':
57                 do_write = 1;
58                 pattern = strtoul(optarg, NULL, 0);
59                 if (pattern > 0xff)
60                     fprintf(stderr, "Please provide a 8 bit pattern\n");
61                 break;
62             default:
63                 fprintf(stderr, "usage: %s [-i interface] [-v vid] [-p pid] [-b baudrate] [-w [pattern]]\n", *argv);
64                 exit(-1);
65         }
66     }
67
68     // Init
69     if ((ftdi = ftdi_new()) == 0)
70     {
71         fprintf(stderr, "ftdi_new failed\n");
72         return EXIT_FAILURE;
73     }
74
75     if (!vid && !pid && (interface == INTERFACE_ANY))
76     {
77         ftdi_set_interface(ftdi, INTERFACE_ANY);
78         struct ftdi_device_list *devlist;
79         int res;
80         if ((res = ftdi_usb_find_all(ftdi, &devlist, 0, 0)) < 0)
81         {
82             fprintf(stderr, "No FTDI with default VID/PID found\n");
83             goto do_deinit;
84         }
85         if (res == 1)
86         {
87             f = ftdi_usb_open_dev(ftdi,  devlist[0].dev);
88             if (f<0)
89             {
90                 fprintf(stderr, "Unable to open device %d: (%s)",
91                         i, ftdi_get_error_string(ftdi));
92             }
93         }
94         ftdi_list_free(&devlist);
95         if (res > 1)
96         {
97             fprintf(stderr, "%d Devices found, please select Device with VID/PID\n", res);
98             /* TODO: List Devices*/
99             goto do_deinit;
100         }
101         if (res == 0)
102         {
103             fprintf(stderr, "No Devices found with default VID/PID\n");
104             goto do_deinit;
105         }
106     }
107     else
108     {
109         // Select interface
110         ftdi_set_interface(ftdi, interface);
111         
112         // Open device
113         f = ftdi_usb_open(ftdi, vid, pid);
114     }
115     if (f < 0)
116     {
117         fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(ftdi));
118         exit(-1);
119     }
120
121     // Set baudrate
122     f = ftdi_set_baudrate(ftdi, baudrate);
123     if (f < 0)
124     {
125         fprintf(stderr, "unable to set baudrate: %d (%s)\n", f, ftdi_get_error_string(ftdi));
126         exit(-1);
127     }
128     
129     if (do_write)
130         for(i=0; i<1024; i++)
131             buf[i] = pattern;
132
133     signal(SIGINT, sigintHandler);
134     while (!exitRequested)
135     {
136         if (do_write)
137             f = ftdi_write_data(ftdi, buf, sizeof(buf));
138         else
139             f = ftdi_read_data(ftdi, buf, sizeof(buf));
140         if (f<0)
141             sleep(1);
142         else if(f> 0 && !do_write)
143         {
144             fprintf(stderr, "read %d bytes\n", f);
145             fwrite(buf, f, 1, stdout);
146             fflush(stderr);
147             fflush(stdout);
148         }
149     }
150     signal(SIGINT, SIG_DFL);
151     retval =  EXIT_SUCCESS;
152             
153     ftdi_usb_close(ftdi);
154     do_deinit:
155     ftdi_free(ftdi);
156
157     return retval;
158 }