Mercurial > forge
changeset 11158:231540543c1d octave-forge
instrument-control: serial OSX compatibility fix
author | eandrius |
---|---|
date | Tue, 23 Oct 2012 14:41:28 +0000 |
parents | cf1eacc6d137 |
children | 02aabc17d658 |
files | main/instrument-control/src/serial/serial.cc main/instrument-control/src/serial/serial_class.cc |
diffstat | 2 files changed, 26 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
--- a/main/instrument-control/src/serial/serial.cc Tue Oct 23 02:41:14 2012 +0000 +++ b/main/instrument-control/src/serial/serial.cc Tue Oct 23 14:41:28 2012 +0000 @@ -28,7 +28,7 @@ static bool type_loaded = false; DEFUN_DLD (serial, args, nargout, -"-*- texinfo -*-\n\ + "-*- texinfo -*-\n\ @deftypefn {Loadable Function} {@var{serial} = } serial ([@var{path}], [@var{baudrate}], [@var{timeout}])\n \ \n\ Open serial interface.\n \ @@ -44,7 +44,7 @@ error("serial: Windows platform support is not yet implemented, go away..."); return octave_value(); #endif - + if (!type_loaded) { octave_serial::register_type(); @@ -57,18 +57,19 @@ print_usage(); return octave_value(); } - + // Default values string path("/dev/ttyUSB0"); unsigned int baud_rate = 115200; short timeout = -1; - + unsigned short bytesize = 8; string parity("N"); unsigned short stopbits = 1; - int oflags = O_RDWR | O_NOCTTY | O_SYNC; + int oflags = O_RDWR | O_NOCTTY | O_SYNC | O_NDELAY; // O_SYNC - All writes immediately effective, no buffering - // O_NOCTTY - Don't make serial terminal the controlling terminal for the process + // O_NOCTTY - Do not make serial terminal the controlling terminal for the process + // O_NDELAY - Do not care what state the DCD signal line is in. Used for open only, later disabled. // Parse the function arguments if (args.length() > 0) @@ -113,20 +114,20 @@ } } - + octave_serial* retval = new octave_serial(); - + // Open the interface if (retval->open(path, oflags) < 0) return octave_value(); - + retval->set_baudrate(baud_rate); retval->set_timeout(timeout); retval->set_parity(parity); retval->set_bytesize(bytesize); retval->set_stopbits(stopbits); - + //retval->flush(2); - + return octave_value(retval); }
--- a/main/instrument-control/src/serial/serial_class.cc Tue Oct 23 02:41:14 2012 +0000 +++ b/main/instrument-control/src/serial/serial_class.cc Tue Oct 23 14:41:28 2012 +0000 @@ -44,7 +44,7 @@ int octave_serial::open(string path, int flags) { - this->fd = ::open(path.c_str(), flags, 0); + this->fd = ::open(path.c_str(), flags); if (this->get_fd() > 0) { @@ -64,22 +64,27 @@ } // Clear all settings - memset(&this->config, 0, sizeof(this->config)); - this->config.c_iflag = 0; - this->config.c_oflag = 0; - this->config.c_cflag = CS8 | CREAD | CLOCAL; // 8n1 - this->config.c_lflag = 0; + this->config.c_iflag = 0; // Input modes + this->config.c_oflag = 0; // Output modes + this->config.c_cflag = CS8 | CREAD | CLOCAL; // Control modes, 8n1 + this->config.c_lflag = 0; // Local modes this->config.c_cc[VMIN] = 1; - this->config.c_cc[VTIME] = 0; - //tcsetattr(this->get_fd(), TCSANOW, &this->config); - if (tcsetattr(this->get_fd(), TCSAFLUSH, &this->config) < 0) + if (tcsetattr(this->get_fd(), TCSANOW, &this->config) < 0) { error("serial: Failed to set default terminal attributes: %s\n", strerror(errno)); this->close(); return -1; } + // Disable NDELAY + if (fcntl(this->get_fd(), F_SETFL, 0) < 0) + { + error("serial: Failed to disable NDELAY flag: %s\n", strerror(errno)); + this->close(); + return -1; + } + this->blocking_read = true; } else