Initial source commit
This commit is contained in:
commit
f1384c11ee
335 changed files with 52715 additions and 0 deletions
108
minorGems/io/serialPort/SerialPort.h
Normal file
108
minorGems/io/serialPort/SerialPort.h
Normal file
|
@ -0,0 +1,108 @@
|
|||
/*
|
||||
* Modification History
|
||||
*
|
||||
* 2003-February-17 Jason Rohrer
|
||||
* Created.
|
||||
*
|
||||
* 2003-April-4 Jason Rohrer
|
||||
* Added function for dumping the read buffer.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#ifndef SERIAL_PORT_INCLUDED
|
||||
#define SERIAL_PORT_INCLUDED
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Serial port.
|
||||
*
|
||||
* Note: Implementation for the functions defined here is provided
|
||||
* separately for each platform (in the mac/ linux/ and win32/
|
||||
* subdirectories).
|
||||
*
|
||||
* @author Jason Rohrer
|
||||
*/
|
||||
class SerialPort {
|
||||
|
||||
|
||||
|
||||
public:
|
||||
|
||||
|
||||
|
||||
static const int PARITY_NONE = 0;
|
||||
static const int PARITY_EVEN = 1;
|
||||
static const int PARITY_ODD = 2;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a serial port.
|
||||
*
|
||||
* @param inBaud the baud rate.
|
||||
* @param inParity the parity, one of SerialPort:: PARITY_NONE,
|
||||
* PARITY_EVEN, or PARITY_ODD.
|
||||
* @param inDataBits the number of data bits, 5, 6, 7, or 8.
|
||||
* @param inStopBits the number of stop bits, 1 or 2.
|
||||
*/
|
||||
SerialPort( int inBaud, int inParity, int inDataBits, int inStopBits );
|
||||
|
||||
|
||||
|
||||
~SerialPort();
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Sends a line of text through this serial port.
|
||||
*
|
||||
* @param inLine the \0-terminated line of text to send.
|
||||
* Should not contain newline characters.
|
||||
* Must be destroyed by caller if non-const.
|
||||
*
|
||||
* @return 1 if the line was sent successfully,
|
||||
* or -1 for a port error.
|
||||
*/
|
||||
int sendLine( char *inLine );
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Receives a line of text from this serial port.
|
||||
*
|
||||
* @return the read line as a \0-terminated string with end of
|
||||
* line characters included, or NULL for a port error.
|
||||
* Must be destroyed by caller if non-NULL.
|
||||
*/
|
||||
char *receiveLine();
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Discards all characters in the receive buffer, including
|
||||
* unread characters.
|
||||
*
|
||||
* Can be used to recover from buffer overflow problems.
|
||||
*/
|
||||
void dumpReceiveBuffer();
|
||||
|
||||
|
||||
|
||||
private:
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Used for platform-specific implementations.
|
||||
*/
|
||||
void *mNativeObjectPointer;
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif
|
88
minorGems/io/serialPort/SerialPortFromFile.cpp
Normal file
88
minorGems/io/serialPort/SerialPortFromFile.cpp
Normal file
|
@ -0,0 +1,88 @@
|
|||
/*
|
||||
* Modification History
|
||||
*
|
||||
* 2003-March-28 Jason Rohrer
|
||||
* Created.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "minorGems/io/serialPort/SerialPort.h"
|
||||
#include "minorGems/util/stringUtils.h"
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
|
||||
SerialPort::SerialPort( int inBaud, int inParity, int inDataBits,
|
||||
int inStopBits ) {
|
||||
|
||||
FILE *file = fopen( "gpscap.txt", "r" );
|
||||
|
||||
mNativeObjectPointer = file;
|
||||
}
|
||||
|
||||
|
||||
|
||||
SerialPort::~SerialPort() {
|
||||
|
||||
if( mNativeObjectPointer != NULL ) {
|
||||
FILE *file = (FILE *)mNativeObjectPointer;
|
||||
|
||||
fclose( file );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int SerialPort::sendLine( char *inLine ) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
char *SerialPort::receiveLine() {
|
||||
if( mNativeObjectPointer != NULL ) {
|
||||
FILE *file = (FILE *)mNativeObjectPointer;
|
||||
|
||||
char *buffer = new char[500];
|
||||
|
||||
// read up to first newline
|
||||
int index = 0;
|
||||
|
||||
char lastCharRead = (char)getc( file );
|
||||
|
||||
while( lastCharRead != '\n' && index < 499 ) {
|
||||
buffer[index] = lastCharRead;
|
||||
lastCharRead = (char)getc( file );
|
||||
index++;
|
||||
}
|
||||
|
||||
|
||||
char *returnString;
|
||||
|
||||
if( index > 0 ) {
|
||||
buffer[ index ] = '\0';
|
||||
|
||||
returnString = stringDuplicate( buffer );
|
||||
}
|
||||
else {
|
||||
returnString = NULL;
|
||||
}
|
||||
|
||||
delete [] buffer;
|
||||
return returnString;
|
||||
}
|
||||
else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
277
minorGems/io/serialPort/linux/SerialPortLinux.cpp
Normal file
277
minorGems/io/serialPort/linux/SerialPortLinux.cpp
Normal file
|
@ -0,0 +1,277 @@
|
|||
/*
|
||||
* Modification History
|
||||
*
|
||||
* 2003-February-17 Jason Rohrer
|
||||
* Created.
|
||||
*
|
||||
* 2003-April-4 Jason Rohrer
|
||||
* Added function for dumping the read buffer.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "minorGems/io/serialPort/SerialPort.h"
|
||||
#include "minorGems/util/stringUtils.h"
|
||||
|
||||
|
||||
// Much of the code in this implementation was copied
|
||||
// from the Serial Programming FAQ, by Gary Frefking
|
||||
// http://en.tldp.org/HOWTO/Serial-Programming-HOWTO/
|
||||
|
||||
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <termios.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
// baudrate settings are defined in <asm/termbits.h>, which is
|
||||
// included by <termios.h>
|
||||
|
||||
|
||||
|
||||
class LinuxSerialPortObject {
|
||||
|
||||
public:
|
||||
int mFileHandle;
|
||||
struct termios mOldTermIO;
|
||||
};
|
||||
|
||||
|
||||
|
||||
SerialPort::SerialPort( int inBaud, int inParity, int inDataBits,
|
||||
int inStopBits ) {
|
||||
|
||||
int fileHandle = open( "/dev/ttyS0", O_RDWR | O_NOCTTY );
|
||||
|
||||
if( fileHandle < 0 ) {
|
||||
|
||||
mNativeObjectPointer = NULL;
|
||||
}
|
||||
else {
|
||||
|
||||
LinuxSerialPortObject *serialPortObject = new LinuxSerialPortObject();
|
||||
serialPortObject->mFileHandle = fileHandle;
|
||||
|
||||
mNativeObjectPointer = (void *)serialPortObject;
|
||||
|
||||
|
||||
struct termios newTermIO;
|
||||
|
||||
//save current serial port settings
|
||||
tcgetattr( fileHandle, &( serialPortObject->mOldTermIO ) );
|
||||
|
||||
// clear struct for new port settings
|
||||
bzero( &newTermIO, sizeof( newTermIO ) );
|
||||
|
||||
unsigned int baudRate;
|
||||
|
||||
switch( inBaud ) {
|
||||
case 1200:
|
||||
baudRate = B1200;
|
||||
break;
|
||||
case 2400:
|
||||
baudRate = B2400;
|
||||
break;
|
||||
case 4800:
|
||||
baudRate = B4800;
|
||||
break;
|
||||
case 9600:
|
||||
baudRate = B9600;
|
||||
break;
|
||||
case 19200:
|
||||
baudRate = B19200;
|
||||
break;
|
||||
case 38400:
|
||||
baudRate = B38400;
|
||||
break;
|
||||
case 57600:
|
||||
baudRate = B57600;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
unsigned int parity = 0;
|
||||
|
||||
switch( inParity ) {
|
||||
case SerialPort::PARITY_NONE:
|
||||
// disable parity
|
||||
parity = 0;
|
||||
break;
|
||||
case SerialPort::PARITY_EVEN:
|
||||
// enable parity, defaults to even
|
||||
parity = PARENB;
|
||||
break;
|
||||
case SerialPort::PARITY_ODD:
|
||||
// enable parity, and set to odd
|
||||
parity = PARENB | PARODD;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
unsigned int dataBits = 0;
|
||||
switch( inDataBits ) {
|
||||
case 5:
|
||||
dataBits = CS5;
|
||||
break;
|
||||
case 6:
|
||||
dataBits = CS6;
|
||||
break;
|
||||
case 7:
|
||||
dataBits = CS7;
|
||||
break;
|
||||
case 8:
|
||||
dataBits = CS8;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
unsigned int stopBits = 0;
|
||||
switch( inStopBits ) {
|
||||
case 1:
|
||||
stopBits = 0;
|
||||
break;
|
||||
case 2:
|
||||
stopBits = CSTOPB;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
newTermIO.c_cflag = baudRate | parity | dataBits |
|
||||
stopBits | CLOCAL | CREAD;
|
||||
|
||||
/*
|
||||
IGNPAR : ignore bytes with parity errors
|
||||
ICRNL : map CR to NL (otherwise a CR input on the other computer
|
||||
will not terminate input)
|
||||
IGNCR : ignore CR, so only one line is read when other
|
||||
machine sends CRLF
|
||||
otherwise make device raw (no other input processing)
|
||||
*/
|
||||
newTermIO.c_iflag = IGNPAR | ICRNL | IGNCR;
|
||||
|
||||
/*
|
||||
Raw output.
|
||||
*/
|
||||
newTermIO.c_oflag = 0;
|
||||
|
||||
/*
|
||||
ICANON : enable canonical input
|
||||
disable all echo functionality, and don't send signals to
|
||||
calling program
|
||||
*/
|
||||
newTermIO.c_lflag = ICANON;
|
||||
|
||||
/*
|
||||
now clean the modem line and activate the settings for the port
|
||||
*/
|
||||
tcflush( fileHandle, TCIFLUSH );
|
||||
tcsetattr( fileHandle, TCSANOW, &newTermIO );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
SerialPort::~SerialPort() {
|
||||
|
||||
if( mNativeObjectPointer != NULL ) {
|
||||
LinuxSerialPortObject *serialPortObject =
|
||||
(LinuxSerialPortObject *)mNativeObjectPointer;
|
||||
|
||||
int fileHandle = serialPortObject->mFileHandle;
|
||||
|
||||
|
||||
tcsetattr( fileHandle, TCSANOW, &( serialPortObject->mOldTermIO ) );
|
||||
|
||||
|
||||
delete serialPortObject;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int SerialPort::sendLine( char *inLine ) {
|
||||
if( mNativeObjectPointer != NULL ) {
|
||||
LinuxSerialPortObject *serialPortObject =
|
||||
(LinuxSerialPortObject *)mNativeObjectPointer;
|
||||
int fileHandle = serialPortObject->mFileHandle;
|
||||
|
||||
int stringLength = strlen( inLine );
|
||||
int numWritten = write( fileHandle, inLine, stringLength );
|
||||
|
||||
char *endLineString = "\n";
|
||||
|
||||
int numWrittenEndLine = write( fileHandle, endLineString, 1 );
|
||||
|
||||
if( numWritten == stringLength &&
|
||||
numWrittenEndLine == 1 ) {
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
char *SerialPort::receiveLine() {
|
||||
if( mNativeObjectPointer != NULL ) {
|
||||
LinuxSerialPortObject *serialPortObject =
|
||||
(LinuxSerialPortObject *)mNativeObjectPointer;
|
||||
int fileHandle = serialPortObject->mFileHandle;
|
||||
|
||||
char *buffer = new char[500];
|
||||
|
||||
int numRead = read( fileHandle, buffer, 500 );
|
||||
|
||||
char *returnString;
|
||||
|
||||
if( numRead != -1 ) {
|
||||
buffer[ numRead ] = '\0';
|
||||
|
||||
returnString = stringDuplicate( buffer );
|
||||
}
|
||||
else {
|
||||
returnString = NULL;
|
||||
}
|
||||
|
||||
delete [] buffer;
|
||||
return returnString;
|
||||
}
|
||||
else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void SerialPort::dumpReceiveBuffer() {
|
||||
if( mNativeObjectPointer != NULL ) {
|
||||
LinuxSerialPortObject *serialPortObject =
|
||||
(LinuxSerialPortObject *)mNativeObjectPointer;
|
||||
int fileHandle = serialPortObject->mFileHandle;
|
||||
|
||||
// from man page:
|
||||
// flushes data received but not read
|
||||
tcflush( fileHandle, TCIFLUSH );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
27
minorGems/io/serialPort/testSerialPort.cpp
Normal file
27
minorGems/io/serialPort/testSerialPort.cpp
Normal file
|
@ -0,0 +1,27 @@
|
|||
|
||||
#include "SerialPort.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
|
||||
printf( "Constructing serial port.\n" );
|
||||
SerialPort *port = new SerialPort( 4800, SerialPort::PARITY_NONE, 8, 1 );
|
||||
|
||||
char *line = port->receiveLine();
|
||||
|
||||
// specific to GPS unit
|
||||
port->sendLine( "ASTRAL" );
|
||||
|
||||
while( line != NULL ) {
|
||||
printf( "received: %s\n", line );
|
||||
|
||||
delete [] line;
|
||||
line = port->receiveLine();
|
||||
}
|
||||
|
||||
printf( "Deleting serial port.\n" );
|
||||
delete port;
|
||||
|
||||
return 0;
|
||||
}
|
1
minorGems/io/serialPort/testSerialPortCompile
Executable file
1
minorGems/io/serialPort/testSerialPortCompile
Executable file
|
@ -0,0 +1 @@
|
|||
g++ -o testSerialPort -I../../.. linux/SerialPortLinux.cpp testSerialPort.cpp
|
441
minorGems/io/serialPort/win32/COMPort.cpp
Normal file
441
minorGems/io/serialPort/win32/COMPort.cpp
Normal file
|
@ -0,0 +1,441 @@
|
|||
/*
|
||||
* Modification History
|
||||
*
|
||||
* 2003-April-4 Jason Rohrer
|
||||
* Added function for dumping the read buffer.
|
||||
*/
|
||||
|
||||
//=============================================================================
|
||||
// General component library for WIN32
|
||||
// Copyright (C) 2000, UAB BBDSoft ( http://www.bbdsoft.com/ )
|
||||
//
|
||||
// This material is provided "as is", with absolutely no warranty expressed
|
||||
// or implied. Any use is at your own risk.
|
||||
//
|
||||
// Permission to use or copy this software for any purpose is hereby granted
|
||||
// without fee, provided the above notices are retained on all copies.
|
||||
// Permission to modify the code and to distribute modified code is granted,
|
||||
// provided the above notices are retained, and a notice that the code was
|
||||
// modified is included with the above copyright notice.
|
||||
//
|
||||
// The author of this program may be contacted at developers@bbdsoft.com
|
||||
//=============================================================================
|
||||
|
||||
|
||||
#ifndef _COMPORT_
|
||||
#include "ComPort.h"
|
||||
#endif
|
||||
|
||||
#ifndef _WINDOWS_
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#ifndef _STDEXCEPT_
|
||||
#include <stdexcept>
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
COMPort::COMPort ( const char * const portName )
|
||||
: theDCB (NULL)
|
||||
{
|
||||
|
||||
thePortHandle = (unsigned ) CreateFile ( portName
|
||||
, GENERIC_READ | GENERIC_WRITE
|
||||
, 0
|
||||
, NULL
|
||||
, OPEN_EXISTING
|
||||
, FILE_FLAG_NO_BUFFERING
|
||||
, NULL
|
||||
);
|
||||
if (thePortHandle == HFILE_ERROR)
|
||||
{
|
||||
throw runtime_error ("COMPort: failed to open.");
|
||||
} // endif
|
||||
|
||||
theDCB = new char [sizeof(DCB)];
|
||||
getState();
|
||||
setBlockingMode();
|
||||
setHandshaking();
|
||||
|
||||
} // end constructor
|
||||
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
COMPort::~COMPort()
|
||||
{
|
||||
|
||||
delete [] theDCB;
|
||||
|
||||
// close serial port device
|
||||
if (CloseHandle ((HANDLE)thePortHandle) == FALSE )
|
||||
{
|
||||
throw runtime_error ("COMPort: failed to close.");
|
||||
} // endif
|
||||
|
||||
} // end destructor
|
||||
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
void COMPort::getState () const
|
||||
{
|
||||
|
||||
if (!GetCommState ( (HANDLE) thePortHandle
|
||||
, (LPDCB) theDCB
|
||||
)
|
||||
)
|
||||
{
|
||||
throw runtime_error ("COMPort: could not retrieve serial port state.");
|
||||
} // endif
|
||||
|
||||
} // end COMPort::getState () const
|
||||
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
COMPort& COMPort::setState ()
|
||||
{
|
||||
|
||||
if (!SetCommState ( (HANDLE) thePortHandle
|
||||
, (LPDCB) theDCB
|
||||
)
|
||||
)
|
||||
{
|
||||
throw runtime_error ("COMPort: could not modify serial port state.");
|
||||
} // endif
|
||||
|
||||
return *this;
|
||||
} // end COMPort::setState ()
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
COMPort& COMPort::setBitRate ( unsigned long Param )
|
||||
{
|
||||
|
||||
DCB & aDCB = *((LPDCB)theDCB);
|
||||
aDCB.BaudRate = Param;
|
||||
|
||||
return setState();
|
||||
} // end COMPort::setBitRate (..)
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
unsigned long COMPort::bitRate() const
|
||||
{
|
||||
|
||||
DCB & aDCB = *((LPDCB)theDCB);
|
||||
|
||||
return aDCB.BaudRate;
|
||||
} // end COMPort::bitRate () const
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
COMPort& COMPort::setLineCharacteristics( char * inConfig )
|
||||
{
|
||||
|
||||
COMMTIMEOUTS aTimeout;
|
||||
if ( !BuildCommDCBAndTimeouts ( inConfig
|
||||
, (LPDCB)theDCB
|
||||
, &aTimeout
|
||||
)
|
||||
)
|
||||
{
|
||||
throw runtime_error ("COMPort: could not set line characteristics.");
|
||||
} // endif
|
||||
|
||||
if ( ! SetCommTimeouts ( (HANDLE(thePortHandle))
|
||||
, &aTimeout
|
||||
)
|
||||
)
|
||||
{
|
||||
throw runtime_error ("COMPort: could not set line characteristics.");
|
||||
} // endif
|
||||
|
||||
return setState();
|
||||
}
|
||||
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
char COMPort::read ()
|
||||
{
|
||||
|
||||
char buffer;
|
||||
DWORD charsRead = 0;
|
||||
|
||||
do
|
||||
{
|
||||
if (! ReadFile ( (HANDLE(thePortHandle))
|
||||
, &buffer
|
||||
, sizeof(char)
|
||||
, &charsRead
|
||||
, NULL
|
||||
)
|
||||
)
|
||||
{
|
||||
throw runtime_error ("COMPort: read failed.");
|
||||
} // endif
|
||||
|
||||
} while ( !charsRead );
|
||||
|
||||
return buffer;
|
||||
} // end COMPort::read()
|
||||
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
void COMPort::dumpReceiveBuffer ()
|
||||
{
|
||||
PurgeComm( (HANDLE(thePortHandle))
|
||||
, PURGE_RXCLEAR
|
||||
);
|
||||
} // end COMPort::dumpReceiveBuffer()
|
||||
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
unsigned long COMPort::read ( void *inBuffer
|
||||
, const unsigned long inCharsReq
|
||||
)
|
||||
{
|
||||
|
||||
DWORD charsRead = 0;
|
||||
if ( !ReadFile ( (HANDLE(thePortHandle))
|
||||
, inBuffer
|
||||
, inCharsReq
|
||||
, &charsRead
|
||||
, NULL
|
||||
)
|
||||
)
|
||||
{
|
||||
throw runtime_error ("COMPort: read failed.");
|
||||
} // endif
|
||||
|
||||
return charsRead;
|
||||
} // end COMPort::read (..)
|
||||
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
COMPort & COMPort::write ( const char inChar )
|
||||
{
|
||||
|
||||
char buffer = inChar;
|
||||
DWORD charsWritten = 0;
|
||||
|
||||
if ( !WriteFile ( (HANDLE(thePortHandle))
|
||||
, &buffer
|
||||
, sizeof(char)
|
||||
, &charsWritten
|
||||
, NULL
|
||||
)
|
||||
)
|
||||
{
|
||||
throw runtime_error ("COMPort: write failed.");
|
||||
} // endif
|
||||
|
||||
return *this;
|
||||
} // end COMPort::write (..)
|
||||
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
unsigned long COMPort::write ( const void *inBuffer
|
||||
, const unsigned long inBufSize
|
||||
)
|
||||
{
|
||||
|
||||
DWORD charsWritten = 0;
|
||||
|
||||
if ( !WriteFile ( (HANDLE(thePortHandle))
|
||||
, inBuffer
|
||||
, inBufSize
|
||||
, &charsWritten
|
||||
, NULL
|
||||
)
|
||||
)
|
||||
{
|
||||
throw runtime_error ("COMPort: write failed.");
|
||||
} // endif
|
||||
|
||||
return charsWritten;
|
||||
} // end COMPort::write()
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
COMPort& COMPort::setxONxOFF ( bool Param )
|
||||
{
|
||||
|
||||
DCB & aDCB = *((LPDCB)theDCB);
|
||||
aDCB.fOutX = Param ? 1 : 0;
|
||||
aDCB.fInX = Param ? 1 : 0;
|
||||
|
||||
return setState();
|
||||
} // end COMPort::setxONxOFF (..)
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
bool COMPort::isxONxOFF () const
|
||||
{
|
||||
|
||||
DCB & aDCB = *((LPDCB)theDCB);
|
||||
|
||||
return (aDCB.fOutX && aDCB.fInX);
|
||||
} // end COMPort::isxONxOFF () const
|
||||
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
COMPort& COMPort::setBlockingMode ( unsigned long inReadInterval
|
||||
, unsigned long inReadMultiplyer
|
||||
, unsigned long inReadConstant
|
||||
)
|
||||
{
|
||||
|
||||
COMMTIMEOUTS commTimeout;
|
||||
if ( !GetCommTimeouts ( (HANDLE(thePortHandle))
|
||||
, &commTimeout
|
||||
)
|
||||
)
|
||||
{
|
||||
throw runtime_error ("COMPort: failed to retrieve timeouts.");
|
||||
} // endif
|
||||
|
||||
commTimeout.ReadIntervalTimeout = inReadInterval;
|
||||
|
||||
if ( inReadInterval==MAXDWORD )
|
||||
{
|
||||
commTimeout.ReadTotalTimeoutMultiplier = 0;
|
||||
commTimeout.ReadTotalTimeoutConstant = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
commTimeout.ReadTotalTimeoutMultiplier = inReadMultiplyer;
|
||||
commTimeout.ReadTotalTimeoutConstant = inReadConstant;
|
||||
} // endifelse
|
||||
|
||||
if ( !SetCommTimeouts ( (HANDLE(thePortHandle))
|
||||
, &commTimeout
|
||||
)
|
||||
)
|
||||
{
|
||||
throw runtime_error ("COMPort: failed to modify timeouts.");
|
||||
} // endif
|
||||
|
||||
return *this;
|
||||
} // end COMPort::setBlockingMode (..)
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
COMPort & COMPort::setHandshaking ( bool inHandshaking )
|
||||
{
|
||||
|
||||
DCB & aDCB = *((LPDCB)theDCB);
|
||||
if (inHandshaking)
|
||||
{
|
||||
aDCB.fOutxCtsFlow = TRUE;
|
||||
aDCB.fOutxDsrFlow = FALSE;
|
||||
aDCB.fRtsControl = RTS_CONTROL_HANDSHAKE;
|
||||
}
|
||||
else
|
||||
{
|
||||
aDCB.fOutxCtsFlow = FALSE;
|
||||
aDCB.fOutxDsrFlow = FALSE;
|
||||
aDCB.fRtsControl = RTS_CONTROL_ENABLE;
|
||||
} // endifelse
|
||||
|
||||
return setState();
|
||||
} // end COMPort::setHandshaking (..)
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
unsigned long COMPort::getMaximumBitRate() const
|
||||
{
|
||||
|
||||
COMMPROP aProp;
|
||||
if ( !GetCommProperties ( (HANDLE)thePortHandle
|
||||
, &aProp )
|
||||
)
|
||||
{
|
||||
throw runtime_error ("COMPort: failed to retrieve port properties.");
|
||||
} // endif
|
||||
|
||||
return aProp.dwMaxBaud;
|
||||
} // end COMPort::getMaximumBitRate () const
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
COMPort::MSPack COMPort::getModemSignals() const
|
||||
{
|
||||
|
||||
MSPack aPack;
|
||||
// 1 bit - DTR, 2 - bit RTS (output signals)
|
||||
// 4 bit - CTS, 5 bit - DSR, 6 bit - RI, 7 bit - DCD (input signals)
|
||||
if ( !GetCommModemStatus ( (HANDLE)thePortHandle
|
||||
, (LPDWORD)&aPack )
|
||||
)
|
||||
{
|
||||
throw runtime_error ("COMPort: failed to retrieve modem signals.");
|
||||
} // endif
|
||||
|
||||
return aPack;
|
||||
} // end COMPort::getModemSignals () const
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
COMPort& COMPort::setParity ( Parity Param )
|
||||
{
|
||||
|
||||
DCB & aDCB = *((LPDCB)theDCB);
|
||||
aDCB.Parity = Param;
|
||||
|
||||
return setState();
|
||||
} // end COMPort::setParity (..)
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
COMPort& COMPort::setDataBits ( DataBits Param )
|
||||
{
|
||||
|
||||
DCB & aDCB = *((LPDCB)theDCB);
|
||||
aDCB.ByteSize = Param;
|
||||
|
||||
return setState();
|
||||
} // end COMPort::setDataBits (..)
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
COMPort& COMPort::setStopBits ( StopBits Param )
|
||||
{
|
||||
|
||||
DCB & aDCB = *((LPDCB)theDCB);
|
||||
aDCB.StopBits = Param;
|
||||
|
||||
return setState();
|
||||
} // end COMPort::setStopBits (..)
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
COMPort::Parity COMPort::parity () const
|
||||
{
|
||||
|
||||
DCB & aDCB = *((LPDCB)theDCB);
|
||||
|
||||
return (COMPort::Parity)aDCB.Parity;
|
||||
} // end COMPort::parity () const
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
COMPort::DataBits COMPort::dataBits () const
|
||||
{
|
||||
|
||||
DCB & aDCB = *((LPDCB)theDCB);
|
||||
|
||||
return (COMPort::DataBits)aDCB.ByteSize;
|
||||
} // end COMPort::dataBits () const
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
COMPort::StopBits COMPort::stopBits () const
|
||||
{
|
||||
|
||||
DCB & aDCB = *((LPDCB)theDCB);
|
||||
|
||||
return (COMPort::StopBits)aDCB.StopBits;
|
||||
} // end COMPort::stopBits () cosnt
|
||||
|
150
minorGems/io/serialPort/win32/COMPort.h
Normal file
150
minorGems/io/serialPort/win32/COMPort.h
Normal file
|
@ -0,0 +1,150 @@
|
|||
/*
|
||||
* Modification History
|
||||
*
|
||||
* 2003-April-4 Jason Rohrer
|
||||
* Added function for dumping the read buffer.
|
||||
*/
|
||||
|
||||
//=============================================================================
|
||||
// General component library for WIN32
|
||||
// Copyright (C) 2000, UAB BBDSoft ( http://www.bbdsoft.com/ )
|
||||
//
|
||||
// This material is provided "as is", with absolutely no warranty expressed
|
||||
// or implied. Any use is at your own risk.
|
||||
//
|
||||
// Permission to use or copy this software for any purpose is hereby granted
|
||||
// without fee, provided the above notices are retained on all copies.
|
||||
// Permission to modify the code and to distribute modified code is granted,
|
||||
// provided the above notices are retained, and a notice that the code was
|
||||
// modified is included with the above copyright notice.
|
||||
//
|
||||
// The author of this program may be contacted at developers@bbdsoft.com
|
||||
//=============================================================================
|
||||
|
||||
|
||||
#ifndef _COMPORT_
|
||||
#define _COMPORT_
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
class COMPort
|
||||
{
|
||||
public:
|
||||
|
||||
enum Parity
|
||||
{
|
||||
None = 0
|
||||
, Odd
|
||||
, Even
|
||||
, Mark
|
||||
, Space
|
||||
};
|
||||
|
||||
enum DataBits
|
||||
{
|
||||
db4 = 4
|
||||
, db5
|
||||
, db6
|
||||
, db7
|
||||
, db8
|
||||
};
|
||||
|
||||
enum StopBits
|
||||
{
|
||||
sb1 = 0,
|
||||
sb15,
|
||||
sb2
|
||||
};
|
||||
|
||||
enum BitRate
|
||||
{
|
||||
br110 = 110,
|
||||
br300 = 300,
|
||||
br600 = 600,
|
||||
br1200 = 1200,
|
||||
br2400 = 2400,
|
||||
br4800 = 4800,
|
||||
br9600 = 9600,
|
||||
br19200 = 19200,
|
||||
br38400 = 38400,
|
||||
br56000 = 56000,
|
||||
br57600 = 57600,
|
||||
br115200 = 115200,
|
||||
br256000 = 256000
|
||||
};
|
||||
|
||||
|
||||
// for function getModemSignals
|
||||
struct MSPack
|
||||
{
|
||||
unsigned char DTR : 1;
|
||||
unsigned char RTS : 1;
|
||||
unsigned char : 2;
|
||||
unsigned char CTS : 1;
|
||||
unsigned char DSR : 1;
|
||||
unsigned char RI : 1;
|
||||
unsigned char DCD : 1;
|
||||
};
|
||||
|
||||
COMPort ( const char * const portName );
|
||||
~COMPort ();
|
||||
|
||||
// I/O operations
|
||||
char read ();
|
||||
COMPort & write (const char inChar);
|
||||
|
||||
unsigned long read ( void *
|
||||
, const unsigned long count
|
||||
);
|
||||
|
||||
unsigned long write ( const void *
|
||||
, const unsigned long count
|
||||
);
|
||||
// dumps unread characters in the receive buffer
|
||||
void dumpReceiveBuffer();
|
||||
|
||||
|
||||
COMPort& setBitRate ( unsigned long Param );
|
||||
unsigned long bitRate () const;
|
||||
|
||||
COMPort& setParity ( Parity Param );
|
||||
Parity parity () const;
|
||||
|
||||
COMPort& setDataBits ( DataBits Param );
|
||||
DataBits dataBits () const;
|
||||
|
||||
COMPort& setStopBits ( StopBits Param );
|
||||
StopBits stopBits () const;
|
||||
|
||||
COMPort & setHandshaking ( bool inHandshaking = true );
|
||||
|
||||
COMPort& setLineCharacteristics ( char * Param );
|
||||
|
||||
unsigned long getMaximumBitRate () const;
|
||||
|
||||
COMPort & setxONxOFF ( bool Param = true);
|
||||
bool isxONxOFF () const;
|
||||
|
||||
MSPack getModemSignals () const;
|
||||
|
||||
COMPort& setBlockingMode ( unsigned long inReadInterval = 0
|
||||
, unsigned long inReadMultiplyer = 0
|
||||
, unsigned long inReadConstant = 0
|
||||
);
|
||||
|
||||
protected:
|
||||
|
||||
private:
|
||||
|
||||
// disable copy constructor and assignment operator
|
||||
COMPort (const COMPort &);
|
||||
COMPort& operator= (const COMPort &);
|
||||
|
||||
void getState () const;
|
||||
COMPort& setState ();
|
||||
|
||||
unsigned thePortHandle;
|
||||
char * theDCB;
|
||||
|
||||
}; // End of COMPort class declaration
|
||||
|
||||
#endif
|
199
minorGems/io/serialPort/win32/SerialPortWin32.cpp
Normal file
199
minorGems/io/serialPort/win32/SerialPortWin32.cpp
Normal file
|
@ -0,0 +1,199 @@
|
|||
/*
|
||||
* Modification History
|
||||
*
|
||||
* 2003-March-24 Jason Rohrer
|
||||
* Created.
|
||||
*
|
||||
* 2003-March-26 Jason Rohrer
|
||||
* Fixed a line parsing bug (lines end with newline, not CR).
|
||||
*
|
||||
* 2003-April-4 Jason Rohrer
|
||||
* Added function for dumping the read buffer.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "minorGems/io/serialPort/SerialPort.h"
|
||||
#include "minorGems/util/stringUtils.h"
|
||||
|
||||
|
||||
// For now, we are using BBDSoft's COM port code
|
||||
#include "COMPort.h"
|
||||
|
||||
|
||||
|
||||
SerialPort::SerialPort( int inBaud, int inParity, int inDataBits,
|
||||
int inStopBits ) {
|
||||
|
||||
COMPort *port = new COMPort( "COM1" );
|
||||
port->setHandshaking( false );
|
||||
|
||||
unsigned long baudRate = COMPort::br2400;
|
||||
|
||||
switch( inBaud ) {
|
||||
case 1200:
|
||||
baudRate = COMPort::br1200;
|
||||
break;
|
||||
case 2400:
|
||||
baudRate = COMPort::br2400;
|
||||
break;
|
||||
case 4800:
|
||||
baudRate = COMPort::br4800;
|
||||
break;
|
||||
case 9600:
|
||||
baudRate = COMPort::br9600;
|
||||
break;
|
||||
case 19200:
|
||||
baudRate = COMPort::br19200;
|
||||
break;
|
||||
case 38400:
|
||||
baudRate = COMPort::br38400;
|
||||
break;
|
||||
case 57600:
|
||||
baudRate = COMPort::br57600;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
port->setBitRate( baudRate );
|
||||
|
||||
switch( inParity ) {
|
||||
case SerialPort::PARITY_NONE:
|
||||
port->setParity( COMPort::None );
|
||||
break;
|
||||
case SerialPort::PARITY_EVEN:
|
||||
port->setParity( COMPort::Even );
|
||||
break;
|
||||
case SerialPort::PARITY_ODD:
|
||||
port->setParity( COMPort::Odd );
|
||||
break;
|
||||
default:
|
||||
port->setParity( COMPort::None );
|
||||
break;
|
||||
}
|
||||
|
||||
switch( inDataBits ) {
|
||||
case 5:
|
||||
port->setDataBits( COMPort::db5 );
|
||||
break;
|
||||
case 6:
|
||||
port->setDataBits( COMPort::db6 );
|
||||
break;
|
||||
case 7:
|
||||
port->setDataBits( COMPort::db7 );
|
||||
break;
|
||||
case 8:
|
||||
port->setDataBits( COMPort::db8 );
|
||||
break;
|
||||
default:
|
||||
port->setDataBits( COMPort::db8 );
|
||||
break;
|
||||
}
|
||||
|
||||
switch( inStopBits ) {
|
||||
case 1:
|
||||
port->setStopBits( COMPort::sb1 );
|
||||
break;
|
||||
case 2:
|
||||
port->setStopBits( COMPort::sb2 );
|
||||
break;
|
||||
default:
|
||||
port->setStopBits( COMPort::sb1 );
|
||||
break;
|
||||
}
|
||||
|
||||
mNativeObjectPointer = (void *)port;
|
||||
}
|
||||
|
||||
|
||||
|
||||
SerialPort::~SerialPort() {
|
||||
|
||||
if( mNativeObjectPointer != NULL ) {
|
||||
COMPort *port = (COMPort *)mNativeObjectPointer;
|
||||
delete port;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int SerialPort::sendLine( char *inLine ) {
|
||||
if( mNativeObjectPointer != NULL ) {
|
||||
COMPort *port = (COMPort *)mNativeObjectPointer;
|
||||
|
||||
int stringLength = strlen( inLine );
|
||||
int numWritten = port->write( (void *)inLine, stringLength );
|
||||
|
||||
char *endLineString = "\n";
|
||||
|
||||
int numWrittenEndLine = port->write( (void *)endLineString, 1 );
|
||||
|
||||
if( numWritten == stringLength &&
|
||||
numWrittenEndLine == 1 ) {
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
char *SerialPort::receiveLine() {
|
||||
if( mNativeObjectPointer != NULL ) {
|
||||
COMPort *port = (COMPort *)mNativeObjectPointer;
|
||||
|
||||
char *buffer = new char[500];
|
||||
|
||||
// read up to first newline
|
||||
int index = 0;
|
||||
|
||||
char lastCharRead = port->read();
|
||||
|
||||
while( lastCharRead != '\n' && index < 499 ) {
|
||||
buffer[index] = lastCharRead;
|
||||
lastCharRead = port->read();
|
||||
index++;
|
||||
}
|
||||
|
||||
|
||||
char *returnString;
|
||||
|
||||
if( index > 0 ) {
|
||||
buffer[ index ] = '\0';
|
||||
|
||||
returnString = stringDuplicate( buffer );
|
||||
}
|
||||
else {
|
||||
returnString = NULL;
|
||||
}
|
||||
|
||||
delete [] buffer;
|
||||
return returnString;
|
||||
}
|
||||
else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void SerialPort::dumpReceiveBuffer() {
|
||||
if( mNativeObjectPointer != NULL ) {
|
||||
COMPort *port = (COMPort *)mNativeObjectPointer;
|
||||
|
||||
port->dumpReceiveBuffer();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue