Linux下的假串口通信 [英] Fake serial communication under Linux

查看:20
本文介绍了Linux下的假串口通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我想在其中模拟设备和调制解调器"之间的连接.该设备将连接到串行端口,并通过该端口与软件调制解调器通信.

I have an application where I want to simulate the connection between a device and a "modem". The device will be connected to a serial port and will talk to the software modem through that.

出于测试目的,我希望能够使用模拟软件设备来测试发送和接收数据.

For testing purposes I want to be able to use a mock software device to test send and receive data.

示例 Python 代码

Example Python code

device = Device()
modem  = Modem()
device.connect(modem)

device.write("Hello")
modem_reply = device.read()

现在,在我的最终应用程序中,我将只传递/dev/ttyS1 或 COM1 或其他任何内容供应用程序使用.但是我怎样才能在软件中做到这一点?我正在运行 Linux 并且应用程序是用 Python 编写的.

Now, in my final app I will just pass /dev/ttyS1 or COM1 or whatever for the application to use. But how can I do this in software? I am running Linux and application is written in Python.

我已经尝试过创建一个 FIFO (mkfifo ~/my_fifo) 并且确实有效,但是我需要一个 FIFO 用于写入和一个用于读取.我想要的是打开 ~/my_fake_serial_port 并对其进行读写.

I have tried making a FIFO (mkfifo ~/my_fifo) and that does work, but then I'll need one FIFO for writing and one for reading. What I want is to open ~/my_fake_serial_port and read and write to that.

我也支付了 pty 模块,但也无法让它工作.我可以从 pty.openpty() 获取主从文件描述符,但尝试读取或写入它们只会导致 IOError Bad File Descriptor 错误消息.

I have also lpayed with the ptymodule, but can't get that to work either. I can get a master and slave file descriptor from pty.openpty() but trying to read or write to them only causes IOError Bad File Descriptor error message.

评论将我指向 SO 问题 是否有像 COM0COM 这样的程序在 linux? 中,它使用 socat 来设置虚拟串行连接.我是这样使用的:

Comments pointed me to the SO question Are there some program like COM0COM in linux? which uses socat to setup a virtual serial connection. I used it like this:

socat PTY,link=$HOME/COM1 PTY,link=$HOME/COM2

感谢你们给我提供了宝贵的信息.我选择接受 Vinay Sajips 的回答,因为这是我在 socat 建议出现之前寻求的解决方案.它似乎工作得很好.

To the rest of you, thank you for giving me valuable information. I chose to accept Vinay Sajips's answer since that is the solution which I went for before the socat suggestion showed up. It seems to work well enough.

推荐答案

可能最好使用 pyserial与串口通信,你可以创建一个模拟版本的 serial.Serial 类,它实现了 read, readline, write 和你需要的任何其他方法.

It's probably best to use pyserial to communicate with the serial port, and you can just create a mock version of the serial.Serial class which implements read, readline, write and any other methods you need.

这篇关于Linux下的假串口通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