00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 #include <cstdlib>
00024 #include <cstring>
00025 
00026 #include <simgear/compiler.h>
00027 
00028 #include <string>
00029 
00030 #include <simgear/debug/logstream.hxx>
00031 #include <simgear/serial/serial.hxx>
00032 
00033 #include "sg_serial.hxx"
00034 
00035 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 
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     
00061 
00062     if ( ! port.set_baud( std::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 
00072 
00073 
00074 
00075 int SGSerial::read( char *buf, int length ) {
00076     int result;
00077 
00078     
00079     
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         std::strncpy( buf, save_buf, length );
00086         save_len = 0;
00087 
00088         return length;
00089     }
00090     
00091     return 0;
00092 }
00093 
00094 
00095 
00096 int SGSerial::readline( char *buf, int length ) {
00097     int result;
00098 
00099     
00100     
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     
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         
00113         return 0;
00114     }
00115 
00116     
00117 
00118     
00119     std::strncpy( buf, save_buf, result );
00120     buf[result] = '\0';
00121     SG_LOG( SG_IO, SG_INFO, "fg_serial line = " << buf );
00122 
00123     
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 
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 
00146 int SGSerial::writestring( const char *str ) {
00147     int length = std::strlen( str );
00148     return write( str, length );
00149 }
00150 
00151 
00152 
00153 bool SGSerial::close() {
00154     if ( ! port.close_port() ) {
00155         return false;
00156     }
00157 
00158     return true;
00159 }