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() {
50 // closing the port here screws us up because if we would even so
51 // much as make a copy of an fgSERIAL object and then delete it,
52 // the file descriptor gets closed. Doh!!!
57 bool fgSERIAL::open_port(const string& device) {
58 struct termios config;
60 fd = open(device.c_str(), O_RDWR | O_NONBLOCK);
61 cout << "Serial fd created = " << fd << endl;
64 FG_LOG( FG_SERIAL, FG_ALERT, "Cannot open " << device
65 << " for serial I/O" );
71 // set required port parameters
72 if ( tcgetattr( fd, &config ) != 0 ) {
73 FG_LOG( FG_SERIAL, FG_ALERT, "Unable to poll port settings" );
77 // cfmakeraw( &config );
79 // cout << "config.c_iflag = " << config.c_iflag << endl;
81 // software flow control on
82 config.c_iflag |= IXON;
83 config.c_iflag |= IXOFF;
85 // config.c_cflag |= CLOCAL;
87 // disable hardware flow control
88 config.c_cflag &= ~(CRTSCTS);
90 // cout << "config.c_iflag = " << config.c_iflag << endl;
92 if ( tcsetattr( fd, TCSANOW, &config ) != 0 ) {
93 FG_LOG( FG_SERIAL, FG_ALERT, "Unable to update port settings" );
101 bool fgSERIAL::close_port() {
107 bool fgSERIAL::set_baud(int baud) {
108 struct termios config;
109 speed_t speed = B9600;
111 if ( tcgetattr( fd, &config ) != 0 ) {
112 FG_LOG( FG_SERIAL, FG_ALERT, "Unable to poll port settings" );
118 } else if ( baud == 1200 ) {
120 } else if ( baud == 2400 ) {
122 } else if ( baud == 4800 ) {
124 } else if ( baud == 9600 ) {
126 } else if ( baud == 19200 ) {
128 } else if ( baud == 38400 ) {
130 } else if ( baud == 57600 ) {
132 } else if ( baud == 115200 ) {
134 #if defined( linux ) || defined( __FreeBSD__ )
135 } else if ( baud == 230400 ) {
139 FG_LOG( FG_SERIAL, FG_ALERT, "Unsupported baud rate " << baud );
143 if ( cfsetispeed( &config, speed ) != 0 ) {
144 FG_LOG( FG_SERIAL, FG_ALERT, "Problem setting input baud rate" );
148 if ( cfsetospeed( &config, speed ) != 0 ) {
149 FG_LOG( FG_SERIAL, FG_ALERT, "Problem setting output baud rate" );
153 if ( tcsetattr( fd, TCSANOW, &config ) != 0 ) {
154 FG_LOG( FG_SERIAL, FG_ALERT, "Unable to update port settings" );
161 string fgSERIAL::read_port() {
162 const int max_count = 1024;
163 char buffer[max_count+1];
167 count = read(fd, buffer, max_count);
168 // cout << "read " << count << " bytes" << endl;
172 if ( errno != EAGAIN ) {
173 FG_LOG( FG_SERIAL, FG_ALERT,
174 "Serial I/O on read, error number = " << errno );
179 buffer[count] = '\0';
186 int fgSERIAL::write_port(const string& value) {
189 count = write(fd, value.c_str(), value.length());
190 // cout << "write '" << value << "' " << count << " bytes" << endl;
192 if ( (int)count != (int)value.length() ) {
193 if ( errno == EAGAIN ) {
194 // ok ... in our context we don't really care if we can't
195 // write a string, we'll just get it the next time around
197 FG_LOG( FG_SERIAL, FG_ALERT,
198 "Serial I/O on write, error number = " << errno );
207 // Revision 1.6 1998/11/30 17:15:29 curt
208 // Having the class destructor close the fd was a bad idea ... especially if you
209 // ever make a copy of the instance and then subsequently destroy either.
210 // close_port() is now a separate member function.
212 // Revision 1.5 1998/11/25 01:33:23 curt
213 // Remove call to cfmakeraw()
215 // Revision 1.4 1998/11/23 21:47:00 curt
216 // Cygnus tools compatibility tweaks.
218 // Revision 1.3 1998/11/19 13:52:54 curt
219 // port configuration tweaks & experiments.
221 // Revision 1.2 1998/11/19 03:35:43 curt
224 // Revision 1.1 1998/11/16 13:53:02 curt