]> git.mxchange.org Git - flightgear.git/blob - src/Network/fg_serial.cxx
UIUC flight model contribution. This is based on LaRCsim, but can read
[flightgear.git] / src / Network / fg_serial.cxx
1 // fg_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 <Aircraft/aircraft.hxx>
32
33 #include "fg_serial.hxx"
34
35 FG_USING_STD(string);
36
37
38 FGSerial::FGSerial() :
39     save_len(0)
40 {
41 }
42
43
44 FGSerial::~FGSerial() {
45 }
46
47
48 // open the serial port based on specified direction
49 bool FGSerial::open( FGProtocol::fgProtocolDir dir ) {
50     if ( ! port.open_port( device ) ) {
51         FG_LOG( FG_IO, FG_ALERT, "Error opening device: " << device );
52         return false;
53     }
54
55     // cout << "fd = " << port.fd << endl;
56
57     if ( ! port.set_baud( atoi( baud.c_str() ) ) ) {
58         FG_LOG( FG_IO, FG_ALERT, "Error setting baud: " << baud );
59         return false;
60     }
61
62     return true;
63 }
64
65
66 // Read data from port.  If we don't get enough data, save what we did
67 // get in the save buffer and return 0.  The save buffer will be
68 // prepended to subsequent reads until we get as much as is requested.
69
70 int FGSerial::read( char *buf, int length ) {
71     int result;
72
73     // read a chunk, keep in the save buffer until we have the
74     // requested amount read
75
76     char *buf_ptr = save_buf + save_len;
77     result = port.read_port( buf_ptr, length - save_len );
78     
79     if ( result + save_len == length ) {
80         strncpy( buf, save_buf, length );
81         save_len = 0;
82
83         return length;
84     }
85     
86     return 0;
87 }
88
89
90 // read data from port
91 int FGSerial::readline( char *buf, int length ) {
92     int result;
93
94     // read a chunk, keep in the save buffer until we have the
95     // requested amount read
96
97     char *buf_ptr = save_buf + save_len;
98     result = port.read_port( buf_ptr, FG_MAX_MSG_SIZE - save_len );
99     save_len += result;
100
101     // look for the end of line in save_buf
102     int i;
103     for ( i = 0; i < save_len && save_buf[i] != '\n'; ++i );
104     if ( save_buf[i] == '\n' ) {
105         result = i + 1;
106     } else {
107         // no end of line yet
108         return 0;
109     }
110
111     // we found an end of line
112
113     // copy to external buffer
114     strncpy( buf, save_buf, result );
115     buf[result] = '\0';
116     FG_LOG( FG_IO, FG_INFO, "fg_serial line = " << buf );
117
118     // shift save buffer
119     for ( i = result; i < save_len; ++i ) {
120         save_buf[ i - result ] = save_buf[i];
121     }
122     save_len -= result;
123
124     return result;
125 }
126
127
128 // write data to port
129 int FGSerial::write( char *buf, int length ) {
130     int result = port.write_port( buf, length );
131
132     if ( result != length ) {
133         FG_LOG( FG_IO, FG_ALERT, "Error writing data: " << device );
134     }
135
136     return result;
137 }
138
139
140 // write null terminated string to port
141 int FGSerial::writestring( char *str ) {
142     int length = strlen( str );
143     return write( str, length );
144 }
145
146
147 // close the port
148 bool FGSerial::close() {
149     if ( ! port.close_port() ) {
150         return false;
151     }
152
153     return true;
154 }