C++ cross-platform RS232 serial communication library

Introduction

Serialib is a cross-platform library written in C++.

The library has been tested on Windows and Linux. This project has been developed with Qt Creator and succesfully compile with:

Functions

Opening and closing

Write operations

Read operations

Special read/write

IO write

IO read

Examples

ASCII table

This first example send a section of the ASCII table on the serial device:

// Connection to serial port
serialib serial;
if (serial.openDevice("/dev/ttyACM0", 115200)!=1) return 1;

// Display ASCII characters (from 32 to 128)
for (int c=32;c<128;c++)
{
    serial.writeChar(c);
    usleep(10000);
}

// Close the serial device
serial.closeDevice();

This example has been tested with the Arduino TFT terminal:

ASCII table received on the TFT touch screen

IO read/write

This second example set DTR and unset RTS before displaying input pins status:

// Connection to serial port
serialib serial;
if (serial.openDevice("COM1", 115200)!=1) return 1;

// Set DTR and unset RTS
serial.DTR(true);
serial.RTS(false);

// Loop forever and display pin status
while (1)
{
    // Read and display the status of each pin (DTR should be 1, RTS should be 0)
    printf ("4-DTR=%d\t", serial.isDTR());
    printf ("7-RTS=%d\t", serial.isRTS());

    printf ("1-DCD=%d\t", serial.isDCD());
    printf ("8-CTS=%d\t", serial.isCTS());
    printf ("6-DSR=%d\t", serial.isDSR());
    printf ("9-RING=%d\n", serial.isRI());
}

Download

Serialib can be downloaded with one of the following links:

See also


Last update : 04/04/2023