Garmin Fleet Management Controller  2.19.0
SerialPort Class Reference

Physical layer implementation for a serial port. More...

#include <SerialPort.h>

+ Inheritance diagram for SerialPort:

Public Member Functions

virtual ~SerialPort ()
 Destructor. Close the serial port. More...
 
virtual uint32 getBaudRate () const
 Return the last successfully applied baud rate . More...
 
const CString & getLastError () const
 Return a description of the last error that occurred. More...
 
virtual const CString & getPortName () const
 Get the name of the serial port that is open. More...
 
bool init (const CString &aPortName)
 Initializes the port passed in. More...
 
virtual bool isOpen () const
 Indicate whether the port is open. More...
 
void onTimer ()
 Timer callback. More...
 
virtual bool setBaudRate (uint32 aBaudRate)
 Set the baud rate. More...
 
virtual void setLinkLayer (LinkLayer *aLinkLayer)
 Change the link layer that receives bytes from this SerialPort. More...
 
virtual bool tx (uint8 *aData, uint16 aSize)
 Transmit bytes on the serial port. More...
 

Static Public Member Functions

static void destroyInstance ()
 Destroy the one and only serial port object. More...
 
static SerialPortgetInstance ()
 Get the one and only serial port object. More...
 
static void getPortList (std::list< CString > &aList)
 Get the list of serial ports. More...
 
static bool initSerialPort (const CString &aPortName)
 Initializes the port passed in. More...
 

Protected Member Functions

 SerialPort ()
 Construct the SerialPort. More...
 
virtual void close ()
 Close the COM port if one is in use. More...
 
virtual void pumpRx ()
 Receive and process any data. More...
 
void recordErrorText (const CString &aOperation)
 Store a textual description of the last error that occurred. More...
 

Static Protected Member Functions

static void getPortListEnum (std::list< CString > &aList)
 Find the COM ports on the system by opening each in turn. More...
 
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. More...
 

Protected Attributes

LinkLayermLinkLayer
 The link layer that is one level up from this serial port. More...
 

Static Protected Attributes

static SerialPortsInstance = NULL
 The one and only instance of this object. More...
 

Private Attributes

uint32 mBaudRate
 The last successfully applied baud rate. More...
 
HANDLE mComPortHandle
 File handle for the com port that is open, or INVALID_HANDLE_VALUE if the port is not open. More...
 
BOOL mHandleInitialized
 If TRUE, mComPortHandle is initialized. More...
 
CString mLastErrorText
 String containing the last communication error. More...
 
CString mPortName
 Display name of the serial port being used for communication. More...
 

Friends

class TcpIpPort
 

Detailed Description

Physical layer implementation for a serial port.

Definition at line 30 of file SerialPort.h.

Constructor & Destructor Documentation

SerialPort::~SerialPort ( )
virtual

Destructor. Close the serial port.

Definition at line 442 of file SerialPort.cpp.

References close(), PhysicalLayer::mLinkLayer, recordErrorText(), and LinkLayer::setPhysicalLayer().

SerialPort::SerialPort ( )
protected

Construct the SerialPort.

Note
The SerialPort is constructed as a closed port; init() must be called to open the port and begin communicating.

Definition at line 82 of file SerialPort.cpp.

References initSerialPort(), mComPortHandle, and PhysicalLayer::mLinkLayer.

Referenced by getInstance(), and initSerialPort().

Member Function Documentation

void SerialPort::close ( )
protectedvirtual

Close the COM port if one is in use.

Close the handle for the com port. After this is called, init() must be called to resume communication.

Reimplemented in TcpIpPort.

Definition at line 191 of file SerialPort.cpp.

References isOpen(), mComPortHandle, and mPortName.

Referenced by getPortListEnum(), init(), initSerialPort(), TcpIpPort::initTcpPort(), setBaudRate(), and ~SerialPort().

void SerialPort::destroyInstance ( )
static

Destroy the one and only serial port object.

Definition at line 50 of file SerialPort.cpp.

References sInstance.

Referenced by getInstance().

uint32 SerialPort::getBaudRate ( ) const
virtual

Return the last successfully applied baud rate .

