C++ Windows Serial Port Header-Only Library
The C++ Windows Serial Port Header-Only Library provides a convenient and encapsulated solution for working with serial ports in Windows environments. It abstracts the complexities of the WinApi, offering a clean interface for reading and writing operations. The library is designed to operate seamlessly with C++17 and has been tested on Windows 10.
bool SerialPort::open()
void SerialPort::close()bool SerialPort::isOpen()similar to Arduino's Serial.available()
std::size_t SerialPort::available()Reading and Writing from and to SerialPort using
- read and write Functions
- stream operators (<< | >>)
void SerialPort::writeString(const std::string& str)
SerialPort& operator<<(SerialPort& port, const std::string& str)char SerialPort::readChar()
char& operator>>(SerialPort& port, char& c)std::string SerialPort::readString()
std::string SerialPort::readString(int length)
std::string& operator>>(SerialPort& port, std::string& str)when defining the length of a string to read in, the current thread waits until the required length is met, then the string is returned.
-
Include Header File: Add the "serial.hpp" header file to your C++ project.
#include "serial.hpp"
-
Instantiate SerialPort Object: Create a
SerialPortobject by specifying the port name and baud rate.SerialPort mySerialPort("COM1", 9600);
-
Open and Close Port: Utilize the
open()andclose()methods to establish and terminate the serial port connection.mySerialPort.open(); // Perform operations... mySerialPort.close(); -
Read and Write Data: Use the
readChar()/readString()andwriteString()methods to perform data communication.std::string str = "Hello, Serial!"; mySerialPort.writeString(str); // all commands are for example 3 chars long std::string command = mySerialPort.readString(3);
#include "serial.hpp"
int main() {
SerialPort mySerialPort("COM3", 9600);
if (mySerialPort.open()) {
// Perform operations...
mySerialPort.close();
}
return 0;
}from tests:
SerialPort port(PORT, BAUD_RATE);
std::cout << "Opening port (" << PORT << ")...\n";
if (!port.open()) {
// print error message:
std::cerr << "Couldn't connect to port " << PORT << "\n";
return -1;
}
std::cout << "Is port Open? " << port.isOpen() << "\n";
std::cout << "Writing \"Hello\" to port\n";
port.writeString("Hello");
std::cout << "Waiting for full answer to return...\n";
std::this_thread::sleep_for(std::chrono::milliseconds(500));
std::cout << "Reading in Answer...\n";
std::string answer;
port >> answer;
std::cout << "Printing Answer...\n";
std::cout << answer << "\n";
std::cout << "Closing Port...\n";
port.close();
std::cout << "Done with Test1.\n";
return 0;Testing Environment:
Program running on:
- Windows 10
- Compiled with Mingw64 g++
g++ .\test.cpp -o test.exe -std=c++17 -l pthread
Connected with Arduino Uno on Port COM3
Arduino program:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available()) {
Serial.write(Serial.read());
}
}This library is provided under the MIT License, allowing for flexibility in usage and modification.
Contributions are welcome! If you encounter issues or have suggestions for improvements, please open an issue or submit a pull request.