diff --git a/Serial/serial.cxx b/Serial/serial.cxx index ed7c0ba3..ed580f8a 100644 --- a/Serial/serial.cxx +++ b/Serial/serial.cxx @@ -22,20 +22,36 @@ // (Log is kept at end of this file) -#include -#include -#include -#include -#include -#include +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "Include/compiler.h" +#ifdef FG_HAVE_STD_INCLUDE +# include +#else +# include +#endif + +#if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ ) + // maybe include something??? +#else +# include +# include +# include +# include +# include +#endif #include #include "serial.hxx" -fgSERIAL::fgSERIAL() { - dev_open = false; +fgSERIAL::fgSERIAL() + : dev_open(false) +{ + // empty } fgSERIAL::fgSERIAL(const string& device, int baud) { @@ -50,11 +66,44 @@ fgSERIAL::~fgSERIAL() { // closing the port here screws us up because if we would even so // much as make a copy of an fgSERIAL object and then delete it, // the file descriptor gets closed. Doh!!! - - // close(fd); } bool fgSERIAL::open_port(const string& device) { + +#if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ ) + + fd = CreateFile( device.c_str(), + GENERIC_READ | GENERIC_WRITE, + 0, // dwShareMode + NULL, // lpSecurityAttributes + OPEN_EXISTING, + FILE_FLAG_OVERLAPPED, + NULL ); + if ( fd == INVALID_HANDLE_VALUE ) + { + LPVOID lpMsgBuf; + FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + GetLastError(), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language + (LPTSTR) &lpMsgBuf, + 0, + NULL ); + + FG_LOG( FG_SERIAL, FG_ALERT, "Error opening serial device \"" + << device << "\" " << (const char*) lpMsgBuf ); + LocalFree( lpMsgBuf ); + return false; + } + + dev_open = true; + return true; + +#else + struct termios config; fd = open(device.c_str(), O_RDWR | O_NONBLOCK); @@ -97,16 +146,29 @@ bool fgSERIAL::open_port(const string& device) { } return true; +#endif } bool fgSERIAL::close_port() { +#if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ ) + CloseHandle( fd ); +#else close(fd); +#endif + return true; } bool fgSERIAL::set_baud(int baud) { + +#if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ ) + + return true; + +#else + struct termios config; speed_t speed = B9600; @@ -158,9 +220,20 @@ bool fgSERIAL::set_baud(int baud) { } return true; + +#endif + } string fgSERIAL::read_port() { + +#if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ ) + + string result = ""; + return result; + +#else + const int max_count = 1024; char buffer[max_count+1]; int count; @@ -183,9 +256,48 @@ string fgSERIAL::read_port() { return result; } + +#endif + } int fgSERIAL::write_port(const string& value) { + +#if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ ) + + LPCVOID lpBuffer = value.c_str(); + DWORD nNumberOfBytesToWrite = value.length(); + DWORD lpNumberOfBytesWritten; + OVERLAPPED lpOverlapped; + + if ( WriteFile( fd, + lpBuffer, + nNumberOfBytesToWrite, + &lpNumberOfBytesWritten, + &lpOverlapped ) == 0 ) + { + LPVOID lpMsgBuf; + FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + GetLastError(), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language + (LPTSTR) &lpMsgBuf, + 0, + NULL ); + + FG_LOG( FG_SERIAL, FG_ALERT, "Serial I/O write error: " + << (const char*) lpMsgBuf ); + LocalFree( lpMsgBuf ); + return int(lpNumberOfBytesWritten); + } + + return int(lpNumberOfBytesWritten); + +#else + static bool error = false; int count; @@ -217,10 +329,29 @@ int fgSERIAL::write_port(const string& value) { } return count; + +#endif + } // $Log$ +// Revision 1.9 1999/02/02 20:13:23 curt +// MSVC++ portability changes by Bernie Bright: +// +// Lib/Serial/serial.[ch]xx: Initial Windows support - incomplete. +// Simulator/Astro/stars.cxx: typo? included instead of +// Simulator/Cockpit/hud.cxx: Added Standard headers +// Simulator/Cockpit/panel.cxx: Redefinition of default parameter +// Simulator/Flight/flight.cxx: Replaced cout with FG_LOG. Deleted +// Simulator/Main/fg_init.cxx: +// Simulator/Main/GLUTmain.cxx: +// Simulator/Main/options.hxx: Shuffled dependency +// Simulator/Objects/material.hxx: +// Simulator/Time/timestamp.hxx: VC++ friend kludge +// Simulator/Scenery/tile.[ch]xx: Fixed using std::X declarations +// Simulator/Main/views.hxx: Added a constant +// // Revision 1.8 1999/01/20 13:42:21 curt // Tweaked FDM interface. // Testing check sum support for NMEA serial output. diff --git a/Serial/serial.hxx b/Serial/serial.hxx index 94e8180d..02e17a9b 100644 --- a/Serial/serial.hxx +++ b/Serial/serial.hxx @@ -30,8 +30,17 @@ # error This library requires C++ #endif +#ifdef HAVE_CONFIG_H +# include +#endif +#include "Include/compiler.h" #include +FG_USING_STD(string); + +#if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ ) +# include +#endif // if someone know how to do this all with C++ streams let me know // #include @@ -39,10 +48,15 @@ class fgSERIAL { +#if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ ) + typedef HANDLE fd_type; +#else + typedef int fd_type; +#endif private: - int fd; + fd_type fd; bool dev_open; public: @@ -66,6 +80,22 @@ public: // $Log$ +// Revision 1.3 1999/02/02 20:13:24 curt +// MSVC++ portability changes by Bernie Bright: +// +// Lib/Serial/serial.[ch]xx: Initial Windows support - incomplete. +// Simulator/Astro/stars.cxx: typo? included instead of +// Simulator/Cockpit/hud.cxx: Added Standard headers +// Simulator/Cockpit/panel.cxx: Redefinition of default parameter +// Simulator/Flight/flight.cxx: Replaced cout with FG_LOG. Deleted +// Simulator/Main/fg_init.cxx: +// Simulator/Main/GLUTmain.cxx: +// Simulator/Main/options.hxx: Shuffled dependency +// Simulator/Objects/material.hxx: +// Simulator/Time/timestamp.hxx: VC++ friend kludge +// Simulator/Scenery/tile.[ch]xx: Fixed using std::X declarations +// Simulator/Main/views.hxx: Added a constant +// // Revision 1.2 1998/11/30 17:15:30 curt // Having the class destructor close the fd was a bad idea ... especially if you // ever make a copy of the instance and then subsequently destroy either.