简体   繁体   English

Python中的半双工串行通信

[英]Half-duplex serial communications in Python

I have a device with a built-in USB/Serial adapter (shows up as a Prolific PL2303). 我有一个内置USB /串行适配器的设备(显示为Prolific PL2303)。 The device documentation provides full details on how to communicate with it, and provides a sample Windows app. 设备文档提供了有关如何与之通信的完整详细信息,并提供了一个示例Windows应用程序。 However, I need to use it on Linux - ideally with Python but I'm not too precious about that. 但是,我需要在Linux上使用它 - 理想情况下使用Python,但我对此并不太珍贵。

The device docs state the device runs at 9600, 8N1 with half duplex. 设备docs说明设备运行在9600,8N1半双工。 The Windows app works fine - device works fine (so I know it's not a device problem). Windows应用程序工作正常 - 设备工作正常(所以我知道这不是一个设备问题)。 However, I can't communicate with it using Python on Linux. 但是,我无法在Linux上使用Python与它进行通信。 I'm using pySerial, and I've tried a similar (full duplex, also PL2303) device which works fine. 我正在使用pySerial,我尝试过类似的(全双工,也是PL2303)设备,它工作正常。

I've tried several combinations of setting xonxoff, rtscts etc. I've also tried RTS toggling using setRTS(True) and checking for CTS and DSR etc - gets all return False. 我已经尝试了几种设置xonxoff,rtscts等的组合。我也尝试过使用setRTS(True)进行RTS切换并检查CTS和DSR等 - 得到全部返回False。 I can successfully open the device using pySerial (I can see activity light flashing - simple but effective test) and pySerial complains if you unplug it during reads/writes. 我可以使用pySerial成功打开设备(我可以看到活动指示灯闪烁 - 简单但有效的测试),如果你在读/写期间拔掉它,pySerial就会抱怨。

Doesn't seem to matter what flags/lines I set or what data I send, always get the same result. 似乎没关系我设置的标志/行或我发送的数据,总是得到相同的结果。

>>> s=serial.Serial()
>>> s.port('/dev/ttyUSB1')
>>> s.open()
>>> s
Serial<id=0x7fe94b533b50, open=True>(port='/dev/ttyUSB1', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=3, xonxoff=0, rtscts=0, dsrdtr=0)
>>> s.write('\2CMD2292\r')  
>>> s.inWaiting()
0
>>> s.setRTS(True)
>>> s.getCTS()
False
(several iterations of above with different flags).

Ignore the data in the write command - that's just a status check command. 忽略write命令中的数据 - 这只是一个状态检查命令。 Doesn't matter what goes there, the device should either respond with an answer or an error. 无论发生什么,设备应该回答一个答案或错误。

I've even resorted to closing and re-opening the device in between setting various ctsrts flags etc., and unplugging / replugging it to force resets. 我甚至在设置各种ctsrts标志等之间关闭并重新打开设备,并拔出/重新插入它以强制重置。 As above it continues to work fine using the Windows test app. 如上所述,它继续使用Windows测试应用程序正常工作。 I can't use Portmon etc. to sniff the Windows port traffic as it's a 64bit Win7 install and I don't currently have time to build an XP machine. 我无法使用Portmon等来嗅探Windows端口流量,因为它是64位Win7安装,我目前没有时间构建XP机器。

Any thoughts? 有什么想法吗?

UPDATE: I've also tried all of the above using Python on the same Windows box that the demo app works on. 更新:我也在演示应用程序所在的同一个Windows框中使用Python尝试了以上所有内容。 It definitely opens the port and communicates, but again no info back regardless of what's written. 它肯定会打开端口并进行通信,但无论写入什么,都不会再回复任何信息。

UPDATE2: It looks like it might be device-driver related. UPDATE2:看起来它可能与设备驱动程序有关。 Some additional background research suggests that some PL2303 chips have functionality to support half-duplex, but this isn't supported by the Linux drivers. 一些额外的背景研究表明,一些PL2303芯片具有支持半双工的功能,但Linux驱动程序不支持此功能。 The Windows demo app comes with a dedicated device driver and the app doesn't work on a clean test machine using default Windows drivers. Windows演示应用程序附带专用设备驱动程序,该应用程序无法在使用默认Windows驱动程序的干净测试计算机上运行。 This makes me think that while I can connect to it successfully through Python, I can't control the duplex comms (eg even in an STX/ETX way) and therefore this might be a hopeless case. 这让我觉得虽然我可以通过Python成功连接到它,但我无法控制双工通信(例如,甚至以STX / ETX方式),因此这可能是一个绝望的情况。 sigh . 叹了口气

UPDATE3: Thanks for the comments below. 更新3:感谢下面的评论。 However, I couldn't find any way round this. 但是,我找不到任何方法。 I tried a USB protocol analyser, and I tried disassembling the driver but it just became rather time-consuming, so in the end I took the device apart and after some tinkering I managed to replace the existing usb-serial adapter with a proper PL2303 part - on the device side, it was just a basic 2-wire serial interface so it didn't care what was talking to it. 我尝试了一个USB协议分析仪,我尝试拆卸驱动程序,但它只是相当耗时,所以最后我把设备分开了,经过一些修补后我设法用适当的PL2303部件替换现有的usb-serial适配器 - 在设备方面,它只是一个基本的2线串行接口,因此它并不关心与之交谈的内容。 I can't seem to close this question so I'll leave it as-is. 我似乎无法关闭这个问题所以我会保持原样。

Try 尝试

s.flush()

just after your s.write call 在你的s.write电话之后

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python-Arduino 串行半双工:Arduino 读取之前的串行输入而不是当前的 - Python-Arduino Serial Half-Duplex: Arduino reads previous serial input instead of current 带有自动RTS的Python PySerial通过使用Beaglebone Black Angstrom的半双工RS-485分线板 - Python PySerial with Auto RTS through Half-Duplex RS-485 breakout board using Beaglebone Black Angstrom 对 USB 全速半双工通话优先级的 PySerial 感到困惑 - Confused about PySerial with USB Full-speed half-duplex talking priority 带扭曲的串行通信 - Serial Communications with twisted Ubuntu配合Python使用PySerial通过RS485半双工传输问题 - Ubuntu mate Python using PySerial via RS485 half duplex transmit problems 有没有办法允许使用python在excel中进行双面打印? - Is there a way to allow duplex printing in excel using python? Python-对多个进程使用相同的双工Pipe() - Python - Use same duplex Pipe() for multiple processes 通过Python以双工模式打印PDF文件 - Print PDF file in duplex mode via Python 通过管道进行python进程通信:竞争条件 - python process communications via pipes: Race condition 基本的Python套接字通信问题; 与netcat合作 - Basic Python socket communications questions; working with netcat
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM