Initial source commit
This commit is contained in:
commit
f1384c11ee
335 changed files with 52715 additions and 0 deletions
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