fix mem leaks in examples ftdi_deinit -> ftdi_free
[libftdi] / examples / serial_test.c
index 7c89e78..6a9d8ff 100644 (file)
@@ -8,6 +8,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
+#ifdef __WIN32__
+#define sleep(x) Sleep(x)
+#endif
 #include <getopt.h>
 #include <signal.h>
 #include <ftdi.h>
@@ -28,7 +31,7 @@ int main(int argc, char **argv)
 {
     struct ftdi_context *ftdi;
     unsigned char buf[1024];
-    int f, i;
+    int f = 0, i;
     int vid = 0x403;
     int pid = 0;
     int baudrate = 115200;
@@ -130,6 +133,21 @@ int main(int argc, char **argv)
         exit(-1);
     }
     
+    /* Set line parameters
+     *
+     * TODO: Make these parameters settable from the command line
+     *
+     * Parameters are choosen that sending a continous stream of 0x55 
+     * should give a square wave
+     *
+     */
+    f = ftdi_set_line_property(ftdi, 8, STOP_BIT_1, NONE);
+    if (f < 0)
+    {
+        fprintf(stderr, "unable to set line parameters: %d (%s)\n", f, ftdi_get_error_string(ftdi));
+        exit(-1);
+    }
+    
     if (do_write)
         for(i=0; i<1024; i++)
             buf[i] = pattern;
@@ -138,7 +156,9 @@ int main(int argc, char **argv)
     while (!exitRequested)
     {
         if (do_write)
-            f = ftdi_write_data(ftdi, buf, sizeof(buf));
+            f = ftdi_write_data(ftdi, buf, 
+                                (baudrate/512 >sizeof(buf))?sizeof(buf):
+                                (baudrate/512)?baudrate/512:1);
         else
             f = ftdi_read_data(ftdi, buf, sizeof(buf));
         if (f<0)