From: <ean...@us...> - 2012-08-07 19:48:38
|
Revision: 10840 http://octave.svn.sourceforge.net/octave/?rev=10840&view=rev Author: eandrius Date: 2012-08-07 19:48:30 +0000 (Tue, 07 Aug 2012) Log Message: ----------- serial: initial release Added Paths: ----------- trunk/octave-forge/main/serial/ trunk/octave-forge/main/serial/COPYING trunk/octave-forge/main/serial/DESCRIPTION trunk/octave-forge/main/serial/INDEX trunk/octave-forge/main/serial/NEWS trunk/octave-forge/main/serial/src/ trunk/octave-forge/main/serial/src/Makefile trunk/octave-forge/main/serial/src/serial.cc trunk/octave-forge/main/serial/src/serial.h trunk/octave-forge/main/serial/src/srl_baudrate.cc trunk/octave-forge/main/serial/src/srl_bytesize.cc trunk/octave-forge/main/serial/src/srl_close.cc trunk/octave-forge/main/serial/src/srl_flush.cc trunk/octave-forge/main/serial/src/srl_parity.cc trunk/octave-forge/main/serial/src/srl_read.cc trunk/octave-forge/main/serial/src/srl_stopbits.cc trunk/octave-forge/main/serial/src/srl_write.cc Added: trunk/octave-forge/main/serial/COPYING =================================================================== --- trunk/octave-forge/main/serial/COPYING (rev 0) +++ trunk/octave-forge/main/serial/COPYING 2012-08-07 19:48:30 UTC (rev 10840) @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. Added: trunk/octave-forge/main/serial/DESCRIPTION =================================================================== --- trunk/octave-forge/main/serial/DESCRIPTION (rev 0) +++ trunk/octave-forge/main/serial/DESCRIPTION 2012-08-07 19:48:30 UTC (rev 10840) @@ -0,0 +1,11 @@ +Name: serial +Version: 0.1.0 +Date: 2012-08-02 +Author: Andrius Sutas <and...@gm...> +Maintainer: Andrius Sutas <and...@gm...> +Title: Serial +Description: Serial functions for communicating from within octave. +Categories: Serial +Depends: octave (>= 3.6.2) +Autoload: no +License: GPLv3+ Added: trunk/octave-forge/main/serial/INDEX =================================================================== --- trunk/octave-forge/main/serial/INDEX (rev 0) +++ trunk/octave-forge/main/serial/INDEX 2012-08-07 19:48:30 UTC (rev 10840) @@ -0,0 +1,11 @@ +serial >> Serial +Serial + serial + srl_write + srl_baudrate + srl_bytesize + srl_flush + srl_parity + srl_stopbits + srl_close + srl_read \ No newline at end of file Added: trunk/octave-forge/main/serial/NEWS =================================================================== --- trunk/octave-forge/main/serial/NEWS (rev 0) +++ trunk/octave-forge/main/serial/NEWS 2012-08-07 19:48:30 UTC (rev 10840) @@ -0,0 +1,15 @@ +Summary of important user-visible changes for serial 0.1.0: +------------------------------------------------------------------- + + ** Initial release + + ** The following functions are new: + serial + srl_write + srl_baudrate + srl_bytesize + srl_flush + srl_parity + srl_stopbits + srl_close + srl_read Added: trunk/octave-forge/main/serial/src/Makefile =================================================================== --- trunk/octave-forge/main/serial/src/Makefile (rev 0) +++ trunk/octave-forge/main/serial/src/Makefile 2012-08-07 19:48:30 UTC (rev 10840) @@ -0,0 +1,17 @@ +OCT = serial.oct +OBJ := serial.o srl_baudrate.o srl_bytesize.o srl_flush.o srl_parity.o srl_stopbits.o srl_write.o srl_close.o srl_read.o + +MKOCTFILE ?= mkoctfile + +all: $(OCT) + +%.oct: $(OBJ) + $(MKOCTFILE) $^ + +%.o: %.cc + $(MKOCTFILE) -c -s $< + +clean: + rm -f *.oct *.o + +.PHONY: all clean Added: trunk/octave-forge/main/serial/src/serial.cc =================================================================== --- trunk/octave-forge/main/serial/src/serial.cc (rev 0) +++ trunk/octave-forge/main/serial/src/serial.cc 2012-08-07 19:48:30 UTC (rev 10840) @@ -0,0 +1,170 @@ +// Copyright (C) 2012 Andrius Sutas <and...@gm...> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// 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 +// TODO: Implement Flow Control +// TODO: Implement read timeout + +#include <octave/oct.h> +#include <octave/ov-int32.h> +//#include <octave/ops.h> +//#include <octave/ov-typeinfo.h> + +#include <iostream> +#include <string> +#include <algorithm> + +#ifndef __WIN32__ +#include <stdio.h> +#include <string.h> +#include <fcntl.h> +#include <errno.h> +#include <termios.h> +#include <unistd.h> +#endif + +using std::string; + +#include "serial.h" + +DEFINE_OCTAVE_ALLOCATOR (octave_serial); +DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_serial, "octave_serial", "octave_serial"); + +static bool type_loaded = false; + +octave_serial::octave_serial() +{ + this->fd = -1; +} + +octave_serial::octave_serial(string path, int flags) +{ + this->fd = open(path.c_str(), flags, 0); + tcgetattr(this->fd, &this->config); +} + +octave_serial::~octave_serial() +{ + this->srl_close(); +} + +int octave_serial::srl_get_fd() +{ + return this->fd; +} + +void octave_serial::print (std::ostream& os, bool pr_as_read_syntax ) const +{ + print_raw (os, pr_as_read_syntax); + newline (os); +} + +void octave_serial::print_raw (std::ostream& os, bool pr_as_read_syntax) const +{ + os << this->fd; +} + +// PKG_ADD: autoload ("serial", "serial.oct"); +DEFUN_DLD (serial, args, nargout, "Hello World Help String") +{ + 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"); + 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; + unsigned short bytesize = 8; + string parity("N"); + unsigned short stopbits = 1; + int oflags = O_RDWR | O_NOCTTY | O_SYNC; + // O_SYNC - All writes immediately effective, no buffering + + if (!type_loaded) + { + octave_serial::register_type(); + type_loaded = true; + } + + // Parse the function arguments + if (args.length() > 0) + { + if (args(0).is_string()) + { + path = args(0).string_value(); + } + else + { + error("serial: 1st argument must be an interface path of type string..."); + return octave_value(); + } + + } + + // is_float_type() is or'ed to allow expression like ("", 123), without user + // having to use ("", int32(123)), as we still only take "int_value" + if (args.length() > 1) + { + if (args(1).is_integer_type() || args(1).is_float_type()) + { + baud_rate = args(1).int_value(); + } + else + { + error("serial: 2nd argument must be an interface baud rate of type integer..."); + return octave_value(); + } + } + + if (args.length() > 2) + { + if (args(2).is_integer_type() || args(2).is_float_type()) + { + oflags = args(2).int_value(); + } + else + { + error("serial: 3rd argument must be an interface flags of type integer..."); + return octave_value(); + } + } + + // Open the interface + octave_serial* retval = new octave_serial(path, oflags); + + if (retval->srl_get_fd() < 0) + { + error("serial: Error opening the interface..."); + return octave_value(); + } + + retval->srl_baudrate(baud_rate); + retval->srl_parity(parity); + retval->srl_bytesize(bytesize); + retval->srl_stopbits(stopbits); + + return octave_value(retval); +} Added: trunk/octave-forge/main/serial/src/serial.h =================================================================== --- trunk/octave-forge/main/serial/src/serial.h (rev 0) +++ trunk/octave-forge/main/serial/src/serial.h 2012-08-07 19:48:30 UTC (rev 10840) @@ -0,0 +1,86 @@ +// Copyright (C) 2012 Andrius Sutas <and...@gm...> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see <http://www.gnu.org/licenses/>. + +#ifndef SERIAL_H +#define SERIAL_H + +#include <octave/oct.h> +#include <octave/ov-int32.h> +//#include <octave/ops.h> +//#include <octave/ov-typeinfo.h> + +#include <string> + +/* a=target variable, b=bit number to act upon 0-n */ +#define BIT_SET(a,b) ((a) |= (1<<(b))) +#define BIT_CLEAR(a,b) ((a) &= ~(1<<(b))) +#define BIT_FLIP(a,b) ((a) ^= (1<<(b))) +#define BIT_CHECK(a,b) ((a) & (1<<(b))) + +#define BITMASK_SET(x,y) ((x) |= (y)) +#define BITMASK_CLEAR(x,y) ((x) &= (~(y))) +#define BITMASK_TOGGLE(x,y) ((x) ^= (y)) +#define BITMASK_CHECK(x,y) ((x) & (y)) + +class octave_serial : public octave_base_value +{ +public: + octave_serial(); + octave_serial(string, int); + ~octave_serial(); + + int srl_get_fd(); + + int srl_write(string); + int srl_read(char *, unsigned int); + int srl_close(); + + int srl_flush(unsigned short); + + int srl_baudrate(unsigned int); + int srl_bytesize(unsigned short); + int srl_parity(string); + int srl_stopbits(unsigned short); + + + // Overloaded base functions + double serial_value() const + { + return (double)this->fd; + } + + virtual double scalar_value (bool frc_str_conv = false) const + { + return (double)this->fd; + } + + void print (std::ostream& os, bool pr_as_read_syntax = false) const; + void print_raw (std::ostream& os, bool pr_as_read_syntax) const; + + // Properties + bool is_constant (void) const { return true;} + bool is_defined (void) const { return true;} + bool print_as_scalar (void) const { return true;} + +private: + int fd; + struct termios config; + + DECLARE_OCTAVE_ALLOCATOR + DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA +}; + + +#endif Added: trunk/octave-forge/main/serial/src/srl_baudrate.cc =================================================================== --- trunk/octave-forge/main/serial/src/srl_baudrate.cc (rev 0) +++ trunk/octave-forge/main/serial/src/srl_baudrate.cc 2012-08-07 19:48:30 UTC (rev 10840) @@ -0,0 +1,137 @@ +#include <octave/oct.h> +#include <octave/ov-int32.h> +//#include <octave/ops.h> +//#include <octave/ov-typeinfo.h> + +// Copyright (C) 2012 Andrius Sutas <and...@gm...> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see <http://www.gnu.org/licenses/>. + +#include <iostream> +#include <string> +#include <algorithm> + +#ifndef __WIN32__ +#include <stdio.h> +#include <string.h> +#include <fcntl.h> +#include <errno.h> +#include <termios.h> +#include <unistd.h> +#endif + +using std::string; + +#include "serial.h" + +// PKG_ADD: autoload ("srl_baudrate", "serial.oct"); +DEFUN_DLD (srl_baudrate, args, nargout, "Hello World Help String") +{ + if (args.length() < 1 || args.length() > 2) + { + error("srl_baudrate: expecting one or two arguments..."); + return octave_value(-1); + } + + + if (args(0).type_id() != octave_serial::static_type_id()) + { + error("srl_baudrate: expecting first argument of type octave_serial..."); + return octave_value(-1); + } + + // Setting new baudrate + if (args.length() > 1) + { + if ( !(args(1).is_integer_type() || args(1).is_float_type()) ) + { + error("srl_baudrate: expecting second argument of type integer..."); + 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()); + + return octave_value(); + } + + // Returning current baud rate + // TODO: return current baudrate + + return octave_value(-115200); +} + +int octave_serial::srl_baudrate(unsigned int baud) +{ + speed_t baud_rate = 0; + + switch (baud) + { + case 0: + baud_rate = B0; break; + case 50: + baud_rate = B50; break; + case 75: + baud_rate = B75; break; + case 110: + baud_rate = B110; break; + case 134: + baud_rate = B134; break; + case 150: + baud_rate = B150; break; + case 200: + baud_rate = B200; break; + case 300: + baud_rate = B300; break; + case 600: + baud_rate = B600; break; + case 1200: + baud_rate = B1200; break; + case 1800: + baud_rate = B1800; break; + case 2400: + baud_rate = B2400; break; + case 4800: + baud_rate = B4800; break; + case 9600: + baud_rate = B9600; break; + case 19200: + baud_rate = B19200; break; + case 38400: + baud_rate = B38400; break; + case 57600: + baud_rate = B57600; break; + case 115200: + baud_rate = B115200; break; + case 230400: + baud_rate = B230400; break; + default: + error("srl_baudrate: currently only standard 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) { + error("srl_baudrate: error setting baud rate..."); + return false; + } + + return true; +} \ No newline at end of file Added: trunk/octave-forge/main/serial/src/srl_bytesize.cc =================================================================== --- trunk/octave-forge/main/serial/src/srl_bytesize.cc (rev 0) +++ trunk/octave-forge/main/serial/src/srl_bytesize.cc 2012-08-07 19:48:30 UTC (rev 10840) @@ -0,0 +1,114 @@ +// Copyright (C) 2012 Andrius Sutas <and...@gm...> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see <http://www.gnu.org/licenses/>. + +#include <octave/oct.h> +#include <octave/ov-int32.h> +//#include <octave/ops.h> +//#include <octave/ov-typeinfo.h> + +#include <iostream> +#include <string> +#include <algorithm> + +#ifndef __WIN32__ +#include <stdio.h> +#include <string.h> +#include <fcntl.h> +#include <errno.h> +#include <termios.h> +#include <unistd.h> +#endif + +using std::string; + +#include "serial.h" + +// PKG_ADD: autoload ("srl_bytesize", "serial.oct"); +DEFUN_DLD (srl_bytesize, args, nargout, "Hello World Help String") +{ + if (args.length() < 1 || args.length() > 2) + { + 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..."); + return octave_value(-1); + } + + // Setting new byte size + if (args.length() > 1) + { + if ( !(args(1).is_integer_type() || args(1).is_float_type()) ) + { + error("srl_bytesize: expecting second argument of type integer..."); + 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()); + + return octave_value(); + } + + // Returning current byte size + // TODO: return current byte size + + return octave_value(-8); +} + +int octave_serial::srl_bytesize(unsigned short bytesize) +{ + tcflag_t c_bytesize = 0; + + switch (bytesize) + { + case 5: c_bytesize = CS5; break; + case 6: c_bytesize = CS6; break; + case 7: c_bytesize = CS7; break; + case 8: c_bytesize = CS8; break; + + default: + error("srl_bytesize: expecting value between [5..8]..."); + 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; + + if (tcsetattr(this->srl_get_fd(), TCSANOW, &this->config) < 0) { + error("srl_bytesize: error setting byte size..."); + return false; + } + + return true; +} \ No newline at end of file Added: trunk/octave-forge/main/serial/src/srl_close.cc =================================================================== --- trunk/octave-forge/main/serial/src/srl_close.cc (rev 0) +++ trunk/octave-forge/main/serial/src/srl_close.cc 2012-08-07 19:48:30 UTC (rev 10840) @@ -0,0 +1,66 @@ +// Copyright (C) 2012 Andrius Sutas <and...@gm...> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see <http://www.gnu.org/licenses/>. + +#include <octave/oct.h> +#include <octave/ov-int32.h> +//#include <octave/ops.h> +//#include <octave/ov-typeinfo.h> + +#include <iostream> +#include <string> +#include <algorithm> + +#ifndef __WIN32__ +#include <stdio.h> +#include <string.h> +#include <fcntl.h> +#include <errno.h> +#include <termios.h> +#include <unistd.h> +#endif + +using std::string; + +#include "serial.h" + +// PKG_ADD: autoload ("srl_close", "serial.oct"); +DEFUN_DLD (srl_close, args, nargout, "Hello World Help String") +{ + if (args.length() != 1) + { + error("srl_close: expecting one argument..."); + 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(); + serial = &((octave_serial &)rep); + + serial->srl_close(); + + return octave_value(); +} + +int octave_serial::srl_close() +{ + return ::close(this->srl_get_fd()); +} \ No newline at end of file Added: trunk/octave-forge/main/serial/src/srl_flush.cc =================================================================== --- trunk/octave-forge/main/serial/src/srl_flush.cc (rev 0) +++ trunk/octave-forge/main/serial/src/srl_flush.cc 2012-08-07 19:48:30 UTC (rev 10840) @@ -0,0 +1,99 @@ +// Copyright (C) 2012 Andrius Sutas <and...@gm...> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see <http://www.gnu.org/licenses/>. + +#include <octave/oct.h> +#include <octave/ov-int32.h> +//#include <octave/ops.h> +//#include <octave/ov-typeinfo.h> + +#include <iostream> +#include <string> +#include <algorithm> + +#ifndef __WIN32__ +#include <stdio.h> +#include <string.h> +#include <fcntl.h> +#include <errno.h> +#include <termios.h> +#include <unistd.h> +#endif + +using std::string; + +#include "serial.h" + +// PKG_ADD: autoload ("srl_flush", "serial.oct"); +int octave_serial::srl_flush(unsigned short queue_selector) +{ + + // TCIFLUSH Flush pending input. + // TCIOFLUSH Flush both pending input and untransmitted output. + // TCOFLUSH Flush untransmitted output. + + + int flag; + + switch (queue_selector) + { + case 0: flag = TCIFLUSH; break; + case 1: flag = TCIOFLUSH; break; + case 2: flag = TCOFLUSH; break; + default: + error("srl_flush: only [0..2] values are accepted..."); + return false; + } + + 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 = 1; + + 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(); +} Added: trunk/octave-forge/main/serial/src/srl_parity.cc =================================================================== --- trunk/octave-forge/main/serial/src/srl_parity.cc (rev 0) +++ trunk/octave-forge/main/serial/src/srl_parity.cc 2012-08-07 19:48:30 UTC (rev 10840) @@ -0,0 +1,123 @@ +// Copyright (C) 2012 Andrius Sutas <and...@gm...> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see <http://www.gnu.org/licenses/>. + +#include <octave/oct.h> +#include <octave/ov-int32.h> +//#include <octave/ops.h> +//#include <octave/ov-typeinfo.h> + +#include <iostream> +#include <string> +#include <algorithm> + +#ifndef __WIN32__ +#include <stdio.h> +#include <string.h> +#include <fcntl.h> +#include <errno.h> +#include <termios.h> +#include <unistd.h> +#endif + +using std::string; + +#include "serial.h" + +// PKG_ADD: autoload ("srl_parity", "serial.oct"); +DEFUN_DLD (srl_parity, args, nargout, "Hello World Help String") +{ + if (args.length() < 1 || args.length() > 2) + { + 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..."); + return octave_value(-1); + } + + // Setting new parity + if (args.length() > 1) + { + if ( !(args(1).is_string()) ) + { + error("srl_parity: expecting second argument of type string..."); + 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()); + + return octave_value(); + } + + // Returning current parity + // TODO: return current parity + + return octave_value("-N"); +} + +int octave_serial::srl_parity(string parity) +{ + // Convert string to lowercase + std::transform(parity.begin(), parity.end(), parity.begin(), ::tolower); + + /* + * PARENB Enable parity generation on output and parity checking for input. + * PARODD If set, then parity for input and output is odd; otherwise even parity is used. + */ + + if (parity == "n" || parity == "none") + { + // Disable parity generation/checking + BITMASK_CLEAR(this->config.c_cflag, PARENB); + } + else if (parity == "e" || parity == "even") + { + // Enable parity generation/checking + BITMASK_SET(this->config.c_cflag, PARENB); + + // Set to Even + BITMASK_CLEAR(this->config.c_cflag, PARODD); + + } + else if (parity == "o" || parity == "odd") + { + // Enable parity generation/checking + BITMASK_SET(this->config.c_cflag, PARENB); + + // Set to Odd + BITMASK_SET(this->config.c_cflag, PARODD); + + } + else + { + error("srl_parity: Only [N]one, [E]ven or [O]dd parities are supported..."); + return false; + } + + if (tcsetattr(this->srl_get_fd(), TCSANOW, &this->config) < 0) { + error("srl_parity: error setting parity..."); + return false; + } + + return true; +} \ No newline at end of file Added: trunk/octave-forge/main/serial/src/srl_read.cc =================================================================== --- trunk/octave-forge/main/serial/src/srl_read.cc (rev 0) +++ trunk/octave-forge/main/serial/src/srl_read.cc 2012-08-07 19:48:30 UTC (rev 10840) @@ -0,0 +1,111 @@ +// Copyright (C) 2012 Andrius Sutas <and...@gm...> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see <http://www.gnu.org/licenses/>. + +#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> +#include <algorithm> + +#ifndef __WIN32__ +#include <stdio.h> +#include <string.h> +#include <fcntl.h> +#include <errno.h> +#include <termios.h> +#include <unistd.h> +#endif + +using std::string; + +#include "serial.h" + +// PKG_ADD: autoload ("srl_read", "serial.oct"); +DEFUN_DLD (srl_read, args, nargout, "Hello World Help String") +{ + if (args.length() < 1 || args.length() > 2) + { + 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..."); + return octave_value(-1); + } + + char *buffer = NULL; + unsigned int buffer_len = 1; + + if (args.length() > 1) + { + if ( !(args(1).is_integer_type() || args(1).is_float_type()) ) + { + error("srl_read: expecting second argument of type integer..."); + return octave_value(-1); + } + + buffer_len = args(1).int_value(); + } + + buffer = new char[buffer_len+1]; + + if (buffer == NULL) + { + error("srl_read: cannot allocate requested memory..."); + return octave_value(-1); + } + + octave_serial* serial = NULL; + + const octave_base_value& rep = args(0).get_rep(); + serial = &((octave_serial &)rep); + + int retval = 0; + + while (retval < buffer_len) + { + retval += serial->srl_read(buffer + retval, buffer_len - retval); + } + + //buffer[retval] = '\0'; + + octave_value_list return_list; + uint8NDArray data; + data.resize(retval); + + // TODO: clean this up + for (int i = 0; i < retval; i++) + { + data(i) = buffer[i]; + } + + return_list(1) = retval; + return_list(0) = data; + + delete[] buffer; + + return return_list; +} + +int octave_serial::srl_read(char *buf, unsigned int len) +{ + return ::read(srl_get_fd(), buf, len); +} \ No newline at end of file Added: trunk/octave-forge/main/serial/src/srl_stopbits.cc =================================================================== --- trunk/octave-forge/main/serial/src/srl_stopbits.cc (rev 0) +++ trunk/octave-forge/main/serial/src/srl_stopbits.cc 2012-08-07 19:48:30 UTC (rev 10840) @@ -0,0 +1,107 @@ +// Copyright (C) 2012 Andrius Sutas <and...@gm...> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see <http://www.gnu.org/licenses/>. + +#include <octave/oct.h> +#include <octave/ov-int32.h> +//#include <octave/ops.h> +//#include <octave/ov-typeinfo.h> + +#include <iostream> +#include <string> +#include <algorithm> + +#ifndef __WIN32__ +#include <stdio.h> +#include <string.h> +#include <fcntl.h> +#include <errno.h> +#include <termios.h> +#include <unistd.h> +#endif + +using std::string; + +#include "serial.h" + +// PKG_ADD: autoload ("srl_stopbits", "serial.oct"); +DEFUN_DLD (srl_stopbits, args, nargout, "Hello World Help String") +{ + if (args.length() < 1 || args.length() > 2) + { + 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..."); + return octave_value(-1); + } + + // Setting new stop bits + if (args.length() > 1) + { + if ( !(args(1).is_integer_type() || args(1).is_float_type()) ) + { + error("srl_stopbits: expecting second argument of type integer..."); + 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()); + + return octave_value(); + } + + // Returning current stop bits + + // TODO: return current stop bits + + return octave_value(-1); +} + +int octave_serial::srl_stopbits(unsigned short stopbits) +{ + /* + * CSTOPB Send two stop bits, else one. + */ + + if (stopbits == 1) + { + // Set to one stop bit + BITMASK_CLEAR(this->config.c_cflag, CSTOPB); + } + else if (stopbits == 2) + { + // Set to two stop bits + BITMASK_SET(this->config.c_cflag, CSTOPB); + } + else + { + error("srl_stopbits: Only 1 or 2 stop bits are supported..."); + return false; + } + + if (tcsetattr(this->srl_get_fd(), TCSANOW, &this->config) < 0) { + error("srl_stopbits: error setting stop bits..."); + return false; + } + + return true; +} \ No newline at end of file Added: trunk/octave-forge/main/serial/src/srl_write.cc =================================================================== --- trunk/octave-forge/main/serial/src/srl_write.cc (rev 0) +++ trunk/octave-forge/main/serial/src/srl_write.cc 2012-08-07 19:48:30 UTC (rev 10840) @@ -0,0 +1,73 @@ +// Copyright (C) 2012 Andrius Sutas <and...@gm...> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see <http://www.gnu.org/licenses/>. + +#include <octave/oct.h> +#include <octave/ov-int32.h> +//#include <octave/ops.h> +//#include <octave/ov-typeinfo.h> + +#include <iostream> +#include <string> +#include <algorithm> + +#ifndef __WIN32__ +#include <stdio.h> +#include <string.h> +#include <fcntl.h> +#include <errno.h> +#include <termios.h> +#include <unistd.h> +#endif + +using std::string; + +#include "serial.h" + +// TODO: implement uint8 array as input type +// PKG_ADD: autoload ("srl_write", "serial.oct"); +DEFUN_DLD (srl_write, args, nargout, "Hello World Help String") +{ + if (args.length() != 2) + { + error("srl_write: expecting 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(1).is_string()) + { + error("srl_write: expecting second argument of type string..."); + return octave_value(-1); + } + + octave_serial* serial = NULL; + + const octave_base_value& rep = args(0).get_rep(); + serial = &((octave_serial &)rep); + + serial->srl_write(args(1).string_value()); + + return octave_value(); +} + +int octave_serial::srl_write(string str) +{ + return ::write(srl_get_fd(), str.c_str(), str.length()); +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |