Public Member Functions | Protected Attributes

CxUtils::Serial Class Reference

Serial (RS232) communication interface. More...

#include <serial.h>

List of all members.

Public Member Functions

 Serial ()
 Default constructor.
virtual ~Serial ()
 Destructor.
int Connect (const std::string &port, const unsigned int baud=9600, const unsigned int bits=8, const unsigned int partiy=0, const unsigned int stop=1)
 Connects to a serial port. This will close any existing connection.
int Initialize (const std::string &port, const unsigned int baud=9600, const unsigned int bits=8, const unsigned int parity=0, const unsigned int stop=1)
int Disconnect ()
 Disconnects from the port.
int Shutdown ()
int Reconnect ()
 Closes the current connection and reconnects with the same settings.
int Send (const char *buff, const unsigned int size)
 Writes to the serial port.
int Send (const std::string &message)
 Writes to the serial port.
int Send (const Packet &p)
 Writes to the serial port.
int Recv (char *buff, unsigned int size, const unsigned int waitTimeMs=1000)
 Reads from the serial port.
int Recv (Packet &p, const unsigned int len, const unsigned int waitTimeMs=1000)
 Reads from the serial port.
void Reset (void)
 Resets the serial port: Flushes all queues and interrupt/terminate any pending i/o operations.
void Flush (void)
 Flushes all data in the read and write buffers.
void FlushReadBuffer (void)
 Flushes all data in the read buffer if the device driver supports the operation.
void FlushWriteBuffer (void)
 Flushes all data in the write buffer if the device driver supports the operation.
unsigned int ReadBytesAvailable (void)
 Reports number of bytes available in serial port read-queue.
unsigned int WriteBytesPending (void)
 Reports number of bytes pending in serial port write-queue.
int GetBaudRate () const
int GetStopBits () const
int GetParity () const
int GetDataBits () const
const char * GetPortName () const
bool IsConnected () const

Protected Attributes

int mCom
 Handle to port.
std::string mPortName
 Port name.
char mParity
 Parity.
int mBaudRate
 Current baudrate.
int mStopBits
 Stop bits.
int mDataBits
 Data bits.
bool mConnected
 Active connection?
Mutex mSerialMutex
 Mutex for thread protection of serial port.

Detailed Description

Serial (RS232) communication interface.

This interface uses a mutex so that it is thread safe for parallel reading and writing.

Definition at line 62 of file serial.h.


Constructor & Destructor Documentation

Serial::Serial (  )

Default constructor.

Definition at line 69 of file serial.cpp.

Serial::~Serial (  ) [virtual]

Destructor.

Disconnects from any active connection.

Definition at line 92 of file serial.cpp.


Member Function Documentation

int Serial::Connect ( const std::string &  port,
const unsigned int  baud = 9600,
const unsigned int  bits = 8,
const unsigned int  parity = 0,
const unsigned int  stop = 1 
)

Connects to a serial port. This will close any existing connection.

Parameters:
portThe port to connect to. In Windows this would be "com1", "com2", etc. In Linux it is the path to the port.
baudThe baud rate to connect at
bitsThe number of data bits (default = 8). [5,8].
parityThe type of parity (CX_SERIAL_NO_PARITY, CX_SERIAL_EVEN_PARITY, CX_SERIAL_ODD_PARITY)
stopThe number of stop bits (0, 1, 2).
Returns:
1 on success, 0 on failure

Definition at line 115 of file serial.cpp.

int Serial::Disconnect (  )

Disconnects from the port.

Returns:
1 on success, 0 on failure.

Definition at line 387 of file serial.cpp.

void Serial::Flush ( void   )

Flushes all data in the read and write buffers.

Definition at line 752 of file serial.cpp.

void Serial::FlushReadBuffer ( void   )

Flushes all data in the read buffer if the device driver supports the operation.

Definition at line 778 of file serial.cpp.

void Serial::FlushWriteBuffer ( void   )

Flushes all data in the write buffer if the device driver supports the operation.

Definition at line 800 of file serial.cpp.

int CxUtils::Serial::GetBaudRate (  ) const [inline]

