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)
26 #include <sys/types.h>
31 #include <Debug/logstream.hxx>
36 fgSERIAL::fgSERIAL() {
40 fgSERIAL::fgSERIAL(const string& device, int baud) {
48 fgSERIAL::~fgSERIAL() {
52 bool fgSERIAL::open_port(const string& device) {
53 struct termios config;
55 if ( (fd = open(device.c_str(), O_RDWR | O_NONBLOCK)) == -1 ) {
56 FG_LOG( FG_SERIAL, FG_ALERT, "Cannot open " << device
57 << " for serial I/O" );
63 // set software flow control
64 if ( tcgetattr( fd, &config ) != 0 ) {
65 FG_LOG( FG_SERIAL, FG_ALERT, "Unable to poll port settings" );
69 config.c_iflag |= IXON;
70 config.c_iflag |= IXOFF;
72 if ( tcsetattr( fd, TCSANOW, &config ) != 0 ) {
73 FG_LOG( FG_SERIAL, FG_ALERT, "Unable to update port settings" );
80 bool fgSERIAL::set_baud(int baud) {
81 struct termios config;
84 if ( tcgetattr( fd, &config ) != 0 ) {
85 FG_LOG( FG_SERIAL, FG_ALERT, "Unable to poll port settings" );
91 } else if ( baud == 1200 ) {
93 } else if ( baud == 2400 ) {
95 } else if ( baud == 4800 ) {
97 } else if ( baud == 9600 ) {
99 } else if ( baud == 19200 ) {
101 } else if ( baud == 38400 ) {
103 } else if ( baud == 57600 ) {
105 } else if ( baud == 115200 ) {
107 } else if ( baud == 230400 ) {
110 FG_LOG( FG_SERIAL, FG_ALERT, "Unsupported baud rate " << baud );
114 if ( cfsetispeed( &config, speed ) != 0 ) {
115 FG_LOG( FG_SERIAL, FG_ALERT, "Problem setting input baud rate" );
119 if ( cfsetospeed( &config, speed ) != 0 ) {
120 FG_LOG( FG_SERIAL, FG_ALERT, "Problem setting output baud rate" );
124 if ( tcsetattr( fd, TCSANOW, &config ) != 0 ) {
125 FG_LOG( FG_SERIAL, FG_ALERT, "Unable to update port settings" );
132 string fgSERIAL::read_port() {
133 const int max_count = 1024;
134 char buffer[max_count+1];
138 count = read(fd, buffer, max_count);
139 // cout << "read " << count << " bytes" << endl;
143 if ( errno != EAGAIN ) {
144 FG_LOG( FG_SERIAL, FG_ALERT,
145 "Serial I/O on read, error number = " << errno );
150 buffer[count] = '\0';
157 int fgSERIAL::write_port(const string& value) {
160 count = write(fd, value.c_str(), value.length());
161 // cout << "write '" << value << "' " << count << " bytes" << endl;
163 if ( (int)count != (int)value.length() ) {
164 FG_LOG( FG_SERIAL, FG_ALERT,
165 "Serial I/O on write, error number = " << errno );
173 // Revision 1.2 1998/11/19 03:35:43 curt
176 // Revision 1.1 1998/11/16 13:53:02 curt