changeset 10744:cb84962bffbf octave-forge

serial: better error reporting
author eandrius
date Wed, 29 Aug 2012 21:57:44 +0000
parents b62b18db7a3a
children e71ca4bc3de2
files main/serial/src/serial.cc main/serial/src/srl_baudrate.cc main/serial/src/srl_bytesize.cc main/serial/src/srl_close.cc main/serial/src/srl_flush.cc main/serial/src/srl_parity.cc main/serial/src/srl_read.cc main/serial/src/srl_stopbits.cc main/serial/src/srl_timeout.cc main/serial/src/srl_write.cc
diffstat 10 files changed, 77 insertions(+), 116 deletions(-) [+]
line wrap: on
line diff
--- a/main/serial/src/serial.cc	Wed Aug 29 19:31:12 2012 +0000
+++ b/main/serial/src/serial.cc	Wed Aug 29 21:57:44 2012 +0000
@@ -54,8 +54,12 @@
 octave_serial::octave_serial(string path, int flags)
 {
     this->fd = open(path.c_str(), flags, 0);
-    tcgetattr(this->fd, &this->config);
-    this->blocking_read = true;
+    
+    if (this->fd > 0)
+    {
+        tcgetattr(this->fd, &this->config);
+        this->blocking_read = true;
+    }
 }
 
 octave_serial::~octave_serial()
@@ -82,21 +86,20 @@
 // PKG_ADD: autoload ("serial", "serial.oct");
 DEFUN_DLD (serial, args, nargout, "Hello World Help String")
 {
+#ifdef __WIN32__
+    error("serial: Windows platform support is not yet implemented, go away...");
+    return octave_value();
+#endif
+
     int nargin = args.length();
 
     // Do not open interface if return value is not assigned
     if (nargout != 1)
     {
-        error("serial: serial() function has one mandatory output argument");
+        print_usage();
         return octave_value();
     }
-
-
-#ifdef __WIN32__
-    error("serial: Windows platform support is not yet implemented, go away...");
-    return octave_value();
-#endif
-
+    
     // Default values
     string path("/dev/ttyUSB0");
     unsigned int baud_rate = 115200;
@@ -123,7 +126,7 @@
         }
         else
         {
-            error("serial: 1st argument must be an interface path of type string...");
+            print_usage();
             return octave_value();
         }
 
@@ -139,7 +142,7 @@
         }
         else
         {
-            error("serial: 2nd argument must be an interface baud rate of type integer...");
+            print_usage();
             return octave_value();
         }
     }
@@ -152,7 +155,7 @@
         }
         else
         {
-            error("serial: 3rd argument must be an timeout of type integer...");
+            print_usage();
             return octave_value();
         }
     }
@@ -162,10 +165,10 @@
 
     if (retval->srl_get_fd() < 0)
     {
-        error("serial: Error opening the interface...");
+        error("serial: Error opening the interface: %s\n", strerror(errno));
         return octave_value();
     }
-
+    
     retval->srl_baudrate(baud_rate);
     
     if (timeout >= 0) {
--- a/main/serial/src/srl_baudrate.cc	Wed Aug 29 19:31:12 2012 +0000
+++ b/main/serial/src/srl_baudrate.cc	Wed Aug 29 21:57:44 2012 +0000
@@ -115,7 +115,9 @@
     case 230400:
         baud_rate = B230400; break;
     default:
-        error("srl_baudrate: currently only standard baud rates are supported...");
+        error("srl_baudrate: currently only 0, 50, 75, 110, \
+                134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, \
+                9600 19200, 38400, 5760, 115200 and 230400 baud rates are supported...");
         return false;
     }
 
@@ -123,7 +125,7 @@
     cfsetospeed(&this->config, baud_rate);
 
     if (tcsetattr(this->srl_get_fd(), TCSANOW, &this->config) < 0) {
-        error("srl_baudrate: error setting baud rate...");
+        error("srl_baudrate: error setting baud rate: %s\n", strerror(errno));
         return false;
     }
 