Definition at line 97 of file serial.h.

int CxUtils::Serial::GetDataBits (  ) const [inline]

Definition at line 100 of file serial.h.

int CxUtils::Serial::GetParity (  ) const [inline]

Definition at line 99 of file serial.h.

const char* CxUtils::Serial::GetPortName (  ) const [inline]

Definition at line 101 of file serial.h.

int CxUtils::Serial::GetStopBits (  ) const [inline]

Definition at line 98 of file serial.h.

int CxUtils::Serial::Initialize ( const std::string &  port,
const unsigned int  baud = 9600,
const unsigned int  bits = 8,
const unsigned int  parity = 0,
const unsigned int  stop = 1 
) [inline]

Definition at line 72 of file serial.h.

bool CxUtils::Serial::IsConnected (  ) const [inline]

Definition at line 102 of file serial.h.

unsigned int Serial::ReadBytesAvailable ( void   )

Reports number of bytes available in serial port read-queue.

Returns:
Number of bytes available to be read.

Definition at line 843 of file serial.cpp.

int Serial::Reconnect (  )

Closes the current connection and reconnects with the same settings.

Returns:
1 on success, 0 on failure.

Definition at line 417 of file serial.cpp.

int Serial::Recv ( Packet p,
const unsigned int  len,
const unsigned int  waitTimeMs = 1000 
)

Reads from the serial port.

Function returns the number of bytes actually read.

Parameters:
pPacket to receive data into.
lenThe number of bytes to try receive.
waitTimeMsMaximum time to wait for data in ms (default is 1000).
Returns:
Number of bytes received.

Definition at line 706 of file serial.cpp.

int Serial::Recv ( char *  buff,
unsigned int  size,
const unsigned int  waitTimeMs = 1000 
)

Reads from the serial port.

Function returns the number of bytes actually read.

Parameters:
buffBuffer for storing read data.
sizeThe size of the buffer for storing read data.
waitTimeMsMaximum time to wait for data in ms (default is 1000).
Returns:
Number of bytes received.

Definition at line 571 of file serial.cpp.

void Serial::Reset ( void   )

Resets the serial port: Flushes all queues and interrupt/terminate any pending i/o operations.

Definition at line 821 of file serial.cpp.

int Serial::Send ( const char *  buff,
const unsigned int  size 
)

Writes to the serial port.

Parameters:
buffBuffer containing data to write.
sizeNumber of bytes/size of buffer to write.
Returns:
Number of bytes sent.

Definition at line 433 of file serial.cpp.

int Serial::Send ( const std::string &  message )

Writes to the serial port.

Parameters:
messageCharacter string to send.
Returns:
Number of bytes sent.

Definition at line 551 of file serial.cpp.

int Serial::Send ( const Packet p )

Writes to the serial port.

Parameters:
pPacket data to send.
Returns:
Number of bytes sent.

Definition at line 535 of file serial.cpp.

int CxUtils::Serial::Shutdown (  ) [inline]

Definition at line 81 of file serial.h.

unsigned int Serial::WriteBytesPending ( void   )

Reports number of bytes pending in serial port write-queue.

Returns:
Number of bytes remaining to be written. (0 IN LINUX).

Definition at line 866 of file serial.cpp.


Member Data Documentation

int CxUtils::Serial::mBaudRate [protected]

Current baudrate.

Definition at line 113 of file serial.h.

int CxUtils::Serial::mCom [protected]

Handle to port.

Definition at line 109 of file serial.h.

bool CxUtils::Serial::mConnected [protected]

Active connection?

Definition at line 116 of file serial.h.

int CxUtils::Serial::mDataBits [protected]

Data bits.

Definition at line 115 of file serial.h.

char CxUtils::Serial::mParity [protected]

Parity.

Definition at line 112 of file serial.h.

std::string CxUtils::Serial::mPortName [protected]

Port name.

Definition at line 111 of file serial.h.

Mutex for thread protection of serial port.

Definition at line 117 of file serial.h.

int CxUtils::Serial::mStopBits [protected]

Stop bits.

Definition at line 114 of file serial.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines