PySerial 和 IronPython - 出现奇怪的错误

发布于 2024-09-04 22:19:06 字数 740 浏览 5 评论 0原文

我有一个设备连接到 COM31。我创建串行连接所需的代码看起来非常简单

port = 31
trex_serial = serial.Serial(port - 1, baudrate=19200, stopbits=serial.STOPBITS_ONE, timeout=1)

当我使用Python2.6运行它时,愚蠢的代码可以工作,但是当我使用IronPython2.6.1执行时,这就是我得到的:

Traceback (most recent call last):
  File "c:\Python26\lib\site-packages\serial\serialutil.py", line 188, in __init__

  File "c:\Python26\lib\site-packages\serial\serialutil.py", line 236, in setPort

  File "c:\Python26\lib\site-packages\serial\serialcli.py", line 139, in makeDeviceName

  File "c:\Python26\lib\site-packages\serial\serialcli.py", line 17, in device

IndexError: index out of range: 30

我不确定发生了什么。 PySerial 明确表示它符合 IronPython 标准。 有什么想法我做错了什么吗?

I have a device connected to COM31. And the code that I need to create a serial connection looks very simple

port = 31
trex_serial = serial.Serial(port - 1, baudrate=19200, stopbits=serial.STOPBITS_ONE, timeout=1)

The foollowing code works when I run it using Python2.6, but when executed by IronPython2.6.1, this is what I get:

Traceback (most recent call last):
  File "c:\Python26\lib\site-packages\serial\serialutil.py", line 188, in __init__

  File "c:\Python26\lib\site-packages\serial\serialutil.py", line 236, in setPort

  File "c:\Python26\lib\site-packages\serial\serialcli.py", line 139, in makeDeviceName

  File "c:\Python26\lib\site-packages\serial\serialcli.py", line 17, in device

IndexError: index out of range: 30

I am not sure what is going on. PySerial clearly says that it is IronPython compliant.
Any ideas what am I doing wrong?

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

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

发布评论

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

评论(1

月野兔 2024-09-11 22:19:06

IronPython 正在询问 .NET 端口是什么。它们的列举方式不同。您很可能要求打开一个就 IronPython/.NET 而言不存在的连接。要找出“真实”端口号,请使用从 pySerial 扫描示例修改的以下代码。然后使用列出的 COM 旁边的号码。

import serial

def scan():
#scan for available ports. return a list of tuples (num, name)
available = []
for i in range(256):
    try:
        s = serial.Serial(i)
        available.append( (i, s.portstr))
        s.close()   # explicit close 'cause of delayed GC in java
    except serial.SerialException:
        pass
    #You must add this check, otherwise the scan won't complete
    except IndexError as Error:
        pass

for n,s in available:
    print "(%d) %s" % (n,s)

return available

对我来说,输出如下所示:

(0) COM9

(1) COM15

(2) COM16

(3) COM1

(4) COM15

然后,当您尝试打开连接时,使用左侧的数字而不是实际的 COMportNumber - 1。例如,我需要打开到 COM15 的连接,因此使用上面的扫描:

def IOCardConnect():
try:
    connection = serial.Serial(4, 115200, timeout=1, parity=serial.PARITY_NONE)
    print "Connection Succesful"
    return connection
except serial.SerialException as Error:
    print Error

此外,一旦连接,pySerial 将期望字节写入 connectio,而不是字符串。所以请确保您像这样发送:

#Use the built in bytes function to convert to a bytes array.
connection.write(bytes('Data_To_Send'))

IronPython is asking .NET what the ports are. They are enumerated differently. Most likely you are asking to open a connection that doesn't exist as far as IronPython/.NET is concerned. To find out the "real" port number, use the following code modified from the pySerial scan examples. Then use the number next to the listed COM.

import serial

def scan():
#scan for available ports. return a list of tuples (num, name)
available = []
for i in range(256):
    try:
        s = serial.Serial(i)
        available.append( (i, s.portstr))
        s.close()   # explicit close 'cause of delayed GC in java
    except serial.SerialException:
        pass
    #You must add this check, otherwise the scan won't complete
    except IndexError as Error:
        pass

for n,s in available:
    print "(%d) %s" % (n,s)

return available

The output looks like this for me:

(0) COM9

(1) COM15

(2) COM16

(3) COM1

(4) COM15

Then when you try to open the connection, use the number on the left NOT the actual COMportNumber - 1. For instance I need to open a connection to COM15, so using the above scan:

def IOCardConnect():
try:
    connection = serial.Serial(4, 115200, timeout=1, parity=serial.PARITY_NONE)
    print "Connection Succesful"
    return connection
except serial.SerialException as Error:
    print Error

Also, once you are connected, pySerial will expect bytes to write to the connectio, not strings. So make sure you send like this:

#Use the built in bytes function to convert to a bytes array.
connection.write(bytes('Data_To_Send'))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文