Serial (RS232) communication interface. More...
#include <serial.h>
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. |
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.
Serial::Serial | ( | ) |
Default constructor.
Definition at line 69 of file serial.cpp.
Serial::~Serial | ( | ) | [virtual] |
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.
port | The port to connect to. In Windows this would be "com1", "com2", etc. In Linux it is the path to the port. |
baud | The baud rate to connect at |
bits | The number of data bits (default = 8). [5,8]. |
parity | The type of parity (CX_SERIAL_NO_PARITY, CX_SERIAL_EVEN_PARITY, CX_SERIAL_ODD_PARITY) |
stop | The number of stop bits (0, 1, 2). |
Definition at line 115 of file serial.cpp.
int Serial::Disconnect | ( | ) |
Disconnects from the port.
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.
const char* CxUtils::Serial::GetPortName | ( | ) | const [inline] |
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] |
unsigned int Serial::ReadBytesAvailable | ( | void | ) |
Reports number of bytes available in serial port read-queue.
Definition at line 843 of file serial.cpp.
int Serial::Reconnect | ( | ) |
Closes the current connection and reconnects with the same settings.
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.
p | Packet to receive data into. |
len | The number of bytes to try receive. |
waitTimeMs | Maximum time to wait for data in ms (default is 1000). |
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.
buff | Buffer for storing read data. |
size | The size of the buffer for storing read data. |
waitTimeMs | Maximum time to wait for data in ms (default is 1000). |
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.
buff | Buffer containing data to write. |
size | Number of bytes/size of buffer to write. |
Definition at line 433 of file serial.cpp.
int Serial::Send | ( | const std::string & | message ) |
Writes to the serial port.
message | Character string to send. |
Definition at line 551 of file serial.cpp.
int Serial::Send | ( | const Packet & | p ) |
Writes to the serial port.
p | Packet data to send. |
Definition at line 535 of file serial.cpp.
unsigned int Serial::WriteBytesPending | ( | void | ) |
Reports number of bytes pending in serial port write-queue.
Definition at line 866 of file serial.cpp.
int CxUtils::Serial::mBaudRate [protected] |
int CxUtils::Serial::mCom [protected] |
bool CxUtils::Serial::mConnected [protected] |
int CxUtils::Serial::mDataBits [protected] |
char CxUtils::Serial::mParity [protected] |
std::string CxUtils::Serial::mPortName [protected] |
Mutex CxUtils::Serial::mSerialMutex [protected] |
int CxUtils::Serial::mStopBits [protected] |