1 // serial.cxx -- Unix serial I/O support
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.
22 // (Log is kept at end of this file)
27 #include <sys/types.h>
32 #include <Debug/logstream.hxx>
37 fgSERIAL::fgSERIAL() {
41 fgSERIAL::fgSERIAL(const string& device, int baud) {
49 fgSERIAL::~fgSERIAL() {
53 bool fgSERIAL::open_port(const string& device) {
54 struct termios config;
56 if ( (fd = open(device.c_str(), O_RDWR | O_NONBLOCK)) == -1 ) {
57 FG_LOG( FG_SERIAL, FG_ALERT, "Cannot open " << device
58 << " for serial I/O" );
64 // set required port parameters
65 if ( tcgetattr( fd, &config ) != 0 ) {
66 FG_LOG( FG_SERIAL, FG_ALERT, "Unable to poll port settings" );
70 // cfmakeraw( &config );
72 // cout << "config.c_iflag = " << config.c_iflag << endl;
74 // software flow control on
75 // config.c_iflag |= IXON;
76 // config.c_iflag |= IXOFF;
78 // disable hardware flow control
79 // config.c_cflag |= CRTSCTS;
81 // cout << "config.c_iflag = " << config.c_iflag << endl;
83 if ( tcsetattr( fd, TCSANOW, &config ) != 0 ) {
84 FG_LOG( FG_SERIAL, FG_ALERT, "Unable to update port settings" );
91 bool fgSERIAL::set_baud(int baud) {
92 struct termios config;
93 speed_t speed = B9600;
95 if ( tcgetattr( fd, &config ) != 0 ) {
96 FG_LOG( FG_SERIAL, FG_ALERT, "Unable to poll port settings" );
102 } else if ( baud == 1200 ) {
104 } else if ( baud == 2400 ) {
106 } else if ( baud == 4800 ) {
108 } else if ( baud == 9600 ) {
110 } else if ( baud == 19200 ) {
112 } else if ( baud == 38400 ) {
114 } else if ( baud == 57600 ) {
116 } else if ( baud == 115200 ) {
118 #if defined( linux ) || defined( __FreeBSD__ )
119 } else if ( baud == 230400 ) {
123 FG_LOG( FG_SERIAL, FG_ALERT, "Unsupported baud rate " << baud );
127 if ( cfsetispeed( &config, speed ) != 0 ) {
128 FG_LOG( FG_SERIAL, FG_ALERT, "Problem setting input baud rate" );
132 if ( cfsetospeed( &config, speed ) != 0 ) {
133 FG_LOG( FG_SERIAL, FG_ALERT, "Problem setting output baud rate" );
137 if ( tcsetattr( fd, TCSANOW, &config ) != 0 ) {
138 FG_LOG( FG_SERIAL, FG_ALERT, "Unable to update port settings" );
145 string fgSERIAL::read_port() {
146 const int max_count = 1024;
147 char buffer[max_count+1];
151 count = read(fd, buffer, max_count);
152 // cout << "read " << count << " bytes" << endl;
156 if ( errno != EAGAIN ) {
157 FG_LOG( FG_SERIAL, FG_ALERT,
158 "Serial I/O on read, error number = " << errno );
163 buffer[count] = '\0';
170 int fgSERIAL::write_port(const string& value) {
173 count = write(fd, value.c_str(), value.length());
174 // cout << "write '" << value << "' " << count << " bytes" << endl;
176 if ( (int)count != (int)value.length() ) {
177 FG_LOG( FG_SERIAL, FG_ALERT,
178 "Serial I/O on write, error number = " << errno );
186 // Revision 1.5 1998/11/25 01:33:23 curt
187 // Remove call to cfmakeraw()
189 // Revision 1.4 1998/11/23 21:47:00 curt
190 // Cygnus tools compatibility tweaks.
192 // Revision 1.3 1998/11/19 13:52:54 curt
193 // port configuration tweaks & experiments.
195 // Revision 1.2 1998/11/19 03:35:43 curt
198 // Revision 1.1 1998/11/16 13:53:02 curt