Mercurial > forge
changeset 10749:04e5e5c740c1 octave-forge
serial: texinfo help strings
author | eandrius |
---|---|
date | Thu, 30 Aug 2012 22:17:16 +0000 |
parents | c219c6c52b6e |
children | 9b8897eea9c3 |
files | main/serial/src/serial.cc main/serial/src/serial.h 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 | 11 files changed, 305 insertions(+), 152 deletions(-) [+] |
line wrap: on
line diff
--- a/main/serial/src/serial.cc Thu Aug 30 09:15:20 2012 +0000 +++ b/main/serial/src/serial.cc Thu Aug 30 22:17:16 2012 +0000 @@ -13,16 +13,12 @@ // You should have received a copy of the GNU General Public License // along with this program; if not, see <http://www.gnu.org/licenses/>. -// TODO: Include more detailed error messages (perror?) // TODO: Implement Flow Control // TODO: Implement H/W handshaking -// TODO: Implement read timeout // TODO: Check if interface is opened first #include <octave/oct.h> #include <octave/ov-int32.h> -//#include <octave/ops.h> -//#include <octave/ov-typeinfo.h> #include <iostream> #include <string> @@ -64,12 +60,7 @@ octave_serial::~octave_serial() { - this->srl_close(); -} - -int octave_serial::srl_get_fd() -{ - return this->fd; + this->close(); } void octave_serial::print (std::ostream& os, bool pr_as_read_syntax ) const @@ -84,7 +75,18 @@ } // PKG_ADD: autoload ("serial", "serial.oct"); -DEFUN_DLD (serial, args, nargout, "Hello World Help String") +DEFUN_DLD (serial, args, nargout, +"-*- texinfo -*-\n\ +@deftypefn {Loadable Function} {@var{serial} = } serial ([@var{path}], [@var{baudrate}], [@var{timeout}])\n \ +\n\ +Open serial interface.\n \ +\n\ +@var{path} - the interface path of type String. If omitted defaults to '/dev/ttyUSB0'. @*\ +@var{baudrate} - the baudrate of interface. If omitted defaults to 115200. @*\ +@var{timeout} - the interface timeout value. If omitted defaults to blocking call.\n \ +\n\ +The serial() shall return instance of @var{octave_serial} class as the result @var{serial}.\n \ +@end deftypefn") { #ifdef __WIN32__ error("serial: Windows platform support is not yet implemented, go away..."); @@ -163,23 +165,23 @@ // Open the interface octave_serial* retval = new octave_serial(path, oflags); - if (retval->srl_get_fd() < 0) + if (retval->get_fd() < 0) { error("serial: Error opening the interface: %s\n", strerror(errno)); return octave_value(); } - retval->srl_baudrate(baud_rate); + retval->set_baudrate(baud_rate); if (timeout >= 0) { - retval->srl_timeout(timeout); + retval->set_timeout(timeout); } - retval->srl_parity(parity); - retval->srl_bytesize(bytesize); - retval->srl_stopbits(stopbits); + retval->set_parity(parity); + retval->set_bytesize(bytesize); + retval->set_stopbits(stopbits); - retval->srl_flush(); + //retval->flush(2); return octave_value(retval); }
--- a/main/serial/src/serial.h Thu Aug 30 09:15:20 2012 +0000 +++ b/main/serial/src/serial.h Thu Aug 30 22:17:16 2012 +0000 @@ -18,8 +18,6 @@ #include <octave/oct.h> #include <octave/ov-int32.h> -//#include <octave/ops.h> -//#include <octave/ov-typeinfo.h> #include <string> @@ -35,30 +33,36 @@ octave_serial(string, int); ~octave_serial(); - int srl_get_fd(); + int write(string); + int write(unsigned char*, int); + + int read(char *, unsigned int); + + int close(); - int srl_write(string); - int srl_write(unsigned char*, int); - int srl_read(char *, unsigned int); - int srl_close(); + int flush(unsigned short); - int srl_flush(unsigned short); - int srl_flush(); + int set_timeout(short); + int get_timeout(); + + int set_baudrate(unsigned int); + int get_baudrate(); + + int set_bytesize(unsigned short); + int get_bytesize(); + + int set_parity(string); + string get_parity(); + + int set_stopbits(unsigned short); + int get_stopbits(); - int srl_timeout(short); - int srl_baudrate(unsigned int); - int srl_bytesize(unsigned short); - int srl_parity(string); - int srl_stopbits(unsigned short); - + int get_fd() { return this->fd; } // Overloaded base functions - double serial_value() const - { - return (double)this->fd; - } + double serial_value() const { return (double)this->fd; } - virtual double scalar_value (bool frc_str_conv = false) const + virtual double scalar_value (bool frc_str_conv = false) const { return (double)this->fd; }
--- a/main/serial/src/srl_baudrate.cc Thu Aug 30 09:15:20 2012 +0000 +++ b/main/serial/src/srl_baudrate.cc Thu Aug 30 22:17:16 2012 +0000 @@ -36,7 +36,20 @@ #include "serial.h" // PKG_ADD: autoload ("srl_baudrate", "serial.oct"); -DEFUN_DLD (srl_baudrate, args, nargout, "Hello World Help String") +DEFUN_DLD (srl_baudrate, args, nargout, +"-*- texinfo -*-\n\ +@deftypefn {Loadable Function} {} srl_baudrate (@var{serial}, @var{baudrate})\n \ +@deftypefnx {Loadable Function} {@var{br} = } srl_baudrate (@var{serial})\n \ +\n\ +Set new or get existing serial interface baudrate parameter. Only standard values are supported.\n \ +\n\ +@var{serial} - instance of @var{octave_serial} class.@*\ +@var{baudrate} - the baudrate value used. Supported values: 0, 50, 75, 110, \ +134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600 19200, 38400, 57600, \ +115200 and 230400.\n \ +\n\ +If @var{baudrate} parameter is omitted, the srl_baudrate() shall return current baudrate value as the result @var{br}.\n \ +@end deftypefn") { if (args.length() < 1 || args.length() > 2 || args(0).type_id() != octave_serial::static_type_id()) @@ -44,6 +57,11 @@ print_usage(); return octave_value(-1); } + + octave_serial* serial = NULL; + + const octave_base_value& rep = args(0).get_rep(); + serial = &((octave_serial &)rep); // Setting new baudrate if (args.length() > 1) @@ -54,23 +72,16 @@ return octave_value(-1); } - octave_serial* serial = NULL; - - const octave_base_value& rep = args(0).get_rep(); - serial = &((octave_serial &)rep); - - serial->srl_baudrate(args(1).int_value()); + serial->set_baudrate(args(1).int_value()); return octave_value(); } // Returning current baud rate - // TODO: return current baudrate - - return octave_value(-115200); + return octave_value(serial->get_baudrate()); } -int octave_serial::srl_baudrate(unsigned int baud) +int octave_serial::set_baudrate(unsigned int baud) { speed_t baud_rate = 0; @@ -117,17 +128,66 @@ default: 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..."); + 9600 19200, 38400, 57600, 115200 and 230400 baud rates are supported..."); return false; } cfsetispeed(&this->config, baud_rate); cfsetospeed(&this->config, baud_rate); - if (tcsetattr(this->srl_get_fd(), TCSANOW, &this->config) < 0) { + if (tcsetattr(this->get_fd(), TCSANOW, &this->config) < 0) { error("srl_baudrate: error setting baud rate: %s\n", strerror(errno)); return false; } return true; +} + +int octave_serial::get_baudrate() +{ + int retval = -1; + + speed_t baudrate = cfgetispeed(&this->config); + + if (baudrate == B0) + retval = 0; + else if (baudrate == B50) + retval = 50; + else if (baudrate == B75) + retval = 75; + else if (baudrate == B110) + retval = 110; + else if (baudrate == B134) + retval = 134; + else if (baudrate == B150) + retval = 150; + else if (baudrate == B200) + retval = 200; + else if (baudrate == B300) + retval = 300; + else if (baudrate == B600) + retval = 600; + else if (baudrate == B1200) + retval = 1200; + else if (baudrate == B1800) + retval = 1800; + else if (baudrate == B2400) + retval = 2400; + else if (baudrate == B4800) + retval = 4800; + else if (baudrate == B9600) + retval = 9600; + else if (baudrate == B19200) + retval = 19200; + else if (baudrate == B38400) + retval = 38400; + else if (baudrate == B57600) + retval = 57600; + else if (baudrate == B115200) + retval = 115200; + else if (baudrate == B230400) + retval = 230400; + + return retval; + } \ No newline at end of file
--- a/main/serial/src/srl_bytesize.cc Thu Aug 30 09:15:20 2012 +0000 +++ b/main/serial/src/srl_bytesize.cc Thu Aug 30 22:17:16 2012 +0000 @@ -36,13 +36,29 @@ #include "serial.h" // PKG_ADD: autoload ("srl_bytesize", "serial.oct"); -DEFUN_DLD (srl_bytesize, args, nargout, "Hello World Help String") +DEFUN_DLD (srl_bytesize, args, nargout, +"-*- texinfo -*-\n\ +@deftypefn {Loadable Function} {} srl_bytesize (@var{serial}, @var{bsize})\n \ +@deftypefnx {Loadable Function} {@var{bs} = } srl_bytesize (@var{serial})\n \ +\n\ +Set new or get existing serial interface byte size parameter.\n \ +\n\ +@var{serial} - instance of @var{octave_serial} class.@*\ +@var{bsize} - byte size of type Integer. Supported values: 5/6/7/8.\n \ +\n\ +If @var{bsize} parameter is omitted, the srl_bytesize() shall return current byte size value or in case of unsupported setting -1, as the result @var{bs}.\n \ +@end deftypefn") { if (args.length() < 1 || args.length() > 2 || args(0).type_id() != octave_serial::static_type_id()) { print_usage(); return octave_value(-1); } + + octave_serial* serial = NULL; + + const octave_base_value& rep = args(0).get_rep(); + serial = &((octave_serial &)rep); // Setting new byte size if (args.length() > 1) @@ -53,23 +69,16 @@ return octave_value(-1); } - octave_serial* serial = NULL; - - const octave_base_value& rep = args(0).get_rep(); - serial = &((octave_serial &)rep); - - serial->srl_bytesize(args(1).int_value()); + serial->set_bytesize(args(1).int_value()); return octave_value(); } // Returning current byte size - // TODO: return current byte size - - return octave_value(-8); + return octave_value(serial->get_bytesize()); } -int octave_serial::srl_bytesize(unsigned short bytesize) +int octave_serial::set_bytesize(unsigned short bytesize) { tcflag_t c_bytesize = 0; @@ -85,24 +94,32 @@ return false; } - // Clear previous config flags - /* - BITMASK_CLEAR(this->config.c_cflag, CS5); - BITMASK_CLEAR(this->config.c_cflag, CS6); - BITMASK_CLEAR(this->config.c_cflag, CS7); - BITMASK_CLEAR(this->config.c_cflag, CS8); - */ - // Clear bitmask CSIZE BITMASK_CLEAR(this->config.c_cflag, CSIZE); // Apply new - this->config.c_cflag |= c_bytesize; + BITMASK_SET(this->config.c_cflag, c_bytesize); - if (tcsetattr(this->srl_get_fd(), TCSANOW, &this->config) < 0) { + if (tcsetattr(this->get_fd(), TCSANOW, &this->config) < 0) { error("srl_bytesize: error setting byte size: %s\n", strerror(errno)); return false; } return true; +} + +int octave_serial::get_bytesize() +{ + int retval = -1; + + if (BITMASK_CHECK(this->config.c_cflag, CS5)) + retval = 5; + else if (BITMASK_CHECK(this->config.c_cflag, CS6)) + retval = 6; + else if (BITMASK_CHECK(this->config.c_cflag, CS7)) + retval = 7; + else if (BITMASK_CHECK(this->config.c_cflag, CS8)) + retval = 8; + + return retval; } \ No newline at end of file
--- a/main/serial/src/srl_close.cc Thu Aug 30 09:15:20 2012 +0000 +++ b/main/serial/src/srl_close.cc Thu Aug 30 22:17:16 2012 +0000 @@ -15,8 +15,6 @@ #include <octave/oct.h> #include <octave/ov-int32.h> -//#include <octave/ops.h> -//#include <octave/ov-typeinfo.h> #include <iostream> #include <string> @@ -36,7 +34,14 @@ #include "serial.h" // PKG_ADD: autoload ("srl_close", "serial.oct"); -DEFUN_DLD (srl_close, args, nargout, "Hello World Help String") +DEFUN_DLD (srl_close, args, nargout, +"-*- texinfo -*-\n\ +@deftypefn {Loadable Function} {} srl_close (@var{serial})\n \ +\n\ +Close the interface and release a file descriptor.\n \ +\n\ +@var{serial} - instance of @var{octave_serial} class.@*\ +@end deftypefn") { if (args.length() != 1 || args(0).type_id() != octave_serial::static_type_id()) { @@ -49,14 +54,14 @@ const octave_base_value& rep = args(0).get_rep(); serial = &((octave_serial &)rep); - serial->srl_close(); + serial->close(); return octave_value(); } -int octave_serial::srl_close() +int octave_serial::close() { - int retval = ::close(this->srl_get_fd()); + int retval = ::close(this->get_fd()); this->fd = -1; return retval; } \ No newline at end of file
--- a/main/serial/src/srl_flush.cc Thu Aug 30 09:15:20 2012 +0000 +++ b/main/serial/src/srl_flush.cc Thu Aug 30 22:17:16 2012 +0000 @@ -15,8 +15,6 @@ #include <octave/oct.h> #include <octave/ov-int32.h> -//#include <octave/ops.h> -//#include <octave/ov-typeinfo.h> #include <iostream> #include <string> @@ -36,7 +34,18 @@ #include "serial.h" // PKG_ADD: autoload ("srl_flush", "serial.oct"); -DEFUN_DLD (srl_flush, args, nargout, "Hello World Help String") +DEFUN_DLD (srl_flush, args, nargout, +"-*- texinfo -*-\n\ +@deftypefn {Loadable Function} {} srl_flush (@var{serial}, [@var{q}])\n \ +\n\ +Flush the pending input/output.\n \ +\n\ +@var{serial} - instance of @var{octave_serial} class.@*\ +@var{q} - queue selector of type Integer. Supported values: 0 - flush untransmitted output, \ +1 - flush pending input, 2 - flush both pending input and untransmitted output.\n \ +\n\ +If @var{q} parameter is omitted, the srl_flush() shall flush both, input and output buffers.\n \ +@end deftypefn") { int queue_selector = 2; // Input and Output @@ -62,12 +71,12 @@ const octave_base_value& rep = args(0).get_rep(); serial = &((octave_serial &)rep); - serial->srl_flush(queue_selector); + serial->flush(queue_selector); return octave_value(); } -int octave_serial::srl_flush(unsigned short queue_selector) +int octave_serial::flush(unsigned short queue_selector) { /* * TCIOFLUSH Flush both pending input and untransmitted output. @@ -87,10 +96,5 @@ return false; } - return ::tcflush(this->srl_get_fd(), flag); -} - -int octave_serial::srl_flush() -{ - return srl_flush(2); + return ::tcflush(this->get_fd(), flag); } \ No newline at end of file
--- a/main/serial/src/srl_parity.cc Thu Aug 30 09:15:20 2012 +0000 +++ b/main/serial/src/srl_parity.cc Thu Aug 30 22:17:16 2012 +0000 @@ -36,13 +36,29 @@ #include "serial.h" // PKG_ADD: autoload ("srl_parity", "serial.oct"); -DEFUN_DLD (srl_parity, args, nargout, "Hello World Help String") +DEFUN_DLD (srl_parity, args, nargout, +"-*- texinfo -*-\n\ +@deftypefn {Loadable Function} {} srl_parity (@var{serial}, @var{parity})\n \ +@deftypefnx {Loadable Function} {@var{p} = } srl_parity (@var{serial})\n \ +\n\ +Set new or get existing serial interface parity parameter. Even/Odd/None values are supported.\n \ +\n\ +@var{serial} - instance of @var{octave_serial} class.@*\ +@var{parity} - parity value of type String. Supported values: Even/Odd/None (case insensitive, can be abbreviated to the first letter only).\n \ +\n\ +If @var{parity} parameter is omitted, the srl_parity() shall return current parity value as the result @var{p}.\n \ +@end deftypefn") { if (args.length() < 1 || args.length() > 2 || args(0).type_id() != octave_serial::static_type_id()) { print_usage(); return octave_value(-1); } + + octave_serial* serial = NULL; + + const octave_base_value& rep = args(0).get_rep(); + serial = &((octave_serial &)rep); // Setting new parity if (args.length() > 1) @@ -53,23 +69,16 @@ return octave_value(-1); } - octave_serial* serial = NULL; - - const octave_base_value& rep = args(0).get_rep(); - serial = &((octave_serial &)rep); - - serial->srl_parity(args(1).string_value()); + serial->set_parity(args(1).string_value()); return octave_value(); } // Returning current parity - // TODO: return current parity - - return octave_value("-N"); + return octave_value(serial->get_parity()); } -int octave_serial::srl_parity(string parity) +int octave_serial::set_parity(string parity) { // Convert string to lowercase std::transform(parity.begin(), parity.end(), parity.begin(), ::tolower); @@ -108,10 +117,20 @@ return false; } - if (tcsetattr(this->srl_get_fd(), TCSANOW, &this->config) < 0) { + if (tcsetattr(this->get_fd(), TCSANOW, &this->config) < 0) { error("srl_parity: error setting parity: %s\n", strerror(errno)); return false; } return true; +} + +string octave_serial::get_parity() +{ + if (!BITMASK_CHECK(this->config.c_cflag, PARENB)) + return "None"; + else if (BITMASK_CHECK(this->config.c_cflag, PARODD)) + return "Odd"; + else + return "Even"; } \ No newline at end of file
--- a/main/serial/src/srl_read.cc Thu Aug 30 09:15:20 2012 +0000 +++ b/main/serial/src/srl_read.cc Thu Aug 30 22:17:16 2012 +0000 @@ -16,8 +16,6 @@ #include <octave/oct.h> #include <octave/ov-int32.h> #include <octave/ov-uint8.h> -//#include <octave/ops.h> -//#include <octave/ov-typeinfo.h> #include <iostream> #include <string> @@ -37,7 +35,17 @@ #include "serial.h" // PKG_ADD: autoload ("srl_read", "serial.oct"); -DEFUN_DLD (srl_read, args, nargout, "Hello World Help String") +DEFUN_DLD (srl_read, args, nargout, +"-*- texinfo -*-\n\ +@deftypefn {Loadable Function} {[@var{data}, @var{count}] = } srl_read (@var{serial}, @var{n})\n \ +\n\ +Read from serial interface.\n \ +\n\ +@var{serial} - instance of @var{octave_serial} class.@*\ +@var{n} - number of bytes to attempt to read of type Integer.\n \ +\n\ +The srl_read() shall return number of bytes successfully read in @var{count} as Integer and the bytes themselves in @var{data} as uint8 array.\n \ +@end deftypefn") { if (args.length() < 1 || args.length() > 2 || args(0).type_id() != octave_serial::static_type_id()) { @@ -77,7 +85,7 @@ // While buffer not full and not timeout while (buffer_read < buffer_len && read_retval != 0) { - read_retval = serial->srl_read(buffer + buffer_read, buffer_len - buffer_read); + read_retval = serial->read(buffer + buffer_read, buffer_len - buffer_read); buffer_read += read_retval; } @@ -86,9 +94,7 @@ // TODO: clean this up for (int i = 0; i < buffer_read; i++) - { data(i) = buffer[i]; - } return_list(1) = buffer_read; return_list(0) = data; @@ -98,7 +104,7 @@ return return_list; } -int octave_serial::srl_read(char *buf, unsigned int len) +int octave_serial::read(char *buf, unsigned int len) { - return ::read(srl_get_fd(), buf, len); + return ::read(get_fd(), buf, len); } \ No newline at end of file
--- a/main/serial/src/srl_stopbits.cc Thu Aug 30 09:15:20 2012 +0000 +++ b/main/serial/src/srl_stopbits.cc Thu Aug 30 22:17:16 2012 +0000 @@ -15,8 +15,6 @@ #include <octave/oct.h> #include <octave/ov-int32.h> -//#include <octave/ops.h> -//#include <octave/ov-typeinfo.h> #include <iostream> #include <string> @@ -36,7 +34,18 @@ #include "serial.h" // PKG_ADD: autoload ("srl_stopbits", "serial.oct"); -DEFUN_DLD (srl_stopbits, args, nargout, "Hello World Help String") +DEFUN_DLD (srl_stopbits, args, nargout, +"-*- texinfo -*-\n\ +@deftypefn {Loadable Function} {} srl_stopbits (@var{serial}, @var{stopb})\n \ +@deftypefnx {Loadable Function} {@var{sb} = } srl_stopbits (@var{serial})\n \ +\n\ +Set new or get existing serial interface stop bits parameter. Only 1 or 2 stop bits are supported.\n \ +\n\ +@var{serial} - instance of @var{octave_serial} class.@*\ +@var{stopb} - number of stop bits used. Supported values: 1, 2.\n \ +\n\ +If @var{stopb} parameter is omitted, the srl_stopbits() shall return current stop bits value as the result @var{sb}.\n \ +@end deftypefn") { if (args.length() < 1 || args.length() > 2 || args(0).type_id() != octave_serial::static_type_id()) { @@ -44,6 +53,11 @@ return octave_value(-1); } + octave_serial* serial = NULL; + + const octave_base_value& rep = args(0).get_rep(); + serial = &((octave_serial &)rep); + // Setting new stop bits if (args.length() > 1) { @@ -53,24 +67,16 @@ return octave_value(-1); } - octave_serial* serial = NULL; - - const octave_base_value& rep = args(0).get_rep(); - serial = &((octave_serial &)rep); - - serial->srl_stopbits(args(1).int_value()); + serial->set_stopbits(args(1).int_value()); return octave_value(); } // Returning current stop bits - - // TODO: return current stop bits - - return octave_value(-1); + return octave_value(serial->get_stopbits()); } -int octave_serial::srl_stopbits(unsigned short stopbits) +int octave_serial::set_stopbits(unsigned short stopbits) { /* * CSTOPB Send two stop bits, else one. @@ -92,10 +98,18 @@ return false; } - if (tcsetattr(this->srl_get_fd(), TCSANOW, &this->config) < 0) { + if (tcsetattr(this->get_fd(), TCSANOW, &this->config) < 0) { error("srl_stopbits: error setting stop bits: %s\n", strerror(errno)); return false; } return true; +} + +int octave_serial::get_stopbits() +{ + if (BITMASK_CHECK(this->config.c_cflag, CSTOPB)) + return 2; + else + return 1; } \ No newline at end of file
--- a/main/serial/src/srl_timeout.cc Thu Aug 30 09:15:20 2012 +0000 +++ b/main/serial/src/srl_timeout.cc Thu Aug 30 22:17:16 2012 +0000 @@ -15,8 +15,6 @@ #include <octave/oct.h> #include <octave/ov-int32.h> -//#include <octave/ops.h> -//#include <octave/ov-typeinfo.h> #include <iostream> #include <string> @@ -36,13 +34,29 @@ #include "serial.h" // PKG_ADD: autoload ("srl_timeout", "serial.oct"); -DEFUN_DLD (srl_timeout, args, nargout, "Hello World Help String") +DEFUN_DLD (srl_timeout, args, nargout, +"-*- texinfo -*-\n\ +@deftypefn {Loadable Function} {} srl_timeout (@var{serial}, @var{timeout})\n \ +@deftypefnx {Loadable Function} {@var{t} = } srl_timeout (@var{serial})\n \ +\n\ +Set new or get existing serial interface timeout parameter used for srl_read() requests. The timeout value is specified in tenths of a second.\n \ +\n\ +@var{serial} - instance of @var{octave_serial} class.@*\ +@var{timeout} - srl_read() timeout value in tenths of a second. Maximum value of 255 (i.e. 25.5 seconds).\n \ +\n\ +If @var{timeout} parameter is omitted, the srl_timeout() shall return current timeout value as the result @var{t}.\n \ +@end deftypefn") { if (args.length() < 1 || args.length() > 2 || args(0).type_id() != octave_serial::static_type_id()) { print_usage(); return octave_value(-1); } + + octave_serial* serial = NULL; + + const octave_base_value& rep = args(0).get_rep(); + serial = &((octave_serial &)rep); // Setting new timeout if (args.length() > 1) @@ -53,24 +67,16 @@ return octave_value(-1); } - octave_serial* serial = NULL; + serial->set_timeout(args(1).int_value()); - const octave_base_value& rep = args(0).get_rep(); - serial = &((octave_serial &)rep); - - serial->srl_timeout(args(1).int_value()); - - return octave_value(); + return octave_value(); // Should it return by default? } // Returning current timeout - - // TODO: return current timeout - - return octave_value(0); + return octave_value(serial->get_timeout()); } -int octave_serial::srl_timeout(short timeout) +int octave_serial::set_timeout(short timeout) { if (timeout < 0 || timeout > 255) { @@ -87,18 +93,26 @@ this->config.c_cc[VMIN] = 1; this->config.c_cc[VTIME] = 0; } - */ - + */ + // Enable timeout, disable blocking read this->blocking_read = false; BITMASK_CLEAR(this->config.c_lflag, ICANON); // Set non-canonical mode this->config.c_cc[VMIN] = 0; this->config.c_cc[VTIME] = (unsigned) timeout; // Set timeout of 'timeout * 10' seconds - - if (tcsetattr(this->srl_get_fd(), TCSANOW, &this->config) < 0) { + + if (tcsetattr(this->get_fd(), TCSANOW, &this->config) < 0) { error("srl_timeout: error setting stop bits..."); return false; } return true; +} + +int octave_serial::get_timeout() +{ + if (blocking_read) + return -1; + else + return this->config.c_cc[VTIME]; } \ No newline at end of file
--- a/main/serial/src/srl_write.cc Thu Aug 30 09:15:20 2012 +0000 +++ b/main/serial/src/srl_write.cc Thu Aug 30 22:17:16 2012 +0000 @@ -15,8 +15,6 @@ #include <octave/oct.h> #include <octave/ov-int32.h> -//#include <octave/ops.h> -//#include <octave/ov-typeinfo.h> #include <iostream> #include <string> @@ -36,7 +34,17 @@ #include "serial.h" // PKG_ADD: autoload ("srl_write", "serial.oct"); -DEFUN_DLD (srl_write, args, nargout, "Hello World Help String") +DEFUN_DLD (srl_write, args, nargout, +"-*- texinfo -*-\n\ +@deftypefn {Loadable Function} {@var{n} = } srl_write (@var{serial}, @var{data})\n \ +\n\ +Write data to a serial interface.\n \ +\n\ +@var{serial} - instance of @var{octave_serial} class.@*\ +@var{data} - data to be written to the serial interface. Can be either of String or uint8 type.\n \ +\n\ +Upon successful completion, srl_write() shall return the number of bytes written as the result @var{n}.\n \ +@end deftypefn") { if (args.length() != 2 || args(0).type_id() != octave_serial::static_type_id()) { @@ -52,7 +60,7 @@ if (args(1).is_string()) // String { - retval = serial->srl_write(args(1).string_value()); + retval = serial->write(args(1).string_value()); } else if (args(1).byte_size() == args(1).numel()) // uint8_t { @@ -63,7 +71,7 @@ for (int i = 0; i < data.length(); i++) buf[i] = (unsigned char)data(i); - retval = serial->srl_write(buf, data.length()); + retval = serial->write(buf, data.length()); delete[] buf; } @@ -76,12 +84,12 @@ return octave_value(retval); } -int octave_serial::srl_write(string str) +int octave_serial::write(string str) { - return ::write(srl_get_fd(), str.c_str(), str.length()); + return ::write(get_fd(), str.c_str(), str.length()); } -int octave_serial::srl_write(unsigned char *buf, int len) +int octave_serial::write(unsigned char *buf, int len) { - return ::write(srl_get_fd(), buf, len); + return ::write(get_fd(), buf, len); } \ No newline at end of file