模拟串口
我正在 Linux 中编写一个 C 程序,它将从串行端口读取/写入。我知道需要在端口上读取和写入的数据,但我当前没有串行端口来测试它。
有没有办法模拟串口?读/写文件就足够了吗?我可以让一个进程写入文件,而另一个进程读取该数据并将其他数据写回文件。或者还有其他可以用来模拟端口的工具吗?
谢谢
I am writing a C program in Linux which will read/write to/from a serial port. I know the data that needs to be read and written on the port but I don't have a serial port to currently test this with.
Is there any way to simulate a serial port? Would reading/writing to a file be sufficient? I can have one process write to the file while another process reads that data and writes back other data to the file. Or are there others tools that can be used to simulate a port?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Linux 上的串行端口是终端设备。一个接近的模拟是创建一个伪终端对;通常与串行端口通信的程序被告知打开伪终端的从机端,并且模拟器从主机端写入和读取。
pty(7)
手册页提供了更多信息。Serial ports on Linux are terminal devices. A close simulation is to create a pseudo-terminal pair; the program that normally talks to the serial port is instead told to open the slave side of the pseudo-terminal, and the simulator writes and reads from the master side.
The
pty(7)
man page has more information.尽管这是一个老话题,而且我的答案并不完全是OP想要的东西,但我决定分享我的经验,因为其他人可能会像我一样遇到它。我没有使用简单的模拟,而是使用了名为“串行转以太网连接器”的软件来访问测试应用程序所需的特定设备。对我来说效果很好。
Despite being an old topic, and my answer is not exactly something the OP was looking for, I decided to share my experience, as someone else might come across it like I did. Instead of straightforward simulation, I used the software called Serial to Ethernet Connector to gain access to the specific device I needed to test the app with. Worked nicely for me.
如果您不关心端口设备特有的属性,那么字符设备,即使是像普通
stdin
和stdout
这样简单的设备也应该可以工作。A character device, even something as simple as normal
stdin
andstdout
should work if you don't care about attributes specific to port devices.