Python 中的虚拟串口设备?

发布于 2024-08-22 02:05:37 字数 144 浏览 6 评论 0原文

我知道我可以使用 pySerial 与串行设备通信,但是如果我现在没有设备但仍需要为其编写客户端怎么办?我怎样才能用Python编写一个“虚拟串行设备”并让pySerial与其对话,就像我运行本地Web服务器一样?也许我只是搜索得不好,但我一直无法找到有关此主题的任何信息。

I know that I can use e.g. pySerial to talk to serial devices, but what if I don't have a device right now but still need to write a client for it? How can I write a "virtual serial device" in Python and have pySerial talk to it, like I would, say, run a local web server? Maybe I'm just not searching well, but I've been unable to find any information on this topic.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

绝不服输 2024-08-29 02:05:38

这是我到目前为止所做和解决的事情:

import os, pty, serial

master, slave = pty.openpty()
s_name = os.ttyname(slave)

ser = serial.Serial(s_name)

# To Write to the device
ser.write('Your text')

# To read from the device
os.read(master,1000)

如果您创建更多虚拟端口,您将不会有任何问题,因为不同的主机会获得不同的文件描述符,即使它们具有相同的名称。

this is something I did and worked out for me so far:

import os, pty, serial

master, slave = pty.openpty()
s_name = os.ttyname(slave)

ser = serial.Serial(s_name)

# To Write to the device
ser.write('Your text')

# To read from the device
os.read(master,1000)

If you create more virtual ports you will have no problems as the different masters get different file descriptors even if they have the same name.

寂寞陪衬 2024-08-29 02:05:38

如果您运行的是 Linux,则可以使用 socat 命令来执行此

socat -d -d pty,raw,echo=0 pty,raw,echo=0

操作 ,如下所示:运行时,它会通知您它创建了哪些串行端口。在我的机器上,这看起来像:

2014/04/23 15:47:49 socat[31711] N PTY is /dev/pts/12
2014/04/23 15:47:49 socat[31711] N PTY is /dev/pts/13
2014/04/23 15:47:49 socat[31711] N starting data transfer loop with FDs [3,3] and [5,5]

现在我可以写入 /dev/pts/13 并在 /dev/pts/12 上接收,反之亦然。

If you are running Linux you can use the socat command for this, like so:

socat -d -d pty,raw,echo=0 pty,raw,echo=0

When the command runs, it will inform you of which serial ports it has created. On my machine this looks like:

2014/04/23 15:47:49 socat[31711] N PTY is /dev/pts/12
2014/04/23 15:47:49 socat[31711] N PTY is /dev/pts/13
2014/04/23 15:47:49 socat[31711] N starting data transfer loop with FDs [3,3] and [5,5]

Now I can write to /dev/pts/13 and receive on /dev/pts/12, and vice versa.

怪我闹别瞎闹 2024-08-29 02:05:38

我能够使用以下代码模拟任意串行端口 ./foo

SerialEmulator.py

import os, subprocess, serial, time

# this script lets you emulate a serial device
# the client program should use the serial port file specifed by client_port

# if the port is a location that the user can't access (ex: /dev/ttyUSB0 often),
# sudo is required

class SerialEmulator(object):
    def __init__(self, device_port='./ttydevice', client_port='./ttyclient'):
        self.device_port = device_port
        self.client_port = client_port
        cmd=['/usr/bin/socat','-d','-d','PTY,link=%s,raw,echo=0' %
                self.device_port, 'PTY,link=%s,raw,echo=0' % self.client_port]
        self.proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        time.sleep(1)
        self.serial = serial.Serial(self.device_port, 9600, rtscts=True, dsrdtr=True)
        self.err = ''
        self.out = ''

    def write(self, out):
        self.serial.write(out)

    def read(self):
        line = ''
        while self.serial.inWaiting() > 0:
            line += self.serial.read(1)
        print line

    def __del__(self):
        self.stop()

    def stop(self):
        self.proc.kill()
        self.out, self.err = self.proc.communicate()

