]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_serial.cxx
Tweaking api.
[simgear.git] / simgear / io / sg_serial.cxx
1 // sg_serial.cxx -- Serial I/O routines
2 //
3 // Written by Curtis Olson, started November 1999.
4 //
5 // Copyright (C) 1999  Curtis L. Olson - curt@flightgear.org
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #include <simgear/compiler.h>
25
26 #include STL_STRING
27
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/serial/serial.hxx>
30
31 #include "sg_serial.hxx"
32
33 FG_USING_STD(string);
34
35
36 SGSerial::SGSerial( const string& device_name, const string& baud_rate ) :
37     save_len(0)
38 {
39     set_type( sgSerialType );
40     device = device_name;
41     baud = baud_rate;
42 }
43
44
45 SGSerial::~SGSerial() {
46 }
47
48
49 // open the serial port based on specified direction
50 bool SGSerial::open( SGProtocolDir dir ) {
51     if ( ! port.open_port( device ) ) {
52         FG_LOG( FG_IO, FG_ALERT, "Error opening device: " << device );
53         return false;
54     }
55
56     // cout << "fd = " << port.fd << endl;
57
58     if ( ! port.set_baud( atoi( baud.c_str() ) ) ) {
59         FG_LOG( FG_IO, FG_ALERT, "Error setting baud: " << baud );
60         return false;
61     }
62
63     return true;
64 }
65
66
67 // Read data from port.  If we don't get enough data, save what we did
68 // get in the save buffer and return 0.  The save buffer will be
69 // prepended to subsequent reads until we get as much as is requested.
70
71 int SGSerial::read( char *buf, int length ) {
72     int result;
73
74     // read a chunk, keep in the save buffer until we have the
75     // requested amount read
76
77     char *buf_ptr = save_buf + save_len;
78     result = port.read_port( buf_ptr, length - save_len );
79     
80     if ( result + save_len == length ) {
81         strncpy( buf, save_buf, length );
82         save_len = 0;
83
84         return length;
85     }
86     
87     return 0;
88 }
89
90
91 // read data from port
92 int SGSerial::readline( char *buf, int length ) {
93     int result;
94
95     // read a chunk, keep in the save buffer until we have the
96     // requested amount read
97
98     char *buf_ptr = save_buf + save_len;
99     result = port.read_port( buf_ptr, SG_IO_MAX_MSG_SIZE - save_len );
100     save_len += result;
101
102     // look for the end of line in save_buf
103     int i;
104     for ( i = 0; i < save_len && save_buf[i] != '\n'; ++i );
105     if ( save_buf[i] == '\n' ) {
106         result = i + 1;
107     } else {
108         // no end of line yet
109         return 0;
110     }
111
112     // we found an end of line
113
114     // copy to external buffer
115     strncpy( buf, save_buf, result );
116     buf[result] = '\0';
117     FG_LOG( FG_IO, FG_INFO, "fg_serial line = " << buf );
118
119     // shift save buffer
120     for ( i = result; i < save_len; ++i ) {
121         save_buf[ i - result ] = save_buf[i];
122     }
123     save_len -= result;
124
125     return result;
126 }
127
128
129 // write data to port
130 int SGSerial::write( char *buf, int length ) {
131     int result = port.write_port( buf, length );
132
133     if ( result != length ) {
134         FG_LOG( FG_IO, FG_ALERT, "Error writing data: " << device );
135     }
136
137     return result;
138 }
139
140
141 // write null terminated string to port
142 int SGSerial::writestring( char *str ) {
143     int length = strlen( str );
144     return write( str, length );
145 }
146
147
148 // close the port
149 bool SGSerial::close() {
150     if ( ! port.close_port() ) {
151         return false;
152     }
153
154     return true;
155 }