]> git.mxchange.org Git - simgear.git/blob - Serial/serial.cxx
Having the class destructor close the fd was a bad idea ... especially if you
[simgear.git] / Serial / serial.cxx
1 // serial.cxx -- Unix serial I/O support
2 //
3 // Written by Curtis Olson, started November 1998.
4 //
5 // Copyright (C) 1998  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 // (Log is kept at end of this file)
23
24
25 #include <errno.h>
26 #include <termios.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31
32 #include <Debug/logstream.hxx>
33
34 #include "serial.hxx"
35
36
37 fgSERIAL::fgSERIAL() {
38     dev_open = false;
39 }
40
41 fgSERIAL::fgSERIAL(const string& device, int baud) {
42     open_port(device);
43     
44     if ( dev_open ) {
45         set_baud(baud);
46     }
47 }
48
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!!!
53
54     // close(fd);
55 }
56
57 bool fgSERIAL::open_port(const string& device) {
58     struct termios config;
59
60     fd = open(device.c_str(), O_RDWR | O_NONBLOCK);
61     cout << "Serial fd created = " << fd << endl;
62
63     if ( fd  == -1 ) {
64         FG_LOG( FG_SERIAL, FG_ALERT, "Cannot open " << device
65                 << " for serial I/O" );
66         return false;
67     } else {
68         dev_open = true;
69     }
70
71     // set required port parameters 
72     if ( tcgetattr( fd, &config ) != 0 ) {
73         FG_LOG( FG_SERIAL, FG_ALERT, "Unable to poll port settings" );
74         return false;
75     }
76
77     // cfmakeraw( &config );
78
79     // cout << "config.c_iflag = " << config.c_iflag << endl;
80
81     // software flow control on
82     config.c_iflag |= IXON;
83     config.c_iflag |= IXOFF;
84
85     // config.c_cflag |= CLOCAL;
86
87     // disable hardware flow control
88     config.c_cflag &= ~(CRTSCTS);
89
90     // cout << "config.c_iflag = " << config.c_iflag << endl;
91
92     if ( tcsetattr( fd, TCSANOW, &config ) != 0 ) {
93         FG_LOG( FG_SERIAL, FG_ALERT, "Unable to update port settings" );
94         return false;
95     }
96
97     return true;
98 }
99
100
101 bool fgSERIAL::close_port() {
102     close(fd);
103     return true;
104 }
105
106
107 bool fgSERIAL::set_baud(int baud) {
108     struct termios config;
109     speed_t speed = B9600;
110
111     if ( tcgetattr( fd, &config ) != 0 ) {
112         FG_LOG( FG_SERIAL, FG_ALERT, "Unable to poll port settings" );
113         return false;
114     }
115
116     if ( baud == 300 ) {
117         speed = B300;
118     } else if ( baud == 1200 ) {
119         speed = B1200;
120     } else if ( baud == 2400 ) {
121         speed = B2400;
122     } else if ( baud == 4800 ) {
123         speed = B4800;
124     } else if ( baud == 9600 ) {
125         speed = B9600;
126     } else if ( baud == 19200 ) {
127         speed = B19200;
128     } else if ( baud == 38400 ) {
129         speed = B38400;
130     } else if ( baud == 57600 ) {
131         speed = B57600;
132     } else if ( baud == 115200 ) {
133         speed = B115200;
134 #if defined( linux ) || defined( __FreeBSD__ )
135     } else if ( baud == 230400 ) {
136         speed = B230400;
137 #endif
138     } else {
139         FG_LOG( FG_SERIAL, FG_ALERT, "Unsupported baud rate " << baud );
140         return false;
141     }
142
143     if ( cfsetispeed( &config, speed ) != 0 ) {
144         FG_LOG( FG_SERIAL, FG_ALERT, "Problem setting input baud rate" );
145         return false;
146     }
147
148     if ( cfsetospeed( &config, speed ) != 0 ) {
149         FG_LOG( FG_SERIAL, FG_ALERT, "Problem setting output baud rate" );
150         return false;
151     }
152
153     if ( tcsetattr( fd, TCSANOW, &config ) != 0 ) {
154         FG_LOG( FG_SERIAL, FG_ALERT, "Unable to update port settings" );
155         return false;
156     }
157
158     return true;
159 }
160
161 string fgSERIAL::read_port() {
162     const int max_count = 1024;
163     char buffer[max_count+1];
164     int count;
165     string result;
166
167     count = read(fd, buffer, max_count);
168     // cout << "read " << count << " bytes" << endl;
169
170     if ( count < 0 ) {
171         // error condition
172         if ( errno != EAGAIN ) {
173             FG_LOG( FG_SERIAL, FG_ALERT, 
174                     "Serial I/O on read, error number = " << errno );
175         }
176
177         return "";
178     } else {
179         buffer[count] = '\0';
180         result = buffer;
181
182         return result;
183     }
184 }
185
186 int fgSERIAL::write_port(const string& value) {
187     int count;
188
189     count = write(fd, value.c_str(), value.length());
190     // cout << "write '" << value << "'  " << count << " bytes" << endl;
191
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
196         } else {
197             FG_LOG( FG_SERIAL, FG_ALERT,
198                     "Serial I/O on write, error number = " << errno );
199         }
200     }
201
202     return count;
203 }
204
205
206 // $Log$
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.
211 //
212 // Revision 1.5  1998/11/25 01:33:23  curt
213 // Remove call to cfmakeraw()
214 //
215 // Revision 1.4  1998/11/23 21:47:00  curt
216 // Cygnus tools compatibility tweaks.
217 //
218 // Revision 1.3  1998/11/19 13:52:54  curt
219 // port configuration tweaks & experiments.
220 //
221 // Revision 1.2  1998/11/19 03:35:43  curt
222 // Updates ...
223 //
224 // Revision 1.1  1998/11/16 13:53:02  curt
225 // Initial revision.
226 //