需要安装 socat (sudo apt-get install socat),以及 pyserial python 包(pip install pyserial)。

打开python解释器并导入SerialEmulator:

>>> from SerialEmulator import SerialEmulator
>>> emulator = SerialEmulator('./ttydevice','./ttyclient') 
>>> emulator.write('foo')
>>> emulator.read()

然后您的客户端程序可以用pyserial包装./ttyclient,创建虚拟串行端口。如果您无法修改客户端代码,您也可以将 client_port 设置为 /dev/ttyUSB0 或类似的设置,但可能需要 sudo

另请注意此问题:Pyserial 无法与虚拟端口配合使用< /a>

I was able to emulate an arbitrary serial port ./foo using this code:

SerialEmulator.py

import os, subprocess, serial, time

# this script lets you emulate a serial device
# the client program should use the serial port file specifed by client_port

# if the port is a location that the user can't access (ex: /dev/ttyUSB0 often),
# sudo is required

class SerialEmulator(object):
    def __init__(self, device_port='./ttydevice', client_port='./ttyclient'):
        self.device_port = device_port
        self.client_port = client_port
        cmd=['/usr/bin/socat','-d','-d','PTY,link=%s,raw,echo=0' %
                self.device_port, 'PTY,link=%s,raw,echo=0' % self.client_port]
        self.proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        time.sleep(1)
        self.serial = serial.Serial(self.device_port, 9600, rtscts=True, dsrdtr=True)
        self.err = ''
        self.out = ''

    def write(self, out):
        self.serial.write(out)

    def read(self):
        line = ''
        while self.serial.inWaiting() > 0:
            line += self.serial.read(1)
        print line

    def __del__(self):
        self.stop()

    def stop(self):
        self.proc.kill()
        self.out, self.err = self.proc.communicate()

socat needs to be installed (sudo apt-get install socat), as well as the pyserial python package (pip install pyserial).

Open the python interpreter and import SerialEmulator:

>>> from SerialEmulator import SerialEmulator
>>> emulator = SerialEmulator('./ttydevice','./ttyclient') 
>>> emulator.write('foo')
>>> emulator.read()

Your client program can then wrap ./ttyclient with pyserial, creating the virtual serial port. You could also make client_port /dev/ttyUSB0 or similar if you can't modify client code, but might need sudo.

Also be aware of this issue: Pyserial does not play well with virtual port

又爬满兰若 2024-08-29 02:05:38

使用 com0com (如果您使用的是 Windows)之类的东西来设置虚拟串行端口可能会更容易,并以此为基础进行开发。

It may be easier to using something like com0com (if you're on Windows) to set up a virtual serial port, and develop on that.

在你怀里撒娇 2024-08-29 02:05:38

如果您需要在不访问设备的情况下测试应用程序,也许循环设备可以完成这项工作。它包含在 pySerial 2.5 https://pythonhosted.org/pyserial/url_handlers.html#loop

Maybe a loop device will do the job if you need to test your application without access to a device. It's included in pySerial 2.5 https://pythonhosted.org/pyserial/url_handlers.html#loop

淡看悲欢离合 2024-08-29 02:05:38

这在一定程度上取决于您现在想要完成的任务...

您可以将对串行端口的访问包装在一个类中,并编写一个实现来使用套接字 I/O 或文件 I/O。然后编写串行 I/O 类以使用相同的接口,并在设备可用时将其插入。 (这实际上是一个很好的设计,无需外部硬件即可测试功能。)

或者,如果您打算使用串行端口作为命令行接口,则可以使用 stdin/stdout。

或者,还有关于linux 的虚拟串行设备的其他答案。

It depends a bit on what you're trying to accomplish now...

You could wrap access to the serial port in a class and write an implementation to use socket I/O or file I/O. Then write your serial I/O class to use the same interface and plug it in when the device is available. (This is actually a good design for testing functionality without requiring external hardware.)

Or, if you are going to use the serial port for a command line interface, you could use stdin/stdout.

Or, there's this other answer about virtual serial devices for linux.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文