examples: add sio example
authorJim Paris <jim@jtan.com>
Wed, 2 Dec 2009 23:13:44 +0000 (18:13 -0500)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Fri, 4 Dec 2009 09:03:43 +0000 (10:03 +0100)
Simple example to just receive and print data.

examples/CMakeLists.txt
examples/Makefile.am
examples/sio.c [new file with mode: 0644]

index ccd2f89..b227115 100644 (file)
@@ -13,6 +13,7 @@ add_executable(bitbang2 bitbang2.c)
 add_executable(bitbang_cbus bitbang_cbus.c)
 add_executable(bitbang_ft2232 bitbang_ft2232.c)
 add_executable(find_all find_all.c)
+add_executable(sio sio.c)
 
 # Linkage
 target_link_libraries(simple ftdi)
@@ -21,6 +22,7 @@ target_link_libraries(bitbang2 ftdi)
 target_link_libraries(bitbang_cbus ftdi)
 target_link_libraries(bitbang_ft2232 ftdi)
 target_link_libraries(find_all ftdi)
+target_link_libraries(sio ftdi)
 
 # libftdi++ examples
 if(FTDI_BUILD_CPP)
index bcd50ec..0aca8a2 100644 (file)
@@ -16,6 +16,7 @@ bin_PROGRAMS = simple \
        bitbang_ft2232 \
        bitbang_cbus \
        find_all \
+       sio \
        $(examples_libftdipp)
 
 # Don't install the example files
@@ -27,6 +28,7 @@ bitbang2_SOURCES = bitbang2.c
 bitbang_ft2232_SOURCES = bitbang_ft2232.c
 bitbang_cbus_SOURCES = bitbang_cbus.c
 find_all_SOURCES = find_all.c
+sio_SOURCES = sio.c
 
 if HAVE_LIBFTDIPP
 find_all_pp_SOURCES = find_all_pp.cpp
diff --git a/examples/sio.c b/examples/sio.c
new file mode 100644 (file)
index 0000000..3425ec7
--- /dev/null
@@ -0,0 +1,81 @@
+/* sio.c
+
+   Read data via serial I/O
+
+   This program is distributed under the GPL, version 2
+*/
+
+#include <stdio.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <ftdi.h>
+
+int main(int argc, char **argv)
+{
+    struct ftdi_context ftdic;
+    char buf[1024];
+    int f, i;
+    int vid = 0x0403;
+    int pid = 0x6001;
+    int baudrate = 115200;
+    int interface = INTERFACE_ANY;
+
+    while ((i = getopt(argc, argv, "i:v:p:b:")) != -1)
+    {
+        switch (i)
+        {
+       case 'i': // 0=ANY, 1=A, 2=B, 3=C, 4=D
+               interface = strtoul(optarg, NULL, 0);
+               break;
+       case 'v':
+               vid = strtoul(optarg, NULL, 0);
+               break;
+       case 'p':
+               pid = strtoul(optarg, NULL, 0);
+               break;
+       case 'b':
+               baudrate = strtoul(optarg, NULL, 0);
+               break;
+       default:
+               fprintf(stderr, "usage: %s [-i interface] [-v vid] [-p pid] [-b baudrate]\n", *argv);
+               exit(-1);
+        }
+    }
+
+    // Init
+    if (ftdi_init(&ftdic) < 0)
+    {
+        fprintf(stderr, "ftdi_init failed\n");
+        return EXIT_FAILURE;
+    }
+
+    // Select first interface
+    ftdi_set_interface(&ftdic, interface);
+
+    // Open device
+    f = ftdi_usb_open(&ftdic, vid, pid);
+    if (f < 0)
+    {
+        fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(&ftdic));
+        exit(-1);
+    }
+
+    // Set baudrate
+    f = ftdi_set_baudrate(&ftdic, 115200);
+    if (f < 0)
+    {
+        fprintf(stderr, "unable to set baudrate: %d (%s)\n", f, ftdi_get_error_string(&ftdic));
+        exit(-1);
+    }
+
+    // Read data forever
+    while ((f = ftdi_read_data(&ftdic, buf, sizeof(buf))) >= 0) {
+           fprintf(stderr, "read %d bytes\n", f);
+           fwrite(buf, f, 1, stdout);
+           fflush(stderr);
+           fflush(stdout);
+    }
+
+    ftdi_usb_close(&ftdic);
+    ftdi_deinit(&ftdic);
+}