IoT::Devices

class SerialDevice

File Information

Library: IoT/Devices
Package: Devices
Header: IoT/Devices/SerialDevice.h

Description

This class provides a simple interface for accessing a serial port.

There are two modes of operation for receiving data: polling (blocking) mode (default) and events (callback) mode.

In polling mode, repeatedly call poll() to detect if data is available, then call one of the read() methods to receive available data. The read() methods can also be called if no data is available, but will block until at least one character can be read.

In event mode the class will start a thread that polls and reads available data from the serial port, and fires the lineReceived event whenever something has been read. Since callback mode uses readLine() to receive entire lines (if possible), delimiter characters and timeout can be set via the "delimiters" (string) and "timeout" (double) properties.

Clients of the class can switch between polling and event mode by enabling or disabling the "events" feature:

setFeature("events", true);

Inheritance

Direct Base Classes: Device

All Base Classes: Device

Member Summary

Member Functions: available, getRTS, poll, readByte, readLine, readString, reconfigure, setRTS, writeByte, writeString

Inherited Functions: getFeature, getPropertyBool, getPropertyDouble, getPropertyInt, getPropertyInt16, getPropertyInt64, getPropertyString, getPropertyTimestamp, hasFeature, hasProperty, setFeature, setPropertyBool, setPropertyDouble, setPropertyInt, setPropertyInt16, setPropertyInt64, setPropertyString, setPropertyTimestamp

Types Aliases

Ptr

using Ptr = Poco::AutoPtr < SerialDevice >;

Constructors

SerialDevice

SerialDevice();

Creates the SerialDevice.

Destructor

~SerialDevice virtual

~SerialDevice();

Destroys the SerialDevice.

Member Functions

available virtual

virtual int available() const = 0;

Returns the number of characters available from the internal buffer.

getRTS virtual

virtual bool getRTS() const = 0;

Returns the RTS status.

poll virtual

virtual bool poll(
    double timeout
) = 0;

Waits for data to arrive at the port.

Returns true immediately if data is already in the internal buffer, or if data arrives during the specified time interval (given in seconds), otherwise false.

readByte virtual

virtual Poco::UInt8 readByte() = 0;

Reads a single character from the port.

If the internal buffer is empty, blocks until data arrives at the port.

readLine virtual

virtual std::string readLine(
    const std::string & delimiters,
    double timeout
) = 0;

Reads a characters from the serial port.

Characters will be read until one of the characters specified in delimiters is encountered, or no character is received for the specified timeout (in seconds).

readString virtual

virtual std::string readString() = 0;

Reads what's currently available from the serial port or internal buffer and returns it in data.

If the internal buffer is empty, blocks until data arrives at the port.

reconfigure virtual

virtual void reconfigure(
    int baudRate,
    const std::string & parameters,
    const std::string & flowControl
) = 0;

Reconfigures the serial port with the given baud rate and communication parameters.

Baud rate must be one of the following: 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200 or 230400.

Specify -1 for baudRate to use the currently configured speed. Note that not all platforms support all baud rates.

To specify character size, parity and stop bits, use the parameters argument. The parameters argument must be a three character string. The first character specifies the serial line character width in bits (5, 6, 7 or 8). The second char specifies the parity (N for none, E for even, O for odd) and the third character specifies the number of stop bits (1 or 2). Example: "8N1" for 8 bit characters, no parity, 1 stop bit.

To specify flow control, use the flowControl argument. Specify "none" or an empty string for no flow control. Specify "rtscts" for hardware (RTS/CTS) flow control.

setRTS virtual

virtual void setRTS(
    bool status
) = 0;

Manually sets or clears RTS.

writeByte virtual

virtual void writeByte(
    Poco::UInt8 byte
) = 0;

Writes the given byte to the port.

writeString virtual

virtual void writeString(
    const std::string & data
) = 0;

Writes the given data to the port. Returns the number of characters written.

Variables

lineReceived

Poco::BasicEvent < const std::string > lineReceived;

Fired when a line of text has been received on the serial port.

Securely control IoT edge devices from anywhere   Connect a Device