]> git.mxchange.org Git - simgear.git/blobdiff - simgear/serial/serial.cxx
Fix platform check for strerror_r on Mac
[simgear.git] / simgear / serial / serial.cxx
index 3c24f87789559a4791fe198325da5119161b1a2b..985cc8755e47c4761a2feafb83b0f471c3010896 100644 (file)
@@ -2,7 +2,7 @@
 //
 // Written by Curtis Olson, started November 1998.
 //
-// Copyright (C) 1998  Curtis L. Olson - curt@flightgear.org
+// Copyright (C) 1998  Curtis L. Olson - http://www.flightgear.org/~curt
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Library General Public
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 // Library General Public License for more details.
 //
-// You should have received a copy of the GNU Library General Public
-// License along with this library; if not, write to the
-// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-// Boston, MA  02111-1307, USA.
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
 // $Id$
 
 
 #include <simgear/compiler.h>
 
-#include STL_IOSTREAM
+#include <iostream>
+#include <cerrno>
 
-#ifdef SG_HAVE_STD_INCLUDE
-#  include <cerrno>
-#else
-#  include <errno.h>
-#endif
-
-#if !defined( WIN32 ) || defined( __CYGWIN__) || defined( __CYGWIN32__ )
+#ifndef _WIN32
 #  include <termios.h>
 #  include <sys/types.h>
 #  include <sys/stat.h>
@@ -66,7 +60,7 @@ SGSerialPort::~SGSerialPort() {
 
 bool SGSerialPort::open_port(const string& device) {
 
-#if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
+#ifdef _WIN32
 
     fd = CreateFile( device.c_str(),
         GENERIC_READ | GENERIC_WRITE,
@@ -102,7 +96,7 @@ bool SGSerialPort::open_port(const string& device) {
 
     struct termios config;
 
-    fd = open(device.c_str(), O_RDWR | O_NONBLOCK);
+    fd = open(device.c_str(), O_RDWR | O_NOCTTY| O_NDELAY);
     SG_LOG( SG_EVENT, SG_DEBUG, "Serial fd created = " << fd);
 
     if ( fd  == -1 ) {
@@ -123,18 +117,24 @@ bool SGSerialPort::open_port(const string& device) {
 
     // cout << "config.c_iflag = " << config.c_iflag << endl;
 
-    // software flow control on
-    config.c_iflag |= IXON;
-    // config.c_iflag |= IXOFF;
+    // disable LF expanded to CR-LF
+    config.c_oflag &= ~(ONLCR);
+
+    // disable software flow control
+    config.c_iflag &= ~(IXON | IXOFF | IXANY);
 
-    // config.c_cflag |= CLOCAL;
+    // enable the receiver and set local mode
+    config.c_cflag |= (CLOCAL | CREAD);
 
-#if ! defined( sgi )    
+#if !defined( sgi ) && !defined(_AIX)
     // disable hardware flow control
     config.c_cflag &= ~(CRTSCTS);
 #endif
 
     // cout << "config.c_iflag = " << config.c_iflag << endl;
+    
+    // Raw (not cooked/canonical) input mode
+    config.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
 
     if ( tcsetattr( fd, TCSANOW, &config ) != 0 ) {
        SG_LOG( SG_IO, SG_ALERT, "Unable to update port settings" );
@@ -147,7 +147,7 @@ bool SGSerialPort::open_port(const string& device) {
 
 
 bool SGSerialPort::close_port() {
-#if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
+#ifdef _WIN32
     CloseHandle( fd );
 #else
     close(fd);
@@ -161,7 +161,7 @@ bool SGSerialPort::close_port() {
 
 bool SGSerialPort::set_baud(int baud) {
 
-#if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
+#ifdef _WIN32
 
     DCB dcb;
     if ( GetCommState( fd, &dcb ) ) {
@@ -234,11 +234,11 @@ bool SGSerialPort::set_baud(int baud) {
        speed = B19200;
     } else if ( baud == 38400 ) {
        speed = B38400;
+#if defined( linux ) || defined( __FreeBSD__ )
     } else if ( baud == 57600 ) {
        speed = B57600;
     } else if ( baud == 115200 ) {
        speed = B115200;
-#if defined( linux ) || defined( __FreeBSD__ )
     } else if ( baud == 230400 ) {
        speed = B230400;
 #endif
@@ -274,7 +274,7 @@ string SGSerialPort::read_port() {
     char buffer[max_count+1];
     string result;
 
-#if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
+#ifdef _WIN32
 
     DWORD count;
     if ( ReadFile( fd, buffer, max_count, &count, 0 ) ) {
@@ -326,7 +326,7 @@ string SGSerialPort::read_port() {
 
 int SGSerialPort::read_port(char *buf, int len) {
 
-#if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
+#ifdef _WIN32
 
     DWORD count;
     if ( ReadFile( fd, buf, len, &count, 0 ) ) {
@@ -383,7 +383,7 @@ int SGSerialPort::read_port(char *buf, int len) {
 
 int SGSerialPort::write_port(const string& value) {
 
-#if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
+#ifdef _WIN32
 
     LPCVOID lpBuffer = value.data();
     DWORD nNumberOfBytesToWrite = value.length();
@@ -457,7 +457,7 @@ int SGSerialPort::write_port(const string& value) {
 
 
 int SGSerialPort::write_port(const char* buf, int len) {
-#if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
+#ifdef _WIN32
 
     LPCVOID lpBuffer = buf;
     DWORD nNumberOfBytesToWrite = len;