Name Last Update
.idea Loading commit data...
.vscode Loading commit data...
docs Loading commit data...
include/CppLinuxSerial Loading commit data...
src Loading commit data...
test Loading commit data...
tools Loading commit data...
.cproject Loading commit data...
.gitignore Loading commit data...
.project Loading commit data...
.travis.yml Loading commit data...
CHANGELOG.md Loading commit data...
CMakeLists.txt Loading commit data...
CMakeLists.txt.in Loading commit data...
LICENSE.txt Loading commit data...
README.md Loading commit data...

README.md

CppLinuxSerial

Serial port library written in C++

Build Status

Description

Library for communicating with COM ports on a Linux system.

Uses fstream to the file I/O.

Installation

Linux, MacOS, Windows

  1. Make sure you have cmake installed.

  2. Clone the git repo onto your local storage.

  3. Change into root repo directory:

    $ cd CppLinuxSerial
    
  4. Create a new build directory and change into it:

    $ mkdir build
    $ cd build
    
  5. Run cmake on the parent directory to generate makefile:

    $ cmake ..
    
  6. Run make on the generated makefile to generate the static library libCppLinuxSerial.a and an unit test executable:

    $ make
    
  7. To install the headers on your system:

    $ sudo make install
    
  8. To run the unit tests (NOTE: because this uses virtual serial ports via stty, this only works on Linux!):

    $ make run_unit_tests
    

    If you get errors such as Could not open device /dev/ttyS10. Is the device name correct and do you have read/write permission?" thrown in the test fixture's constructor., it is probably an issue with either creating the virtual serial ports or permissions to access them.

Examples

#include <CppLinuxSerial/SerialPort.hpp>

using namespace mn::CppLinuxSerial;

int main() {
    // Create serial port object and open serial port
    SerialPort serialPort("/dev/ttyUSB0", BaudRate::B_57600);
    serialPort.SetTimeout(-1); // Block when reading until any data is received
    serialPort.Open();

    // Write some ASCII datae
    serialPort.Write("Hello");

    // Read some data back
    std::string readData;
    serialPort.Read(readData);

    // Close the serial port
    serialPort.Close();
}

If the above code was in a file called main.cpp and you had installed CppLinuxSerial following the instructions above, on a Linux system you should be able to compile the example application with:

g++ main.cpp -lCppLinuxSerial

For more examples, see the .cpp files in test/unit/.

Dependencies

The following table lists all of the libraries dependencies.

Dependency Comments
C++14 C++14 used for strongly typed enums, `std::chrono` and literals.
stdio.h snprintf()
stty Used in unit tests to verify the serial port is configured correctly.

Issues

See GitHub Issues.

FAQ

  1. My code stalls when calling functions like SerialPort::Read(). This is probably because the library is set up to do a blocking read, and not enough characters have been received to allow SerialPort::Read() to return. Use SerialPort::SetNumCharsToWait() to determine how many characters to wait for before returning (set to 0 for non-blocking mode).

Changelog

See CHANGELOG.md.