--- a/main/serial/src/srl_bytesize.cc	Wed Aug 29 19:31:12 2012 +0000
+++ b/main/serial/src/srl_bytesize.cc	Wed Aug 29 21:57:44 2012 +0000
@@ -38,15 +38,9 @@
 // PKG_ADD: autoload ("srl_bytesize", "serial.oct");
 DEFUN_DLD (srl_bytesize, args, nargout, "Hello World Help String")
 {	
-    if (args.length() < 1 || args.length() > 2)
+    if (args.length() < 1 || args.length() > 2 || args(0).type_id() != octave_serial::static_type_id())
     {
-        error("srl_bytesize: expecting one or two arguments...");
-        return octave_value(-1);
-    }
-
-    if (args(0).type_id() != octave_serial::static_type_id())
-    {
-        error("srl_bytesize: expecting first argument of type octave_serial...");
+        print_usage();
         return octave_value(-1);
     }
 
@@ -55,7 +49,7 @@
     {
         if ( !(args(1).is_integer_type() || args(1).is_float_type()) )
         {
-            error("srl_bytesize: expecting second argument of type integer...");
+            print_usage();
             return octave_value(-1);
         }
 
@@ -106,7 +100,7 @@
     this->config.c_cflag |= c_bytesize;
 
     if (tcsetattr(this->srl_get_fd(), TCSANOW, &this->config) < 0) {
-        error("srl_bytesize: error setting byte size...");
+        error("srl_bytesize: error setting byte size: %s\n", strerror(errno));
         return false;
     }
 
--- a/main/serial/src/srl_close.cc	Wed Aug 29 19:31:12 2012 +0000
+++ b/main/serial/src/srl_close.cc	Wed Aug 29 21:57:44 2012 +0000
@@ -38,18 +38,12 @@
 // PKG_ADD: autoload ("srl_close", "serial.oct");
 DEFUN_DLD (srl_close, args, nargout, "Hello World Help String")
 {
-    if (args.length() != 1)
+    if (args.length() != 1 || args(0).type_id() != octave_serial::static_type_id())
     {
-        error("srl_close: expecting one argument...");
+        print_usage();
         return octave_value(-1);
     }
-
-    if (args(0).type_id() != octave_serial::static_type_id())
-    {
-        error("srl_close: expecting first argument of type octave_serial...");
-        return octave_value(-1);
-    }
-
+    
     octave_serial* serial = NULL;
 
     const octave_base_value& rep = args(0).get_rep();
--- a/main/serial/src/srl_flush.cc	Wed Aug 29 19:31:12 2012 +0000
+++ b/main/serial/src/srl_flush.cc	Wed Aug 29 21:57:44 2012 +0000
@@ -36,6 +36,37 @@
 #include "serial.h"
 
 // PKG_ADD: autoload ("srl_flush", "serial.oct");
+DEFUN_DLD (srl_flush, args, nargout, "Hello World Help String")
+{
+    int queue_selector = 2; // Input and Output
+
+    if (args.length() < 1 || args.length() > 2 || args(0).type_id() != octave_serial::static_type_id()) 
+    {
+        print_usage();
+        return octave_value(-1);
+    }
+
+    if (args.length() > 1)
+    {
+        if (!(args(1).is_integer_type() || args(1).is_float_type()))
+        {
+            print_usage();
+            return octave_value(-1);
+        }
+
+        queue_selector = args(1).int_value();
+    }
+
+    octave_serial* serial = NULL;
+
+    const octave_base_value& rep = args(0).get_rep();
+    serial = &((octave_serial &)rep);
+
+    serial->srl_flush(queue_selector);
+
+    return octave_value();
+}
+
 int octave_serial::srl_flush(unsigned short queue_selector)
 {
     /*
@@ -57,43 +88,4 @@
     }
 
     return ::tcflush(this->srl_get_fd(), flag);
-}
-
-// PKG_ADD: autoload ("srl_flush", "serial.oct");
-DEFUN_DLD (srl_flush, args, nargout, "Hello World Help String")
-{
-    int queue_selector = 2; // Input and Output
-
-    if (args.length() < 1 || args.length() > 2) 
-    {
-        error("srl_write: expecting one or two arguments...");
-        return octave_value(-1);
-    }
-
-    if (args(0).type_id() != octave_serial::static_type_id())
-    {
-        error("srl_write: expecting first argument of type octave_serial...");
-        return octave_value(-1);
-    }
-
-
-    if (args.length() > 1)
-    {
-        if (!(args(1).is_integer_type() || args(1).is_float_type()))
-        {
-            error("srl_write: expecting second argument of type integer...");
-            return octave_value(-1);
-        }
-
-        queue_selector = args(1).int_value();
-    }
-
-    octave_serial* serial = NULL;
-
-    const octave_base_value& rep = args(0).get_rep();
-    serial = &((octave_serial &)rep);
-
-    serial->srl_flush(queue_selector);
-
-    return octave_value();
-}
+}
\ No newline at end of file
--- a/main/serial/src/srl_parity.cc	Wed Aug 29 19:31:12 2012 +0000
+++ b/main/serial/src/srl_parity.cc	Wed Aug 29 21:57:44 2012 +0000
@@ -38,15 +38,9 @@
 // PKG_ADD: autoload ("srl_parity", "serial.oct");
 DEFUN_DLD (srl_parity, args, nargout, "Hello World Help String")
 {
-    if (args.length() < 1 || args.length() > 2)
+    if (args.length() < 1 || args.length() > 2 || args(0).type_id() != octave_serial::static_type_id())
     {
-        error("srl_parity: expecting one or two arguments...");
-        return octave_value(-1);
-    }
-
-    if (args(0).type_id() != octave_serial::static_type_id())
-    {
-        error("srl_parity: expecting first argument of type octave_serial...");
+        print_usage();
         return octave_value(-1);
     }
 
@@ -55,7 +49,7 @@
     {
         if ( !(args(1).is_string()) )
         {
-            error("srl_parity: expecting second argument of type string...");
+            print_usage();
             return octave_value(-1);
         }
 
@@ -115,7 +109,7 @@
     }
 
     if (tcsetattr(this->srl_get_fd(), TCSANOW, &this->config) < 0) {
-        error("srl_parity: error setting parity...");
+        error("srl_parity: error setting parity: %s\n", strerror(errno));
         return false;
     }
 
--- a/main/serial/src/srl_read.cc	Wed Aug 29 19:31:12 2012 +0000
+++ b/main/serial/src/srl_read.cc	Wed Aug 29 21:57:44 2012 +0000
@@ -39,15 +39,9 @@
 // PKG_ADD: autoload ("srl_read", "serial.oct");
 DEFUN_DLD (srl_read, args, nargout, "Hello World Help String")
 {
-    if (args.length() < 1 || args.length() > 2)
+    if (args.length() < 1 || args.length() > 2 || args(0).type_id() != octave_serial::static_type_id())
     {
-        error("srl_read: expecting one or two arguments...");
-        return octave_value(-1);
-    }
-
-    if (args(0).type_id() != octave_serial::static_type_id())
-    {
-        error("srl_read: expecting first argument of type octave_serial...");
+        print_usage();
         return octave_value(-1);
     }
 
@@ -58,7 +52,7 @@
     {
         if ( !(args(1).is_integer_type() || args(1).is_float_type()) )
         {
-            error("srl_read: expecting second argument of type integer...");
+            print_usage();
             return octave_value(-1);
         }
 
@@ -69,7 +63,7 @@
 
     if (buffer == NULL)
     {
-        error("srl_read: cannot allocate requested memory...");
+        error("srl_read: cannot allocate requested memory: %s\n", strerror(errno));
         return octave_value(-1);  
     }
 
--- a/main/serial/src/srl_stopbits.cc	Wed Aug 29 19:31:12 2012 +0000
+++ b/main/serial/src/srl_stopbits.cc	Wed Aug 29 21:57:44 2012 +0000
@@ -38,15 +38,9 @@
 // PKG_ADD: autoload ("srl_stopbits", "serial.oct");
 DEFUN_DLD (srl_stopbits, args, nargout, "Hello World Help String")
 {
-    if (args.length() < 1 || args.length() > 2)
+    if (args.length() < 1 || args.length() > 2 || args(0).type_id() != octave_serial::static_type_id())
     {
-        error("srl_stopbits: expecting one or two arguments...");
-        return octave_value(-1);
-    }
-
-    if (args(0).type_id() != octave_serial::static_type_id())
-    {
-        error("srl_stopbits: expecting first argument of type octave_serial...");
+        print_usage();
         return octave_value(-1);
     }
 
@@ -55,7 +49,7 @@
     {
         if ( !(args(1).is_integer_type() || args(1).is_float_type()) )
         {
-            error("srl_stopbits: expecting second argument of type integer...");
+            print_usage();
             return octave_value(-1);
         }
 
@@ -99,7 +93,7 @@
     }
 
     if (tcsetattr(this->srl_get_fd(), TCSANOW, &this->config) < 0) {
-        error("srl_stopbits: error setting stop bits...");
+        error("srl_stopbits: error setting stop bits: %s\n", strerror(errno));
         return false;
     }
 
--- a/main/serial/src/srl_timeout.cc	Wed Aug 29 19:31:12 2012 +0000
+++ b/main/serial/src/srl_timeout.cc	Wed Aug 29 21:57:44 2012 +0000
@@ -38,15 +38,9 @@
 // PKG_ADD: autoload ("srl_timeout", "serial.oct");
 DEFUN_DLD (srl_timeout, args, nargout, "Hello World Help String")
 {
-    if (args.length() < 1 || args.length() > 2)
+    if (args.length() < 1 || args.length() > 2 || args(0).type_id() != octave_serial::static_type_id())
     {
-        error("srl_timeout: expecting one or two arguments...");
-        return octave_value(-1);
-    }
-
-    if (args(0).type_id() != octave_serial::static_type_id())
-    {
-        error("srl_timeout: expecting first argument of type octave_serial...");
+        print_usage();
         return octave_value(-1);
     }
 
@@ -55,7 +49,7 @@
     {
         if ( !(args(1).is_integer_type() || args(1).is_float_type()) )
         {
-            error("srl_timeout: expecting second argument of type integer...");
+            print_usage();
             return octave_value(-1);
         }
 
--- a/main/serial/src/srl_write.cc	Wed Aug 29 19:31:12 2012 +0000
+++ b/main/serial/src/srl_write.cc	Wed Aug 29 21:57:44 2012 +0000
@@ -39,7 +39,7 @@
 // PKG_ADD: autoload ("srl_write", "serial.oct");
 DEFUN_DLD (srl_write, args, nargout, "Hello World Help String")
 {
-    if (args.length() != 2) || 
+    if (args.length() != 2 || 
         args(0).type_id() != octave_serial::static_type_id() || 
         !args(1).is_string())
     {