• Skip to main content
  • Skip to header right navigation
  • Skip to site footer
CDP Studio logo

CDP Studio

The no-code and full-code software development tool for distributed control systems and HMI

  • Doc
  • Why CDP
    • Software developers
    • Automation engineers
    • Managers
  • Products
    • Automation Designer
    • HMI Designer
    • Maritime HMIs
  • Services
  • Use cases
  • Pricing
  • Try CDP

CDP Studio Documentation

  • Framework - CDP Core
  • Serial
  • 5.0.0

Serial Class

This is the serial communication class. More...

Header: #include <Serial>
Inherited By:

SerialLinux and SerialNT

  • List of all members, including inherited members

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":

ParityModesDescription
eNoParityNo parity is used for error-detection
eOddParityIf 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
eEvenParityIf 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.
eMarkParityThe parity bit is always set to 1
eSpaceParityThe 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.

ProtocolModesDescription
e_ProtNoneNo protocol is used.
e_ProtXonXoffSoftware protocol is used to control transmit on (XON) and transmit off (XOFF)
e_ProtRtsCtsHardware based flow control utilizing RTS and CTS pins.
e_ProtDtrDsrHardware 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 ValueDescription
0Multidrop is not used (RS/232)
1Half-duplex mode. Protocol should be set to 'None'
2Full-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.

  • If MultiDrop == 1: ClearDTR
  • If MultiDrop == 2: SetDTR

[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 nBaudRateThe baud rate can be any standard rate. The rate is limited by the type of serial I/O controller used in the hardware.
Parameter eParityThe parity can be any of the enum values.
Parameter nDataBitsThe number of data bits should be between or at 5 and 8.
Parameter nStopBitsThe mumber of stop bits should be 1 or 2.
Parameter eProtocolProtocol can be any of None,XonXoff,RtsCts or DtrDsr.
Parameter nMultiDropIf 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.
  • nMultiDrop == 1 -> HalfDuplex
  • nMultiDrop == 2 -> FullDuplex
Parameter strPortNameThe 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).

The content of this document is confidential information not to be published without the consent of CDP Technologies AS.

CDP Technologies AS, www.cdpstudio.com

Get started with CDP Studio today

Let us help you take your great ideas and turn them into the products your customer will love.

Try CDP Studio for free
Why CDP Studio?

CDP Technologies AS
Hundsværgata 8,
P.O. Box 144
6001 Ålesund, Norway

Tel: +47 990 80 900
E-mail: info@cdptech.com

Company

About CDP

Contact us

Services

Partners

Blog

Developers

Get started

User manuals

Support

Document download

Release notes

My account

Follow CDP

  • LinkedIn
  • YouTube
  • GitHub

© Copyright 2025 CDP Technologies. Privacy and cookie policy.

Return to top