打开Python Serial P时出错

2024-04-24 12:32:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图运行一个脚本,一个同学已经写并演示给我看。所以我知道代码是正确的,这只是和我们的机器配置的不同有关。代码如下:

#!/usr/bin/python

#import statements
import serial
import os
import time

#global constants
control_byte = '\n'
ACL_1_X_addr = ord('X')
ACL_1_Y_addr = ord('Y')
ACL_1_Z_addr = ord('Z')
GYRO_1_X_addr = ord('I')
GYRO_1_Y_addr = ord('J')
GYRO_1_Z_addr = ord('K')


#clear the screen
os.system('clear')

#initialize the serial port
s = serial.Serial()
s.port = 10
s.baudrate = 56818
s.open()

所有内容都会运行到最后一行s.open,在最后一行出现错误:

Traceback (most recent call last):
  File "serial_reader.py", line 25, in <module>
    s.open()
  File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 282, in open
    self._reconfigurePort()
  File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 311, in _reconfigurePort
    raise SerialException("Could not configure port: %s" % msg)
serial.serialutil.SerialException: Could not configure port: (5, 'Input/output error')

我想我需要换一个我要开的港口,但我试过其他一些没有运气的港口。有人知道发生了什么吗?

顺便说一下,我使用的是Python2.7.4


Tags: 代码inpyimportosportusrline
2条回答

isOpen()代替open()

s=serial.Serial("/dev/ttyS0")
s.isOpen()

我猜你需要像s.port="/dev/ttys0"这样的东西。。。数字端口用于“COM10”样式的windows端口,这些端口没有到文件系统的映射。

如果您这样做,您的串行端口应该列出ls /dev/tty*

相关问题 更多 >