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.
28 #include <simgear/compiler.h>
30 #ifdef FG_HAVE_STD_INCLUDE
36 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
37 // maybe include something???
40 # include <sys/types.h>
41 # include <sys/stat.h>
46 #include <simgear/debug/logstream.hxx>
51 FGSerialPort::FGSerialPort()
57 FGSerialPort::FGSerialPort(const string& device, int baud) {
65 FGSerialPort::~FGSerialPort() {
66 // closing the port here screws us up because if we would even so
67 // much as make a copy of an FGSerialPort object and then delete it,
68 // the file descriptor gets closed. Doh!!!
71 bool FGSerialPort::open_port(const string& device) {
73 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
75 fd = CreateFile( device.c_str(),
76 GENERIC_READ | GENERIC_WRITE,
78 NULL, // lpSecurityAttributes
82 if ( fd == INVALID_HANDLE_VALUE )
86 FORMAT_MESSAGE_ALLOCATE_BUFFER |
87 FORMAT_MESSAGE_FROM_SYSTEM |
88 FORMAT_MESSAGE_IGNORE_INSERTS,
91 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
96 FG_LOG( FG_IO, FG_ALERT, "Error opening serial device \""
97 << device << "\" " << (const char*) lpMsgBuf );
98 LocalFree( lpMsgBuf );
107 struct termios config;
109 fd = open(device.c_str(), O_RDWR | O_NONBLOCK);
110 cout << "Serial fd created = " << fd << endl;
113 FG_LOG( FG_IO, FG_ALERT, "Cannot open " << device
114 << " for serial I/O" );
120 // set required port parameters
121 if ( tcgetattr( fd, &config ) != 0 ) {
122 FG_LOG( FG_IO, FG_ALERT, "Unable to poll port settings" );
126 // cfmakeraw( &config );
128 // cout << "config.c_iflag = " << config.c_iflag << endl;
130 // software flow control on
131 config.c_iflag |= IXON;
132 // config.c_iflag |= IXOFF;
134 // config.c_cflag |= CLOCAL;
137 // disable hardware flow control
138 config.c_cflag &= ~(CRTSCTS);
141 // cout << "config.c_iflag = " << config.c_iflag << endl;
143 if ( tcsetattr( fd, TCSANOW, &config ) != 0 ) {
144 FG_LOG( FG_IO, FG_ALERT, "Unable to update port settings" );
153 bool FGSerialPort::close_port() {
154 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
166 bool FGSerialPort::set_baud(int baud) {
168 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
174 struct termios config;
175 speed_t speed = B9600;
177 if ( tcgetattr( fd, &config ) != 0 ) {
178 FG_LOG( FG_IO, FG_ALERT, "Unable to poll port settings" );
184 } else if ( baud == 1200 ) {
186 } else if ( baud == 2400 ) {
188 } else if ( baud == 4800 ) {
190 } else if ( baud == 9600 ) {
192 } else if ( baud == 19200 ) {
194 } else if ( baud == 38400 ) {
196 } else if ( baud == 57600 ) {
198 } else if ( baud == 115200 ) {
200 #if defined( linux ) || defined( __FreeBSD__ )
201 } else if ( baud == 230400 ) {
205 FG_LOG( FG_IO, FG_ALERT, "Unsupported baud rate " << baud );
209 if ( cfsetispeed( &config, speed ) != 0 ) {
210 FG_LOG( FG_IO, FG_ALERT, "Problem setting input baud rate" );
214 if ( cfsetospeed( &config, speed ) != 0 ) {
215 FG_LOG( FG_IO, FG_ALERT, "Problem setting output baud rate" );
219 if ( tcsetattr( fd, TCSANOW, &config ) != 0 ) {
220 FG_LOG( FG_IO, FG_ALERT, "Unable to update port settings" );
230 string FGSerialPort::read_port() {
232 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
239 const int max_count = 1024;
240 char buffer[max_count+1];
244 count = read(fd, buffer, max_count);
245 // cout << "read " << count << " bytes" << endl;
249 if ( errno != EAGAIN ) {
250 FG_LOG( FG_IO, FG_ALERT,
251 "Serial I/O on read, error number = " << errno );
256 buffer[count] = '\0';
266 int FGSerialPort::read_port(char *buf, int len) {
268 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
277 int count = read(fd, buf, len);
278 // cout << "read " << count << " bytes" << endl;
282 if ( errno != EAGAIN ) {
283 FG_LOG( FG_IO, FG_ALERT,
284 "Serial I/O on read, error number = " << errno );
300 int FGSerialPort::write_port(const string& value) {
302 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
304 LPCVOID lpBuffer = value.c_str();
305 DWORD nNumberOfBytesToWrite = value.length();
306 DWORD lpNumberOfBytesWritten;
307 OVERLAPPED lpOverlapped;
311 nNumberOfBytesToWrite,
312 &lpNumberOfBytesWritten,
313 &lpOverlapped ) == 0 )
317 FORMAT_MESSAGE_ALLOCATE_BUFFER |
318 FORMAT_MESSAGE_FROM_SYSTEM |
319 FORMAT_MESSAGE_IGNORE_INSERTS,
322 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
327 FG_LOG( FG_IO, FG_ALERT, "Serial I/O write error: "
328 << (const char*) lpMsgBuf );
329 LocalFree( lpMsgBuf );
330 return int(lpNumberOfBytesWritten);
333 return int(lpNumberOfBytesWritten);
337 static bool error = false;
341 FG_LOG( FG_IO, FG_ALERT, "attempting serial write error recovery" );
342 // attempt some sort of error recovery
343 count = write(fd, "\n", 1);
345 // cout << "Serial error recover successful!\n";
352 count = write(fd, value.c_str(), value.length());
353 // cout << "write '" << value << "' " << count << " bytes" << endl;
355 if ( (int)count == (int)value.length() ) {
358 if ( errno == EAGAIN ) {
359 // ok ... in our context we don't really care if we can't
360 // write a string, we'll just get it the next time around
364 FG_LOG( FG_IO, FG_ALERT,
365 "Serial I/O on write, error number = " << errno );
376 int FGSerialPort::write_port(const char* buf, int len) {
377 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
379 LPCVOID lpBuffer = buf;
380 DWORD nNumberOfBytesToWrite = len;
381 DWORD lpNumberOfBytesWritten;
382 OVERLAPPED lpOverlapped;
386 nNumberOfBytesToWrite,
387 &lpNumberOfBytesWritten,
388 &lpOverlapped ) == 0 )
392 FORMAT_MESSAGE_ALLOCATE_BUFFER |
393 FORMAT_MESSAGE_FROM_SYSTEM |
394 FORMAT_MESSAGE_IGNORE_INSERTS,
397 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
402 FG_LOG( FG_IO, FG_ALERT, "Serial I/O write error: "
403 << (const char*) lpMsgBuf );
404 LocalFree( lpMsgBuf );
405 return int(lpNumberOfBytesWritten);
408 return int(lpNumberOfBytesWritten);
412 static bool error = false;
416 // attempt some sort of error recovery
417 count = write(fd, "\n", 1);
419 // cout << "Serial error recover successful!\n";
426 count = write(fd, buf, len);
427 // cout << "write '" << buf << "' " << count << " bytes" << endl;
429 if ( (int)count == len ) {
433 if ( errno == EAGAIN ) {
434 // ok ... in our context we don't really care if we can't
435 // write a string, we'll just get it the next time around
437 FG_LOG( FG_IO, FG_ALERT,
438 "Serial I/O on write, error number = " << errno );