开发者

Using pySerial to connect to a non-COM port

In Hyperterminal I am able to connect to a serial port called "X64-CL_iPro_1_Serial_0" where I am able to send/receive ASCII text to a camera. However when I try to connect to the same port with pySerial, it throws an exception:

SerialException: could not open port X64-CL_iPro_1_Serial_0: [Error 2] The system cannot find the file specified.

I don't understand why Hyperterminal can detect the port and communicate with it, but Python can't. I downloaded this script from the pySerial website that displays a list of serial ports, and the only ports it came up with was COM1 and COM2, neither of which I can connect to.

My code is very simple, and looks like this:

import serial
port = "X64-CL_iPro_1_Serial_0开发者_JAVA百科"
ser = serial.Serial(port)

Am I doing anything wrong? Is there a way to work around this? Thanks ahead of time.

Edit: It should also be noted that this port does not show up in the device manager, and neither does COM1 or COM2.


The problem lies in the enumeration code you linked. It is wrong in two regards:

  1. It uses a fixed GUID_CLASS_COMPORT to enumerate. It should instead ask the GUID through SetupDiClassGuidsFromName, passing "Ports" as description of the class for which it is asking for names.
  2. The code insists of asking for the friendly name of the port. But if the only goal is to open the device (instead of displaying to an user), it should directly access the DevicePath element, which is a weird-looking-but-perfectly-valid port name to pass to pySerial. The friendly name might even be totally missing.


aside

I'm not clear the question is about non-serial-port use through pyserial, or serial port that is not a COMX port in enumeration.

This may be a bit OT or too hard code for your use, but here goes first, using some other file in a pyserial object:

foo = serial.Serial()
peer = serial.Serial()
foo.fd, peer.fd = posix.openpty()
try: foo._isOpen = peer._isOpen = True  # depending on pyserial version
except: pass
foo._reconfigurePort()
peer.setTimeout(timeout=0.1)
peer._reconfigurePort()

Regarding second, remember that ports beyond COM9 use weird windows notation \\.\COM10, perhaps that's what Hyperterminal does for you. pyserial doesn't do it, so perhaps you need to open the port something like this:

s = serial.Serial("\\\\.\\X64-CL_iPro_1_Serial_0")  # also remember to escape backslash
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