• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

gbmhunter/CppLinuxSerial: Serial port library written in C++.

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

gbmhunter/CppLinuxSerial

开源软件地址(OpenSource Url):

https://github.com/gbmhunter/CppLinuxSerial

开源编程语言(OpenSource Language):

C++ 49.0%

开源软件介绍(OpenSource Introduction):

CppLinuxSerial

Linux serial port library written in C++.

Build Status

Description

Library for communicating with COM ports on a Linux system.

  • Simple API
  • Supports custom baud rates
  • cmake based build system

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:

    $ 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!

Examples

#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, and one stop bit (8n1)
	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

For more examples, see the files in test/.

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. Call SerialPort::SetTimeout(0) before the serial port is open to set a non-blocking mode.

Changelog

See CHANGELOG.md.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
fail0verflow/switch-linux: Linux for the Switch发布时间:2022-08-15
下一篇:
autotest/autotest: Autotest - Fully automated tests on Linux发布时间:2022-08-15
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap