Serial Port C++

SerialPort API related documentation. More...

Namespaces

 dpySerialPort
 Deepsy SERIAL PORT namespace that includes the different enums, structs or method signatures that should be used.
 

Classes

class  SerialPortAvailabilityObserver
 Service Availability observer class. More...
 
class  SerialPortManager
 Allows to interact with a SerialPort service. More...
 

Detailed Description

A quick overview of basic SerialPort API capabilities and examples is given.
If you find SerialPort API useful and would like to know more details, please check out further sections of the SerialPort API documentation or contact the Deepsy team.

Examples

  • Getting serial port list

    #include <dpy/serialportApi.h>
    #include <iostream>
    volatile bool waiting = true;
    void serialport_list_handler(boost::system::error_code error_code, std::map<std::string, dpySerialPort::SerialPortConf> serialPortMap)
    {
    if (error_code) {
    std::cout << "\rError : " << error_code.message() << std::endl;
    } else {
    if (serialPortMap.size() > 0) {
    for (auto s : serialPortMap) {
    std::cout << "Serial Port id : " << s.first << std::endl;
    std::cout << "\tAlias : " << s.second.alias << std::endl;
    if (s.second.protocol == dpySerialPort::SerialPortProtocol::RS485) {
    std::cout << "\tProtocol : RS485" << std::endl;
    } else {
    std::cout << "\tProtocol : RS232" << std::endl;
    }
    std::cout << "\tBaud Rate : " << s.second.baudRate << std::endl;
    std::cout << "\tData bits : " << s.second.dataBits << std::endl;
    std::cout << "\tStop Bits : " << s.second.stopBits << std::endl;
    if (s.second.parity == dpySerialPort::ParityType::NONE) {
    std::cout << "\tParity : NONE" << std::endl;
    } else if (s.second.parity == dpySerialPort::ParityType::ODD) {
    std::cout << "\tParity : ODD" << std::endl;
    } else if (s.second.parity == dpySerialPort::ParityType::EVEN) {
    std::cout << "\tParity : EVEN" << std::endl;
    } else if (s.second.parity == dpySerialPort::ParityType::MARK) {
    std::cout << "\tParity : MARK" << std::endl;
    } else {
    std::cout << "\tParity : SPACE" << std::endl;
    }
    if (s.second.flowControl == dpySerialPort::FlowControl::FLOW_NONE) {
    std::cout << "\tFlow Control : NONE" << std::endl;
    } else if (s.second.flowControl == dpySerialPort::FlowControl::XONXOFF) {
    std::cout << "\tFlow Control : XONXOFF" << std::endl;
    } else if (s.second.flowControl == dpySerialPort::FlowControl::RTSCTS) {
    std::cout << "\tFlow Control : RTSCTS" << std::endl;
    } else {
    std::cout << "\tFlow Control : DSRDTR" << std::endl;
    }
    std::cout << "\tAddress : " << s.second.address << std::endl;
    std::cout << "\tRS485 pinout : " << s.second.RS485pinout << std::endl;
    std::cout << "\tRS485 txen : " << s.second.RS485txen << std::endl;
    std::cout << "\tAllowed Protocols : " << std::endl;
    for (auto allowed : s.second.allowedProtocols) {
    std::cout << "\t\tRS485" << std::endl;
    } else {
    std::cout << "\t\tRS232" << std::endl;
    }
    }
    }
    } else {
    std::cout << "There are no serial ports. " << std::endl;
    }
    }
    waiting = false;
    }
    int main()
    {
    SerialPortManager serialPort;
    waiting = true;
    serialPort.GetSerialPortList(serialport_list_handler);
    while (waiting == true) {
    }
    }

  • Setting configuration of serial port

    #include <dpy/serialportApi.h>
    #include <iostream>
    volatile bool waiting = true;
    inline dpySerialPort::ParityType parityStringToEnum(std::string parity)
    {
    if (parity == "NONE" || parity == "none") {
    } else if (parity == "ODD" || parity == "odd") {
    return dpySerialPort::ParityType::ODD;
    } else if (parity == "EVEN" || parity == "even") {
    return dpySerialPort::ParityType::EVEN;
    } else if (parity == "MARK" || parity == "mark") {
    return dpySerialPort::ParityType::MARK;
    } else {
    return dpySerialPort::ParityType::SPACE;
    }
    }
    inline dpySerialPort::SerialPortProtocol protocolStringToEnum(std::string protocol)
    {
    if (protocol == "RS485") {
    } else {
    }
    }
    inline dpySerialPort::FlowControl flowcontrolStringToEnum(std::string flowcontrol)
    {
    if (flowcontrol == "NONE") {
    return dpySerialPort::FlowControl::FLOW_NONE;
    } else if (flowcontrol == "XONXOFF") {
    } else if (flowcontrol == "RTSCTS") {
    } else {
    }
    }
    void resultHandler(boost::system::error_code ec)
    {
    if (ec) {
    std::cout << "\rError : " << ec.message() << std::endl;
    } else {
    std::cout << "\rAsynchronous operation performed successfully" << std::endl;
    }
    waiting = false;
    }
    int main()
    {
    SerialPortManager serialPort;
    const std::string &serialportid = "COM1";
    dpySerialPort::SerialPortConf serialPortConfiguration;
    serialPortConfiguration.alias = "COM1";
    serialPortConfiguration.protocol = protocolStringToEnum("RS232");
    serialPortConfiguration.baudRate = 9600;
    serialPortConfiguration.dataBits = 8;
    serialPortConfiguration.stopBits = 1;
    serialPortConfiguration.parity = parityStringToEnum("NONE");
    serialPortConfiguration.flowControl = flowcontrolStringToEnum("XONXOFF");
    serialPort.asyncSetSerialPortConfiguration(resultHandler, serialportid, serialPortConfiguration);
    while (waiting == true) {
    }
    }

  • Sending message through serial port

    #include <dpy/serialportApi.h>
    #include <iostream>
    volatile bool waiting = true;
    void resultHandler_sync(boost::system::error_code ec)
    {
    if (ec) {
    std::cout << "\rError : " << ec.message() << std::endl;
    } else {
    std::cout << "\rSynchronous operation performed successfully" << std::endl;
    }
    waiting = false;
    }
    int main()
    {
    SerialPortManager serialPort;
    const std::string &serialportid = "COM1";
    std::string data = "Hello\n";
    serialPort.syncSend(resultHandler_sync, serialportid, data);
    while (waiting == true) {
    }
    }

  • Subscribe to serial port events
    #include <dpy/serialportApi.h>
    #include <iostream>
    volatile bool waiting = true;
    void print_serialportevent(boost::system::error_code ec, const std::string &portid, const std::string &data)
    {
    if (ec.value() == 0) {
    std::cout << "\nNew message received to serial port: " << portid << std::endl;
    std::cout << "New message: " << data << std::endl;
    } else {
    std::cout << "\rError : " << ec.message() << std::endl;
    }
    waiting = false;
    }
    void resultHandler_sync(boost::system::error_code ec)
    {
    if (ec) {
    std::cout << "\rError : " << ec.message() << std::endl;
    } else {
    std::cout << "\rSynchronous operation performed successfully" << std::endl;
    }
    }
    int main()
    {
    SerialPortManager serialPort;
    serialPort.serialPortEvent_S(print_serialportevent);
    const std::string &serialportid = "COM1";
    std::string data = "Hello\n";
    serialPort.syncSend(resultHandler_sync, serialportid, data);
    while (waiting == true) {
    }
    }
