From 0b7d4c8c1e9f93eb6d4ad15945e74db39897ba0a Mon Sep 17 00:00:00 2001 From: curt Date: Mon, 16 Nov 1998 13:53:01 +0000 Subject: [PATCH] Initial revision. --- Serial/Makefile.am | 13 ++++ Serial/serial.cxx | 162 ++++++++++++++++++++++++++++++++++++++++++ Serial/serial.hxx | 71 ++++++++++++++++++ Serial/testserial.cxx | 17 +++++ 4 files changed, 263 insertions(+) create mode 100644 Serial/Makefile.am create mode 100644 Serial/serial.cxx create mode 100644 Serial/serial.hxx create mode 100644 Serial/testserial.cxx diff --git a/Serial/Makefile.am b/Serial/Makefile.am new file mode 100644 index 00000000..66a9f166 --- /dev/null +++ b/Serial/Makefile.am @@ -0,0 +1,13 @@ +bin_PROGRAMS = testserial + +noinst_LIBRARIES = libSerial.a + +libSerial_a_SOURCES = serial.cxx serial.hxx + +testserial_SOURCES = testserial.cxx + +testserial_LDADD = \ + $(top_builddir)/Lib/Serial/libSerial.a \ + $(top_builddir)/Lib/Debug/libDebug.a + +INCLUDES += -I$(top_builddir) -I$(top_builddir)/Lib diff --git a/Serial/serial.cxx b/Serial/serial.cxx new file mode 100644 index 00000000..0106e242 --- /dev/null +++ b/Serial/serial.cxx @@ -0,0 +1,162 @@ +// serial.cxx -- Unix serial I/O support +// +// Written by Curtis Olson, started November 1998. +// +// Copyright (C) 1998 Curtis L. Olson - curt@flightgear.org +// +// 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 2 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, write to the Free Software +// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +// +// $Id$ +// (Log is kept at end of this file) + + +#include +#include +#include +#include +#include + +#include + +#include "serial.hxx" + + +fgSERIAL::fgSERIAL() { + dev_open = false; +} + +fgSERIAL::fgSERIAL(const string& device, int baud) { + open_port(device); + + if ( dev_open ) { + set_baud(baud); + } +} + +fgSERIAL::~fgSERIAL() { + close(fd); +} + +bool fgSERIAL::open_port(const string& device) { + if ( (fd = open(device.c_str(), O_RDWR | O_NONBLOCK)) == -1 ) { + FG_LOG( FG_SERIAL, FG_ALERT, "Cannot open " << device + << " for serial I/O" ); + return false; + } else { + dev_open = true; + return true; + } +} + +bool fgSERIAL::set_baud(int baud) { + struct termios config; + speed_t speed; + + cout << "attempting to set baud rate to: " << baud << endl; + + if ( tcgetattr( fd, &config ) != 0 ) { + FG_LOG( FG_SERIAL, FG_ALERT, "Unable to poll port settings" ); + return false; + } + + if ( baud == 300 ) { + speed = B300; + } else if ( baud == 1200 ) { + speed = B1200; + } else if ( baud == 2400 ) { + speed = B2400; + } else if ( baud == 4800 ) { + speed = B4800; + } else if ( baud == 9600 ) { + speed = B9600; + } else if ( baud == 19200 ) { + speed = B19200; + } else if ( baud == 38400 ) { + speed = B38400; + } else if ( baud == 57600 ) { + speed = B57600; + } else if ( baud == 115200 ) { + speed = B115200; + } else if ( baud == 230400 ) { + speed = B230400; + } else { + FG_LOG( FG_SERIAL, FG_ALERT, "Unsupported baud rate " << baud ); + return false; + } + + if ( cfsetispeed( &config, speed ) != 0 ) { + FG_LOG( FG_SERIAL, FG_ALERT, "Problem setting input baud rate" ); + return false; + } + + if ( cfsetospeed( &config, speed ) != 0 ) { + FG_LOG( FG_SERIAL, FG_ALERT, "Problem setting output baud rate" ); + return false; + } + + if ( tcsetattr( fd, TCSANOW, &config ) != 0 ) { + FG_LOG( FG_SERIAL, FG_ALERT, "Unable to update port settings" ); + return false; + } + + cout << "successfully set baud to " << baud << endl; + + return true; +} + +string fgSERIAL::read_port() { + const int max_count = 1024; + char buffer[max_count+1]; + int count; + string result; + + count = read(fd, buffer, max_count); + // cout << "read " << count << " bytes" << endl; + + if ( count < 0 ) { + // error condition + if ( errno != EAGAIN ) { + FG_LOG( FG_SERIAL, FG_ALERT, + "Serial I/O on read, error number = " << errno ); + } + + return ""; + } else { + buffer[count] = '\0'; + result = buffer; + + return result; + } +} + +int fgSERIAL::write_port(const string& value) { + int count; + + count = write(fd, value.c_str(), value.length()); + // cout << "write '" << value << "' " << count << " bytes" << endl; + + if ( (int)count != (int)value.length() ) { + FG_LOG( FG_SERIAL, FG_ALERT, + "Serial I/O on write, error number = " << errno ); + } + + return count; +} + + +// $Log$ +// Revision 1.1 1998/11/16 13:53:02 curt +// Initial revision. +// diff --git a/Serial/serial.hxx b/Serial/serial.hxx new file mode 100644 index 00000000..011e4959 --- /dev/null +++ b/Serial/serial.hxx @@ -0,0 +1,71 @@ +// serial.hxx -- Unix serial I/O support +// +// Written by Curtis Olson, started November 1998. +// +// Copyright (C) 1998 Curtis L. Olson - curt@flightgear.org +// +// 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 2 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, write to the Free Software +// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +// +// $Id$ +// (Log is kept at end of this file) + + +#ifndef _SERIAL_HXX +#define _SERIAL_HXX + + +#ifndef __cplusplus +# error This library requires C++ +#endif + + +#include + +// if someone know how to do this all with C++ streams let me know +#include + + +class fgSERIAL +{ + +private: + + int fd; + bool dev_open; + +public: + + fgSERIAL(); + fgSERIAL(const string& device, int baud); + + ~fgSERIAL(); + + bool open_port(const string& device); + bool close_port(); + bool set_baud(int baud); + string read_port(); + int write_port(const string& value); + + inline bool is_enabled() { return dev_open; } +}; + + +#endif // _SERIAL_HXX + + +// $Log$ +// Revision 1.1 1998/11/16 13:53:02 curt +// Initial revision. +// diff --git a/Serial/testserial.cxx b/Serial/testserial.cxx new file mode 100644 index 00000000..b563ac76 --- /dev/null +++ b/Serial/testserial.cxx @@ -0,0 +1,17 @@ +#include + +#include "serial.hxx" + +main () { + fgSERIAL port( "/dev/ttyS1", 4800); + string value; + + port.write_port("ATDT 626-9800\n"); + + while ( true ) { + value = port.read_port(); + if ( value.length() ) { + cout << "-> " << value << endl; + } + } +}