summaryrefslogtreecommitdiff
path: root/nuttx/examples/usbserial/main.c
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2008-10-07 23:04:52 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2008-10-07 23:04:52 +0000
commit4079a1f2e089d2004edfd9fab139aab75d4cb93e (patch)
tree7d98ac054963e6234124fe32bb40947c0a97614a /nuttx/examples/usbserial/main.c
parent25007fa5247fb9fd30b21987a74b894998d8e400 (diff)
downloadpx4-nuttx-4079a1f2e089d2004edfd9fab139aab75d4cb93e.tar.gz
px4-nuttx-4079a1f2e089d2004edfd9fab139aab75d4cb93e.tar.bz2
px4-nuttx-4079a1f2e089d2004edfd9fab139aab75d4cb93e.zip
Add retry on open; say hello many times
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@993 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/examples/usbserial/main.c')
-rw-r--r--nuttx/examples/usbserial/main.c76
1 files changed, 68 insertions, 8 deletions
diff --git a/nuttx/examples/usbserial/main.c b/nuttx/examples/usbserial/main.c
index 3657fc9fe..e901938aa 100644
--- a/nuttx/examples/usbserial/main.c
+++ b/nuttx/examples/usbserial/main.c
@@ -46,6 +46,20 @@
* Definitions
****************************************************************************/
+#ifdef CONFIG_CPP_HAVE_VARARGS
+# ifdef CONFIG_DEBUG
+# define message(...) lib_lowprintf(__VA_ARGS__)
+# else
+# define message(...) printf(__VA_ARGS__)
+# endif
+#else
+# ifdef CONFIG_DEBUG
+# define message lib_lowprintf
+# else
+# define message printf
+# endif
+#endif
+
/****************************************************************************
* Private Data
****************************************************************************/
@@ -78,26 +92,72 @@ int user_start(int argc, char *argv[])
/* Initialize the USB serial driver */
+ message("user_start: Registering USB serial driver\n");
ret = usbdev_serialinitialize(0);
if (ret < 0)
{
- printf("ERROR: Failed to create the USB serial device: %d\n", -ret);
+ message("user_start: ERROR: Failed to create the USB serial device: %d\n", -ret);
return 1;
}
+ message("user_start: Successfully registered the serial driver\n");
/* Open the USB serial device for output */
- stream = fopen("/dev/ttyUSB0", "w");
- if (!stream)
+ do
+ {
+ message("user_start: Opening USB serial driver\n");
+ stream = fopen("/dev/ttyUSB0", "w");
+ if (!stream)
+ {
+ int errcode = errno;
+ message("user_start: ERROR: Failed to open /dev/ttyUSB0: %d\n", errcode);
+
+ /* ENOTCONN means that the USB device is not yet connected */
+
+ if (errcode = ENOTCONN)
+ {
+ message("user_start: Not connected. Wait and try again.\n");
+ sleep(5);
+ }
+ else
+ {
+ /* Give up on other errors */
+
+ message("user_start: Aborting\n");
+ return 2;
+ }
+ }
+ }
+ while (!stream);
+ message("user_start: Successfully opened the serial driver\n");
+
+ /* Then say hello -- forever */
+
+ for (;;)
{
- printf("ERROR: Failed to open /dev/ttyUSB0: %d\n", errno);
- return 2;
+ message("user_start: Saying hello\n");
+ ret = fprintf(stream, "Hello, World!!\n");
+ if (ret < 0)
+ {
+ message("user_start: ERROR: fprintf failed: %d\n", errno);
+ fclose(stream);
+ return 3;
+ }
+
+ ret = fflush(stream);
+ if (ret < 0)
+ {
+ message("user_start: ERROR: fflush failed: %d\n", errno);
+ fclose(stream);
+ return 4;
+ }
+
+ message("user_start: Waiting\n");
+ sleep(5);
}
- /* Then say hello */
+ /* Won't get here, but if we did this what we would have to do */
- fprintf(stream, "Hello, World!!\n");
- fflush(stream);
fclose(stream);
return 0;
}