Reimplemented in TcpIpPort.

Definition at line 552 of file SerialPort.cpp.

References mBaudRate.

SerialPort * SerialPort::getInstance ( )
static

Get the one and only serial port object.

Returns
The serial port
Note
SerialPort is a singleton because this application is designed to communicate with a single client device. In a server-based application, there must be one serial port per connected client.

Definition at line 36 of file SerialPort.cpp.

References destroyInstance(), SerialPort(), and sInstance.

Referenced by GarminLinkLayer::GarminLinkLayer(), getPortListEnum(), CSelectCommPortDlg::OnBnClickedOk(), CLogViewerDlg::OnLbnSelchangeLog(), FmiApplicationLayer::rx(), and CFmiPcAppDlg::tryNextCommRate().

const CString & SerialPort::getLastError ( ) const

Return a description of the last error that occurred.

Definition at line 423 of file SerialPort.cpp.

References mLastErrorText.

void SerialPort::getPortList ( std::list< CString > &  aList)
static

Get the list of serial ports.

Parameters
aListThe list to store the port names.

Definition at line 60 of file SerialPort.cpp.

References getPortListEnum(), getPortListFromRegistry(), and TCP_PORT_NAME.

Referenced by CSelectCommPortDlg::OnInitDialog().

void SerialPort::getPortListEnum ( std::list< CString > &  aList)
staticprotected

Find the COM ports on the system by opening each in turn.

Parameters
aListThe list to append port names to.

Definition at line 382 of file SerialPort.cpp.

References close(), and getInstance().

Referenced by getPortList(), and getPortListFromRegistry().

bool SerialPort::getPortListFromRegistry ( std::list< CString > &  aList)
staticprotected

Get the list of com ports on the system by enumerating the device map in the Windows registry.

Parameters
aListThe list of port names to append to
Returns
TRUE if successful, FALSE otherwise. Note that a return value of FALSE does not mean aList is untouched; aList will have all of the port names that were found.
Note
May require administrative privileges to open the registry

Definition at line 293 of file SerialPort.cpp.

References getPortListEnum().

Referenced by getPortList(), and tx().

const CString & SerialPort::getPortName ( ) const
virtual

Get the name of the serial port that is open.

Returns
The name of the serial port that is open, or an empty string if the port is closed.

Reimplemented in TcpIpPort.

Definition at line 484 of file SerialPort.cpp.

References mPortName.

bool SerialPort::init ( const CString &  port)

Initializes the port passed in.

Parameters
portNull-terminated string containing the friendly name of the port (e.g., "COM1")
Returns
TRUE if the port was successfully opened and initialized, FALSE otherwise

Definition at line 124 of file SerialPort.cpp.

References close(), DEFAULT_BAUD_RATE, mComPortHandle, mPortName, recordErrorText(), RX_QUEUE_SIZE, setBaudRate(), and TX_QUEUE_SIZE.

Referenced by initSerialPort().

bool SerialPort::initSerialPort ( const CString &  port)
static

Initializes the port passed in.

Parameters
portNull-terminated string containing the friendly name of the port (e.g., "COM1")
Returns
TRUE if the port was successfully opened and initialized, FALSE otherwise

Definition at line 96 of file SerialPort.cpp.

References Logger::clearLog(), close(), GarminLinkLayer::getInstance(), init(), Logger::isLogOpen(), GarminLinkLayer::resetPhysicalLayer(), SerialPort(), and sInstance.

Referenced by CSelectCommPortDlg::OnBnClickedOk(), and SerialPort().

bool SerialPort::isOpen ( ) const
virtual

Indicate whether the port is open.

Returns
true if the serial port is open, false otherwise.

Reimplemented in TcpIpPort.

Definition at line 493 of file SerialPort.cpp.

References mComPortHandle, and setBaudRate().

Referenced by close(), and onTimer().

void SerialPort::onTimer ( )
virtual

Timer callback.

If the serial port is open, receive (and process) anything that is ready to receive.

Implements TimerListener.

Definition at line 433 of file SerialPort.cpp.

References isOpen(), and pumpRx().

