18 #define RX_QUEUE_SIZE ( 0x4000 ) 21 #define TX_QUEUE_SIZE ( 0x1000 ) 24 #define DEFAULT_BAUD_RATE ( 9600 ) 74 aList.push_back( tcpSocket );
128 COMMTIMEOUTS timeouts;
129 CString portDeviceFile;
131 portDeviceFile.Format( _T(
"\\\\.\\%s"), port );
134 portDeviceFile.GetString(),
135 GENERIC_READ | GENERIC_WRITE,
139 FILE_ATTRIBUTE_NORMAL,
161 timeouts.ReadIntervalTimeout = 0;
162 timeouts.ReadTotalTimeoutMultiplier = 0;
163 timeouts.ReadTotalTimeoutConstant = 0;
164 timeouts.WriteTotalTimeoutMultiplier = 0;
165 timeouts.WriteTotalTimeoutConstant = 0;
174 if( !PurgeComm(
mComPortHandle, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR ) )
209 uint8 readBuffer[ 256 ];
222 DWORD remainder = status.cbInQue;
223 while( remainder > 0 )
226 readSize =
minval( remainder,
sizeof( readBuffer ) );
228 if( ReadFile(
mComPortHandle, readBuffer, readSize, (LPDWORD)&readSize, NULL ) == 0 )
231 if( ( error & CE_OVERRUN ) ||
232 ( error & CE_RXOVER ) )
247 remainder -= readSize;
268 ASSERT( aData != NULL );
269 ASSERT( aSize != 0 );
271 if( WriteFile(
mComPortHandle, aData, aSize, (LPDWORD)&writeCount, NULL ) == 0 )
274 if( error & CE_TXFULL )
294 std::list<CString> &aList
314 TEXT(
"HARDWARE\\DEVICEMAP\\SERIALCOMM"),
321 retCode = RegQueryInfoKey
337 valueBuf =
new TCHAR[maxValueLen + 1];
338 dataBuf =
new BYTE[maxDataLen + 1];
341 for( i = 0, retCode = ERROR_SUCCESS; i < numValues; i++ )
343 valueLen = maxValueLen + 1;
344 dataLen = maxDataLen + 1;
346 retCode = RegEnumValue
357 if( retCode == ERROR_SUCCESS )
359 if( dataType == REG_SZ )
360 aList.push_back( CString( (TCHAR*)dataBuf ) );
383 std::list<CString> &aList
388 for(
int portNum = 1; portNum <= 256; portNum++ )
390 CString portDeviceFile;
393 portName.Format( _T(
"COM%u"), portNum );
394 portDeviceFile.Format( _T(
"\\\\.\\%s"), portName );
399 GENERIC_READ | GENERIC_WRITE,
406 if( hPort != INVALID_HANDLE_VALUE )
408 aList.push_back( portName );
409 ::CloseHandle( hPort );
411 #if defined( _DEBUG ) 414 TRACE(
"Failed to open port %s, error was %d\n", portName.GetBuffer(), GetLastError() );
457 const CString& aOperation
461 DWORD errorCode = GetLastError();
463 ASSERT( errorCode != ERROR_SUCCESS );
467 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
470 MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
475 mLastErrorText.Format( _T(
"%s: %s"), aOperation, CString( pszMessage ) );
476 LocalFree( pszMessage );
512 dcb.DCBlength =
sizeof( DCB );
521 dcb.BaudRate = aBaudRate;
523 dcb.StopBits = ONESTOPBIT;
524 dcb.Parity = NOPARITY;
526 dcb.fAbortOnError =
FALSE;
527 dcb.fOutxCtsFlow =
FALSE;
528 dcb.fOutxDsrFlow =
FALSE;
529 dcb.fDtrControl = DTR_CONTROL_DISABLE;
530 dcb.fRtsControl = RTS_CONTROL_DISABLE;
531 dcb.fDsrSensitivity =
FALSE;
532 dcb.fTXContinueOnXoff =
FALSE;
virtual bool setBaudRate(uint32 aBaudRate)
Set the baud rate.
void recordErrorText(const CString &aOperation)
Store a textual description of the last error that occurred.
CString mPortName
Display name of the serial port being used for communication.
virtual void close()
Close the COM port if one is in use.
static void clearLog()
Empties the packet log.
static bool initSerialPort(const CString &aPortName)
Initializes the port passed in.
static void getPortListEnum(std::list< CString > &aList)
Find the COM ports on the system by opening each in turn.
virtual uint32 getBaudRate() const
Return the last successfully applied baud rate .
static SerialPort * sInstance
The one and only instance of this object.
static GarminLinkLayer * getInstance()
Get the one and only link layer object.
HANDLE mComPortHandle
File handle for the com port that is open, or INVALID_HANDLE_VALUE if the port is not open...
static bool getPortListFromRegistry(std::list< CString > &aList)
Get the list of com ports on the system by enumerating the device map in the Windows registry...
virtual const CString & getPortName() const
Get the name of the serial port that is open.
SerialPort()
Construct the SerialPort.
bool init(const CString &aPortName)
Initializes the port passed in.
virtual bool tx(uint8 *aData, uint16 aSize)
Transmit bytes on the serial port.
#define RX_QUEUE_SIZE
The size of Windows' RX queue for the com port, in bytes.
virtual ~SerialPort()
Destructor. Close the serial port.
void resetPhysicalLayer(PhysicalLayer *aPort)
Reset the physical layer.
#define minval(_x, _y)
The smaller of _x and _y.
const CString & getLastError() const
Return a description of the last error that occurred.
LinkLayer * mLinkLayer
The link layer that is one level up from this serial port.
static SerialPort * getInstance()
Get the one and only serial port object.
#define DEFAULT_BAUD_RATE
The default baud rate for the serial port.
static void getPortList(std::list< CString > &aList)
Get the list of serial ports.
unsigned short int uint16
16-bit unsigned integer
virtual void rx(uint8 const *const aData, uint32 const aSize)=0
Receive data from the physical layer.
unsigned char uint8
8-bit unsigned integer
void setPhysicalLayer(PhysicalLayer *aPort)
Set the physical layer.
CString mLastErrorText
String containing the last communication error.
virtual bool isOpen() const
Indicate whether the port is open.
static bool isLogOpen()
Returns true if the log file is open.
unsigned long int uint32
32-bit unsigned integer
virtual void pumpRx()
Receive and process any data.
void onTimer()
Timer callback.
Physical layer implementation for a serial port.
#define TX_QUEUE_SIZE
The size of Windows' TX queue for the com port, in bytes.
uint32 mBaudRate
The last successfully applied baud rate.
static void destroyInstance()
Destroy the one and only serial port object.