head 1.4; access; symbols; locks; strict; comment @# @; 1.4 date 2002.09.07.01.50.40; author curt; state Exp; branches; next 1.3; 1.3 date 2002.08.31.19.01.57; author curt; state Exp; branches; next 1.2; 1.2 date 2002.08.31.18.54.04; author curt; state Exp; branches; next 1.1; 1.1 date 2002.02.15.17.22.00; author curt; state Exp; branches; next ; desc @@ 1.4 log @Updated doxygen docs. @ text @
Main Page Class Hierarchy Compound List File List Compound Members File Members

# 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 - curt@@flightgear.org
    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., 675 Mass Ave, Cambridge, MA 02139, USA.
    00020 //
    00021 // $Id: sg_serial.cxx,v 1.5 2001/03/24 05:02:15 curt Exp $
    00022
    00023
    00024 #include <simgear/compiler.h>
    00025
    00026 #include STL_STRING
    00027
    00028 #include <simgear/debug/logstream.hxx>
    00029 #include <simgear/serial/serial.hxx>
    00030
    00031 #include "sg_serial.hxx"
    00032
    00033 SG_USING_STD(string);
    00034
    00035
    00036 SGSerial::SGSerial( const string& device_name, const string& baud_rate ) :
    00037     save_len(0)
    00038 {
    00039     set_type( sgSerialType );
    00040     device = device_name;
    00041     baud = baud_rate;
    00042 }
    00043
    00044
    00045 SGSerial::~SGSerial() {
    00046 }
    00047
    00048
    00049 // open the serial port based on specified direction
    00050 bool SGSerial::open( const SGProtocolDir d ) {
    00051     set_dir( d );
    00052
    00053     if ( ! port.open_port( device ) ) {
    00054         SG_LOG( SG_IO, SG_ALERT, "Error opening device: " << device );
    00055         return false;
    00056     }
    00057
    00058     // cout << "fd = " << port.fd << endl;
    00059
    00060     if ( ! port.set_baud( atoi( baud.c_str() ) ) ) {
    00061         SG_LOG( SG_IO, SG_ALERT, "Error setting baud: " << baud );
    00062         return false;
    00063     }
    00064
    00065     return true;
    00066 }
    00067
    00068
    00069 // Read data from port.  If we don't get enough data, save what we did
    00070 // get in the save buffer and return 0.  The save buffer will be
    00071 // prepended to subsequent reads until we get as much as is requested.
    00072
    00073 int SGSerial::read( char *buf, int length ) {
    00074     int result;
    00075
    00076     // read a chunk, keep in the save buffer until we have the
    00077     // requested amount read
    00078
    00079     char *buf_ptr = save_buf + save_len;
    00080     result = port.read_port( buf_ptr, length - save_len );
    00081
    00082     if ( result + save_len == length ) {
    00083         strncpy( buf, save_buf, length );
    00084         save_len = 0;
    00085
    00086         return length;
    00087     }
    00088
    00089     return 0;
    00090 }
    00091
    00092
    00093 // read data from port
    00094 int SGSerial::readline( char *buf, int length ) {
    00095     int result;
    00096
    00097     // read a chunk, keep in the save buffer until we have the
    00098     // requested amount read
    00099
    00100     char *buf_ptr = save_buf + save_len;
    00101     result = port.read_port( buf_ptr, SG_IO_MAX_MSG_SIZE - save_len );
    00102     save_len += result;
    00103
    00104     // look for the end of line in save_buf
    00105     int i;
    00106     for ( i = 0; i < save_len && save_buf[i] != '\n'; ++i );
    00107     if ( save_buf[i] == '\n' ) {
    00108         result = i + 1;
    00109     } else {
    00110         // no end of line yet
    00111         return 0;
    00112     }
    00113
    00114     // we found an end of line
    00115
    00116     // copy to external buffer
    00117     strncpy( buf, save_buf, result );
    00118     buf[result] = '\0';
    00119     SG_LOG( SG_IO, SG_INFO, "fg_serial line = " << buf );
    00120
    00121     // shift save buffer
    00122     for ( i = result; i < save_len; ++i ) {
    00123         save_buf[ i - result ] = save_buf[i];
    00124     }
    00125     save_len -= result;
    00126
    00127     return result;
    00128 }
    00129
    00130
    00131 // write data to port
    00132 int SGSerial::write( const char *buf, const int length ) {
    00133     int result = port.write_port( buf, length );
    00134
    00135     if ( result != length ) {
    00136         SG_LOG( SG_IO, SG_ALERT, "Error writing data: " << device );
    00137     }
    00138
    00139     return result;
    00140 }
    00141
    00142
    00143 // write null terminated string to port
    00144 int SGSerial::writestring( const char *str ) {
    00145     int length = strlen( str );
    00146     return write( str, length );
    00147 }
    00148
    00149
    00150 // close the port
    00151 bool SGSerial::close() {
    00152     if ( ! port.close_port() ) {
    00153         return false;
    00154     }
    00155
    00156     return true;
    00157 }

@ 1.3 log @Oops, try again. @ text @d166 1 a166 1