sg_serial.cxx

00001 // sg_serial.cxx -- Serial I/O routines
00002 //
00003 // Written by Curtis Olson, started November 1999.
00004 //
00005 // Copyright (C) 1999  Curtis L. Olson - http://www.flightgear.org/~curt
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License as
00009 // published by the Free Software Foundation; either version 2 of the
00010 // License, or (at your option) any later version.
00011 //
00012 // This program is distributed in the hope that it will be useful, but
00013 // WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 // General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with this program; if not, write to the Free Software
00019 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00020 //
00021 // $Id: sg__serial_8cxx-source.html,v 1.15 2007-12-17 15:37:10 curt Exp $
00022 
00023 #include <stdlib.h>
00024 #include <cstring>
00025 
00026 #include <simgear/compiler.h>
00027 
00028 #include STL_STRING
00029 
00030 #include <simgear/debug/logstream.hxx>
00031 #include <simgear/serial/serial.hxx>
00032 
00033 #include "sg_serial.hxx"
00034 
00035 SG_USING_STD(string);
00036 
00037 
00038 SGSerial::SGSerial( const string& device_name, const string& baud_rate ) :
00039     save_len(0)
00040 {
00041     set_type( sgSerialType );
00042     device = device_name;
00043     baud = baud_rate;
00044 }
00045 
00046 
00047 SGSerial::~SGSerial() {
00048 }
00049 
00050 
00051 // open the serial port based on specified direction
00052 bool SGSerial::open( const SGProtocolDir d ) {
00053     set_dir( d );
00054 
00055     if ( ! port.open_port( device ) ) {
00056         SG_LOG( SG_IO, SG_ALERT, "Error opening device: " << device );
00057         return false;
00058     }
00059 
00060     // cout << "fd = " << port.fd << endl;
00061 
00062     if ( ! port.set_baud( atoi( baud.c_str() ) ) ) {
00063         SG_LOG( SG_IO, SG_ALERT, "Error setting baud: " << baud );
00064         return false;
00065     }
00066 
00067     return true;
00068 }
00069 
00070 
00071 // Read data from port.  If we don't get enough data, save what we did
00072 // get in the save buffer and return 0.  The save buffer will be
00073 // prepended to subsequent reads until we get as much as is requested.
00074 
00075 int SGSerial::read( char *buf, int length ) {
00076     int result;
00077 
00078     // read a chunk, keep in the save buffer until we have the
00079     // requested amount read
00080 
00081     char *buf_ptr = save_buf + save_len;
00082     result = port.read_port( buf_ptr, length - save_len );
00083     
00084     if ( result + save_len == length ) {
00085         strncpy( buf, save_buf, length );
00086         save_len = 0;
00087 
00088         return length;
00089     }
00090     
00091     return 0;
00092 }
00093 
00094 
00095 // read data from port
00096 int SGSerial::readline( char *buf, int length ) {
00097     int result;
00098 
00099     // read a chunk, keep in the save buffer until we have the
00100     // requested amount read
00101 
00102     char *buf_ptr = save_buf + save_len;
00103     result = port.read_port( buf_ptr, SG_IO_MAX_MSG_SIZE - save_len );
00104     save_len += result;
00105 
00106     // look for the end of line in save_buf
00107     int i;
00108     for ( i = 0; i < save_len && save_buf[i] != '\n'; ++i );
00109     if ( save_buf[i] == '\n' ) {
00110         result = i + 1;
00111     } else {
00112         // no end of line yet
00113         return 0;
00114     }
00115 
00116     // we found an end of line
00117 
00118     // copy to external buffer
00119     strncpy( buf, save_buf, result );
00120     buf[result] = '\0';
00121     SG_LOG( SG_IO, SG_INFO, "fg_serial line = " << buf );
00122 
00123     // shift save buffer
00124     for ( i = result; i < save_len; ++i ) {
00125         save_buf[ i - result ] = save_buf[i];
00126     }
00127     save_len -= result;
00128 
00129     return result;
00130 }
00131 
00132 
00133 // write data to port
00134 int SGSerial::write( const char *buf, const int length ) {
00135     int result = port.write_port( buf, length );
00136 
00137     if ( result != length ) {
00138         SG_LOG( SG_IO, SG_WARN, "Error writing data: " << device );
00139     }
00140 
00141     return result;
00142 }
00143 
00144 
00145 // write null terminated string to port
00146 int SGSerial::writestring( const char *str ) {
00147     int length = strlen( str );
00148     return write( str, length );
00149 }
00150 
00151 
00152 // close the port
00153 bool SGSerial::close() {
00154     if ( ! port.close_port() ) {
00155         return false;
00156     }
00157 
00158     return true;
00159 }

Generated on Mon Dec 17 09:30:55 2007 for SimGear by  doxygen 1.5.1