Serial Class
This is the serial communication class. More...
Header: | #include <OSAPI/Serial/Serial.h> |
Inherited By: | SerialLinux and SerialNT |
Public Functions
virtual | ~Serial() |
virtual void | ClearDTR() = 0 |
virtual void | ClearRTS() = 0 |
virtual void | Close() = 0 |
virtual void | Configure(XMLElementEx *nodeConfig) |
unsigned int | GetBaudRate() |
virtual bool | GetByte(unsigned char *pByte, unsigned int lTimeout) = 0 |
virtual int | GetBytes(unsigned char *pBuf, int nBytes, unsigned int lTimeout) = 0 |
virtual double | GetClockFrequencyMHz() |
unsigned char | GetDataBits() |
virtual unsigned int | GetInfinite() = 0 |
char | GetParity() |
unsigned char | GetStopBits() |
virtual unsigned int | GetZeroTimeout() = 0 |
virtual bool | Open() = 0 |
virtual bool | PutByte(const unsigned char *pByte, unsigned int lTimeout) = 0 |
virtual int | PutBytes(const unsigned char *pBuf, int nBytes, unsigned int lTimeout) = 0 |
void | SetBaud(unsigned int nBaudRate) |
virtual void | SetClockFrequencyMHz(double dFrequencyMHz) |
virtual void | SetDTR() = 0 |
virtual void | SetRTS() = 0 |
virtual void | SetupParams(unsigned int nBaudRate, ParityModes eParity, int nDataBits, int nStopBits, ProtocolModes eProtocol, int nMultiDrop, std::string strPortName) = 0 |
Static Public Members
Serial * | MakeSerial() |
Protected Functions
Serial() | |
virtual void | MultiDrop_Control_Duplex(void) |
virtual void | MultiDrop_Receive_Enable(void) |
virtual void | MultiDrop_Transmit_Enable(void) |
const Serial & | operator=(const Serial &right) |
Detailed Description
This is the serial communication class.
This class gives access to a named RS/232, RS/422, RS/485 or compatible COM port, for instance 'COM1' on windows, or '/dev/ttyS0' on Linux. It is possible to set up baud rate, parity, number of databits, number of stopbits, handshaking, multidrop and name of COM port to control.
Baudrate
The Baudrate determines the speed of the data transmission. Higher baudrates generally have lower tolerance against noise than lower baud rates.
The following baud rates are supported by this class, but note that some of them may be unsupported by hardware.
Standard Baud rates |
---|
50 |
75 |
110 |
134 |
150 |
300 |
600 |
1200 |
1800 |
2400 |
4800 |
9600 |
19200 |
38400 |
57600 |
115200 |
230400 |
Parity
In serial communication, a parity bit may be used as an error-detection mechanism. Supported parities are "None", "Even", "Odd", "Mark" or "Space":
ParityModes | Description |
---|---|
eNoParity | No parity is used for error-detection |
eOddParity | If the count of bits with a value of 1 is even, the parity bit is set to 1, making the value an odd number. If the count of bits with a value of 1 is odd, then the number value is already odd, so parity is set to 0 |
eEvenParity | If the count of bits with a value of 1 is even, the parity bit is set to 0, making the value an even number. If the count of bits with a value of 1 is odd, the parity bit is set to 1, making the value an odd number. |
eMarkParity | The parity bit is always set to 1 |
eSpaceParity | The parity bit is always set to 0 |
Stop bits
Character Stop bits can be either 1 or 2 bits per character.
Note: The combination of 8 databits and 2 stopbits is illegal. The typical Stop bit value is 1.
Data bits
This is the number of data bits per character. Can be 5,6,7 or 8. The typical combination is 8 databits and 1 stop bit.
Note: The combination of 8 databits and 2 stopbits is illegal. Typical Data bit values are 7 or 8.
Protocol
The protocol to use: None, XonXoff, RtsCts or DtrDsr.
ProtocolModes | Description |
---|---|
e_ProtNone | No protocol is used. |
e_ProtXonXoff | Software protocol is used to control transmit on (XON) and transmit off (XOFF) |
e_ProtRtsCts | Hardware based flow control utilizing RTS and CTS pins. |
e_ProtDtrDsr | Hardware based flow control utilizing DTR and DSR pins. |
MultiDrop
This is used to enable RS/422 or RS/485 communication and sets whether the communication should be half-duplex (uni-directional) or full-duplex (bi-directional). MultiDrop will only function as intended when Protocol is set to None. When communicationg using RS/232, MultiDrop should always be set to 'None'. On a 2-wire setup, use 'HalfDuplex', and on a 4-wire setup use either 'FullDuplex' or 'HalfDuplex'.
Note: MultiDrop may not be supported by the hardware and/or serial driver.
The below table shows the MultiDrop values used in SetupParams() function.
MultiDrop Value | Description |
---|---|
0 | Multidrop is not used (RS/232) |
1 | Half-duplex mode. Protocol should be set to 'None' |
2 | Full-duplex mode. Protocol should be set to 'None' |
Usages
Example using 'hardcoded' values:
#include <OSAPI/Serial/Serial.h> .... Serial* serial = Serial::MakeSerial(); serial->SetupParams(9600,e_NoParity,8,1,e_ProtNone,0,"/dev/ttyS0"); serial->Open(); if(serial->PutBytes("test",4,1000)==4) CDPMessage("PutBytes succeeded!"); else CDPMessage("PutBytes failed!"); serial->Close(); delete serial;
Example using configured values:
#include <OSAPI/Serial/Serial.h> ... Serial* serial = Serial::MakeSerial(); ... serial->Configure(parentConfigurationElement); serial->Open(); if(serial->PutBytes("test",4,1000)==4) CDPMessage("PutBytes succeeded!"); else CDPMessage("PutBytes failed!"); serial->Close(); delete serial;
See also Configure().
Member Function Documentation
[protected]
Serial::Serial()
Constructor. Protected, code should use MakeSerial() to get an instance of the correct serial class.
[virtual]
Serial::~Serial()
Destroys the instance of Serial. The destructor is virtual.
[pure virtual]
void Serial::ClearDTR()
Clear the DTR pin.
[pure virtual]
void Serial::ClearRTS()
Clear the RTS pin.
[pure virtual]
void Serial::Close()
Closes the port.
[virtual]
void Serial::Configure(XMLElementEx *nodeConfig)
Configures itself from nodeConfig. Assumes nodeConfig is an XMLElementEx that contains the following child elements.
<BaudRate></BaudRate> <Parity></Parity> <StopBits></StopBits> <Protocol></Protocol> <MultiDrop></MultiDrop> <ComPort Name=""></ComPort>
See also Baudrate, Parity, Data bits, Stop bits, Protocol, and MultiDrop.
unsigned int Serial::GetBaudRate()
Return the baudrate.
[pure virtual]
bool Serial::GetByte(unsigned char *pByte, unsigned int lTimeout)
Reads a single character into the caller-supplied pByte.
Returns after the character has been received, or after a timeout (whichever occurs first). lTimeout value is in milliseconds.
As GetByte() is a blocking call, do not call it from a Process() function. See CDPComponent and OSAPIThread for more information about component threads.
[pure virtual]
int Serial::GetBytes(unsigned char *pBuf, int nBytes, unsigned int lTimeout)
Reads a string of length nBytes into the caller-supplied buffer pBuf.
Returns after all the characters have been received, or after a timeout (whichever occurs first). lTimeout value is in milliseconds.
If a timeout occurs the function returns 0.
As GetBytes() is a blocking call, do not call it from a Process() function. See CDPComponent and OSAPIThread for more information about component threads.
[virtual]
double Serial::GetClockFrequencyMHz()
Return the ClockFrequency in MHz
unsigned char Serial::GetDataBits()
Return Databits.
[pure virtual]
unsigned int Serial::GetInfinite()
Returns the timeout value for "infinite".
char Serial::GetParity()
Returns the Parity as a char: eEvenParity: 'E' eOddParity: 'O' eMarkParity: 'M' eSpaceParity: 'S' everything else: 'N'
unsigned char Serial::GetStopBits()
Return Stopbits.
[pure virtual]
unsigned int Serial::GetZeroTimeout()
Returns the smallest possible timeout value.
[static]
Serial *Serial::MakeSerial()
Creates a serial object for the current target platform.
[virtual protected]
void Serial::MultiDrop_Control_Duplex(void)
MultiDrop_Control_Duplex.
[virtual protected]
void Serial::MultiDrop_Receive_Enable(void)
Call and SetRTS() if multidrop.
[virtual protected]
void Serial::MultiDrop_Transmit_Enable(void)
Call ClearRTS() if multidrop.
[pure virtual]
bool Serial::Open()
Opens the serial port.
See also SetupParams() and Configure().
[pure virtual]
bool Serial::PutByte(const unsigned char *pByte, unsigned int lTimeout)
Sends a single character.
Returns after the character has been sent, or after a timeout (whichever occurs first). lTimeout value is in milliseconds.
As PutByte() is a blocking call, do not call it from a Process() function. See CDPComponent and OSAPIThread for more information about component threads.
[pure virtual]
int Serial::PutBytes(const unsigned char *pBuf, int nBytes, unsigned int lTimeout)
Sends a string from the caller-supplied buffer pBuf that is nBytes in size.
Returns after *all* the characters have been sent, or after a timeout (whichever occurs first). lTimeout value is in milliseconds.
As PutBytes() is a blocking call, do not call it from a Process() function. See CDPComponent and OSAPIThread for more information about component threads.
void Serial::SetBaud(unsigned int nBaudRate)
Change the baudrate (must be set before Open()).
[virtual]
void Serial::SetClockFrequencyMHz(double dFrequencyMHz)
Set the clockfrequency in MHz. Should be called before Open().
Note that if the serial host uses a non-standard ClockFrequencyMHz, it must be specified in the configuration, or set by calling this function prior to Open().
[pure virtual]
void Serial::SetDTR()
Set the DTR pin.
[pure virtual]
void Serial::SetRTS()
Set the RTS pin.
[pure virtual]
void Serial::SetupParams(unsigned int nBaudRate, ParityModes eParity, int nDataBits, int nStopBits, ProtocolModes eProtocol, int nMultiDrop, std::string strPortName)
Parameter setup.
Parameter nBaudRate | The baud rate can be any standard rate. The rate is limited by the type of serial I/O controller used in the hardware. |
Parameter eParity | The parity can be any of the enum values. |
Parameter nDataBits | The number of data bits should be between or at 5 and 8. |
Parameter nStopBits | The mumber of stop bits should be 1 or 2. |
Parameter eProtocol | Protocol can be any of None,XonXoff,RtsCts or DtrDsr. |
Parameter nMultiDrop | If nMultiDrop > 0, the DTR pin will be set, and CTS will be used for handshake. Note that you can not use RtsCts or DtrDsr for protocol if multidrop is true .
|
Parameter strPortName | The name of the port to open, typically 'COM1' etc on Windows, '/dev/ttyS0' on Linux. |
See also Baudrate, Parity, Data bits, Stop bits, Protocol, and MultiDrop.
[protected]
const Serial &Serial::operator=(const Serial &right)
Assignment operator. Not implemented (only declared to prevent use).
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.