For Linux
CppLinuxSerial
一个简单好用的serial库 https://github.com/gbmhunter/CppLinuxSerial
- 下载、编译、安装如下
git clone https://github.com/gbmhunter/CppLinuxSerial.git cd CppLinuxSerial/ && mkdir build && cd build cmake .. make sudo make install
- 安装位置如下,生成了一个静态库,头文件也只有俩
Installing: /usr/local/lib/libCppLinuxSerial.a Installing: /usr/local/include/CppLinuxSerial Installing: /usr/local/include/CppLinuxSerial/SerialPort.hpp Installing: /usr/local/include/CppLinuxSerial/Exception.hpp
-
测试
// serial库的头文件,需要在CmakeLists.txt里面添加库,或这在编译时添加库 #include <CppLinuxSerial/SerialPort.hpp> using namespace mn::CppLinuxSerial; int main() { // 创建对象 // 自定义波特率 SerialPort serialPort("/dev/ttyACM0", 13000); SerialPort serialPort("/dev/ttyUSB0", BaudRate::B_57600); // 接收超时时间 // 阻塞接收: 输入为0,代表非阻塞; 输入为-1代表阻塞接收 serialPort.SetTimeout(-1); // Block when reading until any data is received // 打开 serialPort.Open(); // 发送 serialPort.Write("Hello"); // 接收 std::string readData; serialPort.Read(readData); // 关闭 serialPort.Close(); }