Linux serial port library written in C++.
Library for communicating with COM ports on a Linux system.
- Simple API
- Supports custom baud rates
cmake
based build system
-
Make sure you have
cmake
installed. -
Clone the git repo onto your local storage.
-
Change into root repo directory:
$ cd CppLinuxSerial
-
Create a new build directory and change into it:
$ mkdir build $ cd build
-
Run cmake on the parent directory to generate makefile:
$ cmake ..
-
Run make on the generated makefile to generate the static library
libCppLinuxSerial.a
and an unit test executable:$ make
-
To install the headers on your system:
$ sudo make install
-
To run the unit tests:
$ make run_unit_tests
NOTE: The unit tests used to use virtual serial ports via
stty
on Linux to do more through testing. I ran into permission problems running stty on TravisCI after they did an update and had to remove tests (leaving almost no tests remaining). If anyone wants to add better unit tests, it is greatly welcomed!
#include <CppLinuxSerial/SerialPort.hpp>
using namespace mn::CppLinuxSerial;
int main() {
// Create serial port object and open serial port at 57600 buad, 8 data bits, no parity bit, one stop bit (8n1),
// and no flow control
SerialPort serialPort("/dev/ttyUSB0", BaudRate::B_57600, NumDataBits::EIGHT, Parity::NONE, NumStopBits::ONE);
// Use SerialPort serialPort("/dev/ttyACM0", 13000); instead if you want to provide a custom baud rate
serialPort.SetTimeout(-1); // Block when reading until any data is received
serialPort.Open();
// Write some ASCII data
serialPort.Write("Hello");
// Read some data back (will block until at least 1 byte is received due to the SetTimeout(-1) call above)
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
If you wanted to enable flow control (hardware or software flow control), you can add it onto the end of the constructor as shown below. If you don't set them, they both default to OFF (the most common setting).
// Enabling hardware flow control
SerialPort serialPort("/dev/ttyUSB0", BaudRate::B_57600, NumDataBits::EIGHT, Parity::NONE, NumStopBits::ONE, HardwareFlowControl::ON, SoftwareFlowControl::OFF);
If you want to read and write binary rather than strings, you can use WriteBinary()
and ReadBinary()
which take vectors of bytes rather than std::string
:
serialPort.WriteBinary(const std::vector<uint8_t>& data);
serialPort.ReadBinary(std::vector<uint8_t>& data);
For more examples, see the files in test/
.
See GitHub Issues.
-
I get the error
Could not open device "/dev/ttyACM0". Is the device name correct and do you have read/write permissions?
, but the device is definitely there. You typically have to add your user to thedialout
group before you can accesstty
devices. -
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 allowSerialPort::Read()
to return. CallSerialPort::SetTimeout(0)
before the serial port is open to set a non-blocking mode.
If you want to use this library in WSL, you'll have to use usbipd to pass-through the USB device.
usbipd wsl list
$ usbipd wsl list
BUSID VID:PID DEVICE STATE
1-1 046d:c332 USB Input Device Not attached
1-4 13d3:5666 USB2.0 HD UVC WebCam Not attached
1-5 2341:0043 Arduino Uno (COM4) Not attached
1-6 046d:0a9c Logitech G432 Gaming Headset, USB Input Device Not attached
1-8 0b05:1837 USB Input Device Not attached
1-9 8087:0a2a Intel(R) Wireless Bluetooth(R) Not attached
Attaching the Arduino Uno (need to be done with Admin priviliges the first time around):
usbipd wsl attach --busid=1-5
/dev/ttyACM0
now appears inside WSL, and you can use CppLinuxSerial with this device like usual.
See CHANGELOG.md.