void SerialPort::pumpRx ( )
protectedvirtual

Receive and process any data.

Reimplemented in TcpIpPort.

Definition at line 204 of file SerialPort.cpp.

References mComPortHandle, minval, PhysicalLayer::mLinkLayer, LinkLayer::rx(), and tx().

Referenced by onTimer().

void SerialPort::recordErrorText ( const CString &  aOperation)
protected

Store a textual description of the last error that occurred.

Parameters
aOperationThe name of the operation that caused the error

Definition at line 456 of file SerialPort.cpp.

References mLastErrorText.

Referenced by init(), TcpIpPort::init(), TcpIpPort::initTcpPort(), TcpIpPort::pumpRx(), setBaudRate(), TcpIpPort::tx(), and ~SerialPort().

bool SerialPort::setBaudRate ( uint32  aBaudRate)
virtual

Set the baud rate.

Parameters
aBaudRateThe new baud rate for the port, in bps. \

Reimplemented in TcpIpPort.

Definition at line 504 of file SerialPort.cpp.

References close(), FALSE, mBaudRate, mComPortHandle, recordErrorText(), and TRUE.

Referenced by init(), isOpen(), FmiApplicationLayer::rx(), and CFmiPcAppDlg::tryNextCommRate().

void PhysicalLayer::setLinkLayer ( LinkLayer aLinkLayer)
virtualinherited

Change the link layer that receives bytes from this SerialPort.

Parameters
aLinkLayerThe LinkLayer object that receives bytes, or NULL if no LinkLayer should receive data from this port.

Definition at line 19 of file PhysicalLayer.cpp.

References PhysicalLayer::mLinkLayer.

Referenced by GarminLinkLayer::GarminLinkLayer(), GarminLinkLayer::resetPhysicalLayer(), and LinkLayer::~LinkLayer().

bool SerialPort::tx ( uint8 aData,
uint16  aSize 
)
virtual

Transmit bytes on the serial port.

Parameters
aDataThe bytes to transmit
aSizeThe number of bytes to transmit
Returns
true if the bytes were transmitted, false otherwise

Implements PhysicalLayer.

Reimplemented in TcpIpPort.

Definition at line 258 of file SerialPort.cpp.

References getPortListFromRegistry(), and mComPortHandle.

Referenced by pumpRx().

Friends And Related Function Documentation

friend class TcpIpPort
friend

Definition at line 97 of file SerialPort.h.

Member Data Documentation

uint32 SerialPort::mBaudRate
private

The last successfully applied baud rate.

Definition at line 116 of file SerialPort.h.

Referenced by getBaudRate(), and setBaudRate().

HANDLE SerialPort::mComPortHandle
private

File handle for the com port that is open, or INVALID_HANDLE_VALUE if the port is not open.

Definition at line 102 of file SerialPort.h.

Referenced by close(), init(), isOpen(), pumpRx(), SerialPort(), setBaudRate(), and tx().

BOOL SerialPort::mHandleInitialized
private

If TRUE, mComPortHandle is initialized.

Definition at line 105 of file SerialPort.h.

CString SerialPort::mLastErrorText
private

String containing the last communication error.

Note
This is currently only used for errors reported by SerialPort::init()

Definition at line 113 of file SerialPort.h.

Referenced by getLastError(), and recordErrorText().

LinkLayer* PhysicalLayer::mLinkLayer
protectedinherited

The link layer that is one level up from this serial port.

Definition at line 47 of file PhysicalLayer.h.

Referenced by TcpIpPort::pumpRx(), pumpRx(), SerialPort(), PhysicalLayer::setLinkLayer(), ~SerialPort(), and TcpIpPort::~TcpIpPort().

CString SerialPort::mPortName
private

Display name of the serial port being used for communication.

Definition at line 108 of file SerialPort.h.

Referenced by close(), getPortName(), and init().

SerialPort * SerialPort::sInstance = NULL
staticprotected

The one and only instance of this object.

Definition at line 95 of file SerialPort.h.

Referenced by destroyInstance(), getInstance(), initSerialPort(), and TcpIpPort::initTcpPort().


The documentation for this class was generated from the following files: