C语言 C:跨平台RS-232串口库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2973402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C: cross-platform RS-232 serial library?
提问by Hamza
I am looking for an open source cross-platform library for working with the serial port in C, something along the lines of the awesome PySeriallibrary (unfortunately I have to use C for this application).
我正在寻找一个开源的跨平台库,用于在 C 中使用串行端口,类似于令人敬畏的PySerial库(不幸的是,我必须将 C 用于此应用程序)。
I have only found this one: http://www.teuniz.net/RS-232/, and that doesn't seem to have mention OS X compatibility.
我只找到了这个:http://www.teuniz.net/RS-232/,而且似乎没有提到 OS X 兼容性。
回答by Martin L
You would think that a cross-platform C library for serial ports is something that would have existed for 20 years already, but as of 2013 there still wasn't one, so I created libserialport.
您可能会认为用于串行端口的跨平台 C 库已经存在了 20 年,但截至 2013 年仍然没有,所以我创建了libserialport。
It is tested on Windows, Mac OS X, Linux (including Android), BSD and will probably build on other Unix systems also. It supports:
它在 Windows、Mac OS X、Linux(包括 Android)、BSD 上进行了测试,并且可能还会在其他 Unix 系统上构建。它支持:
- Port enumeration (obtaining a list of serial ports on the system)
- Opening, closing and querying ports
- Setting port parameters (baud rate, parity, etc.)
- Port signalling operations (modem control lines, breaks, etc.)
- Reading, writing, and flushing data
- Waiting for events
- Obtaining error information
- 端口枚举(获取系统上的串口列表)
- 打开、关闭和查询端口
- 设置端口参数(波特率、奇偶校验等)
- 端口信令操作(调制解调器控制线、中断等)
- 读取、写入和刷新数据
- 等待事件
- 获取错误信息
The initial 0.1.0 release is in active use by the Sigrokproject for supporting serial hardware and is packaged in several Linux distributions already. The git version also includes new API calls for getting information about USB serial adapters (VID/PID etc).
Sigrok项目正在积极使用最初的 0.1.0 版本来支持串行硬件,并且已经打包在多个 Linux 发行版中。git 版本还包括新的 API 调用,用于获取有关 USB 串行适配器(VID/PID 等)的信息。
回答by Mark Rushakoff
I realize that you said C and not C++, but perhaps you could use Boost Asio's serial portsand compile that into a library to be linked with your C application. It claims to be POSIX compatible, and OS X is POSIX.
我意识到您说的是 C 而不是 C++,但也许您可以使用Boost Asio 的串行端口并将其编译成一个库以与您的 C 应用程序链接。它声称与 POSIX 兼容,而 OS X 是 POSIX。
回答by wjwwood
This is a shameless plug, but I have a MIT licensed library for RS-232 communication which is portable to Windows and Linux and OS X here:
这是一个无耻的插件,但我有一个 MIT 许可的 RS-232 通信库,可移植到 Windows、Linux 和 OS X 在这里:
回答by Frédéric
I released mine under the MIT license: https://github.com/Marzac/rs232
我在 MIT 许可下发布了我的:https: //github.com/Marzac/rs232
It is programmed in C and works well for Windows, Mac and Linux.
它是用 C 语言编写的,适用于 Windows、Mac 和 Linux。
回答by trivvo
Get some additional OS X-specific Objective-C code along the lines of AMSerialPort, goSerial, serialconnect, PolKit, ... and implement a conditional compilation of your C source file.
沿着 AMSerialPort、goSerial、serialconnect、PolKit 等行获取一些额外的 OS X 特定的 Objective-C 代码,并实现 C 源文件的条件编译。
回答by caf
OS X supports the standard POSIX serial API, from <termios.h>and <unistd.h>. Look up the functions tcsetattr(), cfsetspeed()and so on.
OS X 支持标准的 POSIX 串行 API,来自<termios.h>和<unistd.h>. 查找函数tcsetattr(),cfsetspeed()等等。

