Search for any devices with one of the default VID/PID, if no VID/PID if given
[libftdi] / examples / serial_test.c
CommitLineData
8b0b694f 1/* serial_test.c
d69dbd9e 2
8b0b694f 3 Read/write data via serial I/O
d69dbd9e
JP
4
5 This program is distributed under the GPL, version 2
6*/
7
8#include <stdio.h>
579b006f 9#include <stdlib.h>
d69dbd9e
JP
10#include <unistd.h>
11#include <getopt.h>
8b0b694f 12#include <signal.h>
d69dbd9e
JP
13#include <ftdi.h>
14
8b0b694f
UB
15static int exitRequested = 0;
16/*
17 * sigintHandler --
18 *
19 * SIGINT handler, so we can gracefully exit when the user hits ctrl-C.
20 */
21static void
22sigintHandler(int signum)
23{
24 exitRequested = 1;
25}
26
d69dbd9e
JP
27int main(int argc, char **argv)
28{
8b0b694f 29 struct ftdi_context *ftdi;
97c6b5f6 30 unsigned char buf[1024];
d69dbd9e 31 int f, i;
8b0b694f
UB
32 int vid = 0;
33 int pid = 0;
d69dbd9e
JP
34 int baudrate = 115200;
35 int interface = INTERFACE_ANY;
8b0b694f
UB
36 int do_write = 0;
37 unsigned int pattern;
38 int retval = EXIT_FAILURE;
d69dbd9e 39
8b0b694f 40 while ((i = getopt(argc, argv, "i:v:p:b:w::")) != -1)
d69dbd9e
JP
41 {
42 switch (i)
43 {
05c2e40a
TJ
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;
8b0b694f
UB
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;
05c2e40a 62 default:
8b0b694f 63 fprintf(stderr, "usage: %s [-i interface] [-v vid] [-p pid] [-b baudrate] [-w [pattern]]\n", *argv);
05c2e40a 64 exit(-1);
d69dbd9e
JP
65 }
66 }
67
68 // Init
8b0b694f 69 if ((ftdi = ftdi_new()) == 0)
d69dbd9e 70 {
8b0b694f 71 fprintf(stderr, "ftdi_new failed\n");
d69dbd9e
JP
72 return EXIT_FAILURE;
73 }
74
8b0b694f
UB
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 }
d69dbd9e
JP
115 if (f < 0)
116 {
8b0b694f 117 fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(ftdi));
d69dbd9e
JP
118 exit(-1);
119 }
120
121 // Set baudrate
8b0b694f 122 f = ftdi_set_baudrate(ftdi, baudrate);
d69dbd9e
JP
123 if (f < 0)
124 {
8b0b694f 125 fprintf(stderr, "unable to set baudrate: %d (%s)\n", f, ftdi_get_error_string(ftdi));
d69dbd9e
JP
126 exit(-1);
127 }
8b0b694f
UB
128
129 if (do_write)
130 for(i=0; i<1024; i++)
131 buf[i] = pattern;
d69dbd9e 132
8b0b694f
UB
133 signal(SIGINT, sigintHandler);
134 while (!exitRequested)
05c2e40a 135 {
8b0b694f
UB
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 }
d69dbd9e 149 }
8b0b694f
UB
150 signal(SIGINT, SIG_DFL);
151 retval = EXIT_SUCCESS;
152
153 ftdi_usb_close(ftdi);
154 do_deinit:
155 ftdi_free(ftdi);
d69dbd9e 156
8b0b694f 157 return retval;
d69dbd9e 158}