1 // fg_serial.cxx -- higher level serial port management routines
3 // Written by Curtis Olson, started November 1998.
5 // Copyright (C) 1998 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
24 #include <Include/compiler.h>
26 #ifdef FG_HAVE_STD_INCLUDES
28 # include <cstdlib> // atoi()
31 # include <stdlib.h> // atoi()
38 #include <Debug/logstream.hxx>
39 #include <Aircraft/aircraft.hxx>
40 #include <Include/fg_constants.h>
41 #include <Serial/serial.hxx>
42 #include <Time/fg_time.hxx>
44 #include "options.hxx"
46 #include "fg_serial.hxx"
51 // support an arbitrary number of serial channels. Each channel can
52 // be assigned to an arbitrary port. Bi-directional communication is
53 // supported by the underlying layer, but probably will never be
56 typedef vector < fgIOCHANNEL > io_container;
57 typedef io_container::iterator io_iterator;
58 typedef io_container::const_iterator const_io_iterator;
60 // define the four channels
61 io_container port_list;
64 fgIOCHANNEL::fgIOCHANNEL() :
65 kind( FG_SERIAL_DISABLED ),
71 fgIOCHANNEL::~fgIOCHANNEL() {
75 // configure a port based on the config string
76 static fgIOCHANNEL parse_port_config( const string& config )
80 string::size_type begin, end;
84 FG_LOG( FG_SERIAL, FG_INFO, "Parse serial port config: " << config );
87 end = config.find(",", begin);
88 if ( end == string::npos ) {
92 p.device = config.substr(begin, end - begin);
94 FG_LOG( FG_SERIAL, FG_INFO, " device = " << p.device );
97 end = config.find(",", begin);
98 if ( end == string::npos ) {
102 p.format = config.substr(begin, end - begin);
104 FG_LOG( FG_SERIAL, FG_INFO, " format = " << p.format );
107 end = config.find(",", begin);
108 if ( end == string::npos ) {
112 p.baud = config.substr(begin, end - begin);
114 FG_LOG( FG_SERIAL, FG_INFO, " baud = " << p.baud );
117 p.direction = config.substr(begin);
118 FG_LOG( FG_SERIAL, FG_INFO, " direction = " << p.direction );
120 p.valid_config = true;
126 // configure a port based on the config info
127 static bool config_port( fgIOCHANNEL &p )
129 if ( p.port.is_enabled() ) {
130 FG_LOG( FG_SERIAL, FG_ALERT, "This shouldn't happen, but the port "
131 << "is already in use, ignoring" );
135 if ( ! p.port.open_port( p.device ) ) {
136 FG_LOG( FG_SERIAL, FG_ALERT, "Error opening device: " << p.device );
140 // cout << "fd = " << p.port.fd << endl;
142 if ( ! p.port.set_baud( atoi( p.baud.c_str() ) ) ) {
143 FG_LOG( FG_SERIAL, FG_ALERT, "Error setting baud: " << p.baud );
147 if ( p.format == "nmea" ) {
148 if ( p.direction == "out" ) {
149 p.kind = fgIOCHANNEL::FG_SERIAL_NMEA_OUT;
150 } else if ( p.direction == "in" ) {
151 p.kind = fgIOCHANNEL::FG_SERIAL_NMEA_IN;
153 FG_LOG( FG_SERIAL, FG_ALERT, "Unknown direction" );
156 } else if ( p.format == "garmin" ) {
157 if ( p.direction == "out" ) {
158 p.kind = fgIOCHANNEL::FG_SERIAL_GARMIN_OUT;
159 } else if ( p.direction == "in" ) {
160 p.kind = fgIOCHANNEL::FG_SERIAL_GARMIN_IN;
162 FG_LOG( FG_SERIAL, FG_ALERT, "Unknown direction" );
165 } else if ( p.format == "fgfs" ) {
166 if ( p.direction == "out" ) {
167 p.kind = fgIOCHANNEL::FG_SERIAL_FGFS_OUT;
168 } else if ( p.direction == "in" ) {
169 p.kind = fgIOCHANNEL::FG_SERIAL_FGFS_IN;
171 FG_LOG( FG_SERIAL, FG_ALERT, "Unknown direction" );
174 } else if ( p.format == "rul" ) {
175 if ( p.direction == "out" ) {
176 p.kind = fgIOCHANNEL::FG_SERIAL_RUL_OUT;
177 } else if ( p.direction == "in" ) {
178 FG_LOG( FG_SERIAL, FG_ALERT, "RUL format is outgoing only" );
181 FG_LOG( FG_SERIAL, FG_ALERT, "Unknown direction" );
185 FG_LOG( FG_SERIAL, FG_ALERT, "Unknown format" );
193 // step through the port config streams (from fgOPTIONS) and setup
194 // serial port channels for each
195 void fgSerialInit() {
198 str_container port_options_list = current_options.get_port_options_list();
200 // we could almost do this in a single step except pushing a valid
201 // port onto the port list copies the structure and destroys the
202 // original, which closes the port and frees up the fd ... doh!!!
204 // parse the configuration strings and store the results in stub
205 // fgIOCHANNEL structures
206 const_str_iterator current_str = port_options_list.begin();
207 const_str_iterator last_str = port_options_list.end();
208 for ( ; current_str != last_str; ++current_str ) {
209 port = parse_port_config( *current_str );
210 if ( port.valid_config ) {
211 result = config_port( port );
213 port_list.push_back( port );
220 char calc_nmea_cksum(char *sentence) {
221 unsigned char sum = 0;
224 // printf("%s\n", sentence);
226 len = strlen(sentence);
228 for ( i = 1; i < len; i++ ) {
229 // printf("%c", sentence[i]);
234 // printf("sum = %02x\n", sum);
239 static void send_nmea_out( fgIOCHANNEL *p ) {
240 char rmc[256], gga[256];
241 char rmc_sum[10], gga_sum[10];
248 f = current_aircraft.fdm_state;
249 t = FGTime::cur_time_params;
251 // run once every two seconds
252 if ( p->last_time == t->get_cur_time() ) {
255 p->last_time = t->get_cur_time();
256 if ( t->get_cur_time() % 2 != 0 ) {
261 sprintf( utc, "%02d%02d%02d",
262 t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
265 double latd = f->get_Latitude() * RAD_TO_DEG;
273 min = (latd - (double)deg) * 60.0;
274 sprintf( lat, "%02d%06.3f,%c", abs(deg), min, dir);
277 double lond = f->get_Longitude() * RAD_TO_DEG;
285 min = (lond - (double)deg) * 60.0;
286 sprintf( lon, "%03d%06.3f,%c", abs(deg), min, dir);
289 sprintf( speed, "%05.1f", f->get_V_equiv_kts() );
292 sprintf( heading, "%05.1f", f->get_Psi() * RAD_TO_DEG );
295 sprintf( altitude_m, "%02d", (int)(f->get_Altitude() * FEET_TO_METER) );
297 char altitude_ft[10];
298 sprintf( altitude_ft, "%02d", (int)f->get_Altitude() );
301 sprintf( date, "%02d%02d%02d", t->getGmt()->tm_mday,
302 t->getGmt()->tm_mon+1, t->getGmt()->tm_year );
304 // $GPRMC,HHMMSS,A,DDMM.MMM,N,DDDMM.MMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E*XX
305 sprintf( rmc, "GPRMC,%s,A,%s,%s,%s,%s,%s,0.000,E",
306 utc, lat, lon, speed, heading, date );
307 sprintf( rmc_sum, "%02X", calc_nmea_cksum(rmc) );
309 sprintf( gga, "GPGGA,%s,%s,%s,1,,,%s,F,,,,",
310 utc, lat, lon, altitude_ft );
311 sprintf( gga_sum, "%02X", calc_nmea_cksum(gga) );
314 FG_LOG( FG_SERIAL, FG_DEBUG, rmc );
315 FG_LOG( FG_SERIAL, FG_DEBUG, gga );
318 string rmc_sentence = "$";
321 rmc_sentence += rmc_sum;
322 rmc_sentence += "\n";
323 p->port.write_port(rmc_sentence);
324 // cout << rmc_sentence;
327 string gga_sentence = "$";
330 gga_sentence += gga_sum;
331 gga_sentence += "\n";
332 p->port.write_port(gga_sentence);
333 // cout << gga_sentence;
336 static void read_nmea_in( fgIOCHANNEL *p ) {
339 static void send_garmin_out( fgIOCHANNEL *p ) {
340 char rmc[256], rmc_sum[256], rmz[256], rmz_sum[256];
347 f = current_aircraft.fdm_state;
348 t = FGTime::cur_time_params;
350 // run once per second
351 if ( p->last_time == t->get_cur_time() ) {
354 p->last_time = t->get_cur_time();
355 if ( t->get_cur_time() % 2 != 0 ) {
360 sprintf( utc, "%02d%02d%02d",
361 t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
364 double latd = f->get_Latitude() * RAD_TO_DEG;
372 min = (latd - (double)deg) * 60.0;
373 sprintf( lat, "%02d%06.3f,%c", abs(deg), min, dir);
376 double lond = f->get_Longitude() * RAD_TO_DEG;
384 min = (lond - (double)deg) * 60.0;
385 sprintf( lon, "%03d%06.3f,%c", abs(deg), min, dir);
388 sprintf( speed, "%05.1f", f->get_V_equiv_kts() );
391 sprintf( heading, "%05.1f", f->get_Psi() * RAD_TO_DEG );
394 sprintf( altitude_m, "%02d", (int)(f->get_Altitude() * FEET_TO_METER) );
396 char altitude_ft[10];
397 sprintf( altitude_ft, "%02d", (int)f->get_Altitude() );
400 sprintf( date, "%02d%02d%02d", t->getGmt()->tm_mday,
401 t->getGmt()->tm_mon+1, t->getGmt()->tm_year );
403 // $GPRMC,HHMMSS,A,DDMM.MMM,N,DDDMM.MMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E*XX
404 sprintf( rmc, "GPRMC,%s,A,%s,%s,%s,%s,%s,000.0,E",
405 utc, lat, lon, speed, heading, date );
406 sprintf( rmc_sum, "%02X", calc_nmea_cksum(rmc) );
408 // sprintf( gga, "$GPGGA,%s,%s,%s,1,04,0.0,%s,M,00.0,M,,*00\r\n",
409 // utc, lat, lon, altitude_m );
411 sprintf( rmz, "PGRMZ,%s,f,3", altitude_ft );
412 sprintf( rmz_sum, "%02X", calc_nmea_cksum(rmz) );
414 FG_LOG( FG_SERIAL, FG_DEBUG, rmc );
415 FG_LOG( FG_SERIAL, FG_DEBUG, rmz );
418 string rmc_sentence = "$";
421 rmc_sentence += rmc_sum;
422 rmc_sentence += "\n";
423 p->port.write_port(rmc_sentence);
424 // cout << rmc_sentence;
426 // RMZ sentence (garmin proprietary)
427 string rmz_sentence = "$";
430 rmz_sentence += rmz_sum;
431 rmz_sentence += "\n";
432 p->port.write_port(rmz_sentence);
433 // cout << rmz_sentence;
436 static void read_garmin_in( fgIOCHANNEL *p ) {
439 static void send_fgfs_out( fgIOCHANNEL *p ) {
442 static void read_fgfs_in( fgIOCHANNEL *p ) {
446 // "RUL" output format (for some sort of motion platform)
448 // The Baud rate is 2400 , one start bit, eight data bits, 1 stop bit,
451 // For position it requires a 3-byte data packet defined as follows:
453 // First bite: ascII character"p" ( 80 decimal )
454 // Second byte X pos. (1-255) 1 being 0* and 255 being 359*
455 // Third byte Y pos.( 1-255) 1 being 0* and 255 359*
457 // So sending 80 127 127 to the two axis motors will position on 180*
458 // The RS- 232 port is a nine pin connector and the only pins used are
461 static void send_rul_out( fgIOCHANNEL *p ) {
467 f = current_aircraft.fdm_state;
468 t = FGTime::cur_time_params;
470 // run as often as possibleonce per second
472 // this runs once per second
473 // if ( p->last_time == t->get_cur_time() ) {
476 // p->last_time = t->get_cur_time();
477 // if ( t->get_cur_time() % 2 != 0 ) {
481 // get roll and pitch, convert to degrees
482 double roll_deg = f->get_Phi() * RAD_TO_DEG;
483 while ( roll_deg < -180.0 ) {
486 while ( roll_deg > 180.0 ) {
490 double pitch_deg = f->get_Theta() * RAD_TO_DEG;
491 while ( pitch_deg < -180.0 ) {
494 while ( pitch_deg > 180.0 ) {
498 // scale roll and pitch to output format (1 - 255)
499 // straight && level == (128, 128)
501 int roll = (int)( (roll_deg+180.0) * 255.0 / 360.0) + 1;
502 int pitch = (int)( (pitch_deg+180.0) * 255.0 / 360.0) + 1;
504 sprintf( rul, "p%c%c\n", roll, pitch);
506 FG_LOG( FG_SERIAL, FG_INFO, "p " << roll << " " << pitch );
508 string rul_sentence = rul;
509 p->port.write_port(rul_sentence);
513 // one more level of indirection ...
514 static void process_port( fgIOCHANNEL *p ) {
515 if ( p->kind == fgIOCHANNEL::FG_SERIAL_NMEA_OUT ) {
517 } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_NMEA_IN ) {
519 } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_GARMIN_OUT ) {
521 } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_GARMIN_IN ) {
523 } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_FGFS_OUT ) {
525 } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_FGFS_IN ) {
527 } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_RUL_OUT ) {
533 // process any serial port work
534 void fgSerialProcess() {
537 io_iterator current = port_list.begin();
538 io_iterator last = port_list.end();
540 for ( ; current != last; ++current ) {
542 if ( port->kind != fgIOCHANNEL::FG_SERIAL_DISABLED ) {
543 process_port ( port );