void syncSend(dpySerialPort::handler_function handler, const std::string &portid, const std::string &data)
Send message through serial port.
Definition: serialportTypes.h:44
FlowControl flowControl
Flow Control.
Definition: serialportTypes.h:52
@ XONXOFF
Xon/Xoff.
Definition: serialportTypes.h:26
ParityType parity
Parity type.
Definition: serialportTypes.h:51
@ RTSCTS
Rts/Cts.
Definition: serialportTypes.h:27
SerialPortProtocol
Definition: serialportTypes.h:31
@ RS232
232
Definition: mcuTypes.h:74
int baudRate
Baud Rate.
Definition: serialportTypes.h:48
SerialPortProtocol protocol
Connection Type.
Definition: serialportTypes.h:47
int dataBits
Data bits.
Definition: serialportTypes.h:49
@ DSRDTR
Dsr/Dtr.
Definition: serialportTypes.h:28
int stopBits
Stop bits.
Definition: serialportTypes.h:50
@ NONE
No messages.
Definition: iPower.h:74
FlowControl
Definition: serialportTypes.h:24
@ RS485
485
Definition: mcuTypes.h:73
Allows to interact with a SerialPort service.
Definition: serialportApi.h:81
void asyncSetSerialPortConfiguration(dpySerialPort::handler_function handler, const std::string &portid, dpySerialPort::SerialPortConf serialPortConfiguration)
Modifies serial port configuration.