1 // serial.cxx -- Unix serial I/O support
3 // Written by Curtis Olson, started November 1998.
5 // Copyright (C) 1998 Curtis L. Olson - http://www.flightgear.org/~curt
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // Library 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include <simgear/compiler.h>
28 #ifdef SG_HAVE_STD_INCLUDE
34 #if !defined( WIN32 ) || defined( __CYGWIN__) || defined( __CYGWIN32__ )
36 # include <sys/types.h>
37 # include <sys/stat.h>
42 #include <simgear/debug/logstream.hxx>
46 SGSerialPort::SGSerialPort()
52 SGSerialPort::SGSerialPort(const string& device, int baud) {
60 SGSerialPort::~SGSerialPort() {
61 // closing the port here screws us up because if we would even so
62 // much as make a copy of an SGSerialPort object and then delete it,
63 // the file descriptor gets closed. Doh!!!
66 bool SGSerialPort::open_port(const string& device) {
68 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
70 fd = CreateFile( device.c_str(),
71 GENERIC_READ | GENERIC_WRITE,
73 NULL, // lpSecurityAttributes
77 if ( fd == INVALID_HANDLE_VALUE )
81 FORMAT_MESSAGE_ALLOCATE_BUFFER |
82 FORMAT_MESSAGE_FROM_SYSTEM |
83 FORMAT_MESSAGE_IGNORE_INSERTS,
86 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
91 SG_LOG( SG_IO, SG_ALERT, "Error opening serial device \""
92 << device << "\" " << (const char*) lpMsgBuf );
93 LocalFree( lpMsgBuf );
102 struct termios config;
104 fd = open(device.c_str(), O_RDWR | O_NOCTTY| O_NDELAY);
105 SG_LOG( SG_EVENT, SG_DEBUG, "Serial fd created = " << fd);
108 SG_LOG( SG_IO, SG_ALERT, "Cannot open " << device
109 << " for serial I/O" );
115 // set required port parameters
116 if ( tcgetattr( fd, &config ) != 0 ) {
117 SG_LOG( SG_IO, SG_ALERT, "Unable to poll port settings" );
121 // cfmakeraw( &config );
123 // cout << "config.c_iflag = " << config.c_iflag << endl;
125 // disable software flow control
126 config.c_iflag &= ~(IXON | IXOFF | IXANY);
128 // enable the receiver and set local mode
129 config.c_cflag |= (CLOCAL | CREAD);
131 #if !defined( sgi ) && !defined(_AIX)
132 // disable hardware flow control
133 config.c_cflag &= ~(CRTSCTS);
136 // cout << "config.c_iflag = " << config.c_iflag << endl;
138 // Raw (not cooked/canonical) input mode
139 config.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
141 if ( tcsetattr( fd, TCSANOW, &config ) != 0 ) {
142 SG_LOG( SG_IO, SG_ALERT, "Unable to update port settings" );
151 bool SGSerialPort::close_port() {
152 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
164 bool SGSerialPort::set_baud(int baud) {
166 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
169 if ( GetCommState( fd, &dcb ) ) {
171 dcb.fOutxCtsFlow = FALSE;
172 dcb.fOutxDsrFlow = FALSE;
176 if ( !SetCommState( fd, &dcb ) ) {
179 FORMAT_MESSAGE_ALLOCATE_BUFFER |
180 FORMAT_MESSAGE_FROM_SYSTEM |
181 FORMAT_MESSAGE_IGNORE_INSERTS,
184 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
189 SG_LOG( SG_IO, SG_ALERT, "Unable to update port settings: "
190 << (const char*) lpMsgBuf );
191 LocalFree( lpMsgBuf );
197 FORMAT_MESSAGE_ALLOCATE_BUFFER |
198 FORMAT_MESSAGE_FROM_SYSTEM |
199 FORMAT_MESSAGE_IGNORE_INSERTS,
202 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
207 SG_LOG( SG_IO, SG_ALERT, "Unable to poll port settings: "
208 << (const char*) lpMsgBuf );
209 LocalFree( lpMsgBuf );
217 struct termios config;
218 speed_t speed = B9600;
220 if ( tcgetattr( fd, &config ) != 0 ) {
221 SG_LOG( SG_IO, SG_ALERT, "Unable to poll port settings" );
227 } else if ( baud == 1200 ) {
229 } else if ( baud == 2400 ) {
231 } else if ( baud == 4800 ) {
233 } else if ( baud == 9600 ) {
235 } else if ( baud == 19200 ) {
237 } else if ( baud == 38400 ) {
239 #if defined( linux ) || defined( __FreeBSD__ )
240 } else if ( baud == 57600 ) {
242 } else if ( baud == 115200 ) {
244 } else if ( baud == 230400 ) {
248 SG_LOG( SG_IO, SG_ALERT, "Unsupported baud rate " << baud );
252 if ( cfsetispeed( &config, speed ) != 0 ) {
253 SG_LOG( SG_IO, SG_ALERT, "Problem setting input baud rate" );
257 if ( cfsetospeed( &config, speed ) != 0 ) {
258 SG_LOG( SG_IO, SG_ALERT, "Problem setting output baud rate" );
262 if ( tcsetattr( fd, TCSANOW, &config ) != 0 ) {
263 SG_LOG( SG_IO, SG_ALERT, "Unable to update port settings" );
273 string SGSerialPort::read_port() {
275 const int max_count = 1024;
276 char buffer[max_count+1];
279 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
282 if ( ReadFile( fd, buffer, max_count, &count, 0 ) ) {
283 buffer[count] = '\0';
288 FORMAT_MESSAGE_ALLOCATE_BUFFER |
289 FORMAT_MESSAGE_FROM_SYSTEM |
290 FORMAT_MESSAGE_IGNORE_INSERTS,
293 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
298 SG_LOG( SG_IO, SG_ALERT, "Serial I/O read error: "
299 << (const char*) lpMsgBuf );
300 LocalFree( lpMsgBuf );
307 int count = read(fd, buffer, max_count);
308 // cout << "read " << count << " bytes" << endl;
312 if ( errno != EAGAIN ) {
313 SG_LOG( SG_IO, SG_ALERT,
314 "Serial I/O on read, error number = " << errno );
319 buffer[count] = '\0';
329 int SGSerialPort::read_port(char *buf, int len) {
331 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
334 if ( ReadFile( fd, buf, len, &count, 0 ) ) {
341 FORMAT_MESSAGE_ALLOCATE_BUFFER |
342 FORMAT_MESSAGE_FROM_SYSTEM |
343 FORMAT_MESSAGE_IGNORE_INSERTS,
346 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
351 SG_LOG( SG_IO, SG_ALERT, "Serial I/O read error: "
352 << (const char*) lpMsgBuf );
353 LocalFree( lpMsgBuf );
363 int count = read(fd, buf, len);
364 // cout << "read " << count << " bytes" << endl;
368 if ( errno != EAGAIN ) {
369 SG_LOG( SG_IO, SG_ALERT,
370 "Serial I/O on read, error number = " << errno );
386 int SGSerialPort::write_port(const string& value) {
388 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
390 LPCVOID lpBuffer = value.data();
391 DWORD nNumberOfBytesToWrite = value.length();
392 DWORD lpNumberOfBytesWritten;
396 nNumberOfBytesToWrite,
397 &lpNumberOfBytesWritten,
402 FORMAT_MESSAGE_ALLOCATE_BUFFER |
403 FORMAT_MESSAGE_FROM_SYSTEM |
404 FORMAT_MESSAGE_IGNORE_INSERTS,
407 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
412 SG_LOG( SG_IO, SG_ALERT, "Serial I/O write error: "
413 << (const char*) lpMsgBuf );
414 LocalFree( lpMsgBuf );
415 return int(lpNumberOfBytesWritten);
418 return int(lpNumberOfBytesWritten);
422 static bool error = false;
426 SG_LOG( SG_IO, SG_ALERT, "attempting serial write error recovery" );
427 // attempt some sort of error recovery
428 count = write(fd, "\n", 1);
430 // cout << "Serial error recover successful!\n";
437 count = write(fd, value.c_str(), value.length());
438 // cout << "write '" << value << "' " << count << " bytes" << endl;
440 if ( (int)count == (int)value.length() ) {
443 if ( errno == EAGAIN ) {
444 // ok ... in our context we don't really care if we can't
445 // write a string, we'll just get it the next time around
449 SG_LOG( SG_IO, SG_ALERT,
450 "Serial I/O on write, error number = " << errno );
461 int SGSerialPort::write_port(const char* buf, int len) {
462 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
464 LPCVOID lpBuffer = buf;
465 DWORD nNumberOfBytesToWrite = len;
466 DWORD lpNumberOfBytesWritten;
470 nNumberOfBytesToWrite,
471 &lpNumberOfBytesWritten,
476 FORMAT_MESSAGE_ALLOCATE_BUFFER |
477 FORMAT_MESSAGE_FROM_SYSTEM |
478 FORMAT_MESSAGE_IGNORE_INSERTS,
481 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
486 SG_LOG( SG_IO, SG_ALERT, "Serial I/O write error: "
487 << (const char*) lpMsgBuf );
488 LocalFree( lpMsgBuf );
489 return int(lpNumberOfBytesWritten);
492 return int(lpNumberOfBytesWritten);
496 static bool error = false;
500 // attempt some sort of error recovery
501 count = write(fd, "\n", 1);
503 // cout << "Serial error recover successful!\n";
510 count = write(fd, buf, len);
511 // cout << "write '" << buf << "' " << count << " bytes" << endl;
513 if ( (int)count == len ) {
517 if ( errno == EAGAIN ) {
518 // ok ... in our context we don't really care if we can't
519 // write a string, we'll just get it the next time around
521 SG_LOG( SG_IO, SG_ALERT,
522 "Serial I/O on write, error number = " << errno );