Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python raspi SerialException:设备报告准备读取,但未返回任何数据。用于自动钢筋混凝土车_Python_Arduino_Raspberry Pi_Serial Port - Fatal编程技术网

Python raspi SerialException:设备报告准备读取,但未返回任何数据。用于自动钢筋混凝土车

Python raspi SerialException:设备报告准备读取,但未返回任何数据。用于自动钢筋混凝土车,python,arduino,raspberry-pi,serial-port,Python,Arduino,Raspberry Pi,Serial Port,我一直在关注《用树莓皮和阿杜伊诺开始机器人技术》一书,但在代码方面遇到了问题。我对编程非常陌生,我正在使用这本书来熟悉Pi、Arduino和Python。这本书有点含糊不清,没有提供解决问题的方法 Traceback (most recent call last): File "/home/Danberry/Roject/pi_roamer_01.py", line 80, in <module> val = ser.readline().decode('utf-8')

我一直在关注《用树莓皮和阿杜伊诺开始机器人技术》一书,但在代码方面遇到了问题。我对编程非常陌生,我正在使用这本书来熟悉Pi、Arduino和Python。这本书有点含糊不清,没有提供解决问题的方法

Traceback (most recent call last):
  File "/home/Danberry/Roject/pi_roamer_01.py", line 80, in <module>
    val = ser.readline().decode('utf-8')
  File "/home/Danberry/.local/lib/python2.7/site-packages/serial/serialposix.py", line 501, in read
    'device reports readiness to read but returned no data '
SerialException: device reports readiness to read but returned no data (device disconnected or multiple access on port?)
回溯(最近一次呼叫最后一次):
文件“/home/Danberry/Roject/pi_roamer_01.py”,第80行,在
val=ser.readline().decode('utf-8')
文件“/home/Danberry/.local/lib/python2.7/site packages/serial/serialposix.py”,第501行,已读
'设备报告已准备好读取,但未返回任何数据'
SerialException:设备报告已准备好读取,但未返回任何数据(设备已断开连接或端口上的多重访问?)
我已经确认一切都已正确连接并单独工作。现在的问题是让覆盆子圆周率去读它

import serial
import time
import random

from Adafruit_MotorHAT import Adafruit_MotorHAT as amhat
from Adafruit_MotorHAT import Adafruit_DCMotor as adamo

# create motor objects
motHAT = amhat(addr=0x60)
mot1 = motHAT.getMotor(1)
mot2 = motHAT.getMotor(2)
mot3 = motHAT.getMotor(3)
mot4 = motHAT.getMotor(4)

# open serial port
ser = serial.Serial('/dev/ttyACM0', 115200)

# create variables
# sensors
distMid = 0.0
distLeft = 0.0
distRight = 0.0

# motor multipliers
m1Mult = 1.0
m2Mult = 1.0
m3Mult = 1.0
m4Mult = 1.0

# distance threshold
distThresh = 12.0
distCutOff = 30.0

# speeds
speedDef = 200
leftSpeed = speedDef
rightSpeed = speedDef
turnTime = 1.0
defTime = 0.1
driveTime = defTime

def driveMotors(leftChnl = speedDef, rightChnl = speedDef, duration = defTime):
    # determine the speed of each motor by multiplying
    # the channel by the motors multiplier
    m1Speed = leftChnl * m1Mult
    m2Speed = leftChnl * m2Mult
    m3Speed = rightChnl * m3Mult
    m4Speed = rightChnl * m4Mult

    # set each motor speed. Since the speed can be a
    # negative number, we take the absolute value
    mot1.setSpeed(abs(int(m1Speed)))
    mot2.setSpeed(abs(int(m2Speed)))
    mot3.setSpeed(abs(int(m3Speed)))
    mot4.setSpeed(abs(int(m4Speed)))

    # run the motors. if the channel is negative, run
    # reverse. else run forward
    if(leftChnl < 0):
        mot1.run(amhat.BACKWARD)
        mot2.run(amhat.BACKWARD)
    else:
        mot1.run(amhat.FORWARD)
        mot2.run(amhat.FORWARD)

    if (rightChnl > 0):
        mot3.run(amhat.BACKWARD)
        mot4.run(amhat.BACKWARD)
    else:
        mot3.run(amhat.FORWARD)
        mot4.run(amhat.FORWARD)

    # wait for duration
    time.sleep(duration)

try:
    while 1:
        # read the serial port
        val = ser.readline().decode('utf=8')
        print val

        # parse the serial string
        parsed = val.split(',')
        parsed = [x.rstrip() for x in parsed]

        # only assign new values if there are
        # three or more available
        if(len(parsed)>2):
            distMid = float(parsed[0] + str(0))
            distLeft = float(parsed[1] + str(0))
            distRight = float(parsed[2] + str(0))

        # apply cutoff distance
        if(distMid > distCutOff):
            distMid = distCutOff
        if(distLeft > distCutOff):
            distLeft = distCutOff
        if(distRight > distCutOff):
            distRight = distCutOff

        # reset driveTime
        driveTime = defTime

        # if obstacle to left, steer right by increasing
        # leftSpeed and running rightSpeed negative defSpeed
        # if obstacle to right, steer to left by increasing
        # rightSpeed and running leftSpeed negative
        if(distLeft <= distThresh):
            leftSpeed = speedDef
            rightSpeed = -speedDef
        elif (distRight <= distThresh):
            leftSpeed = -speedDef
            rightSpeed = speedDef
        else:
            leftSpeed = speedDef
            rightSpeed = speedDef

        # if obstacle dead ahead, stop then turn toward most
        # open direction. if both directions open, turn random
        if(distMid <= distThresh):
            # stop
            leftSpeed = 0
            rightSpeed = 0
            driveMotors(leftSpeed, rightSpeed, 1)
            time.sleep(1)
            leftSpeed = -150
            rightSpeed = -150
            driveMotors(leftSpeed, rightSpeed, 1)
            # determine preferred direction. if distLeft >
            # distRight, turn left. if distRight > distLeft,
            # turn right. if equal, turn random
            dirPref = distRight - distLeft
            if(dirPref == 0):
                dirPref = random.random()
            if(dirPref < 0):
                leftSpeed = -speedDef
                rightSpeed = speedDef
            elif(dirPref > 0):
                leftSpeed = speedDef
                rightSpeed = -speedDef
            driveTime = turnTime

        # drive the motors
        driveMotors(leftSpeed, rightSpeed, driveTime)
        ser.flushInput()

except KeyboardInterrupt:
    mot1.run(amhat.RELEASE)
    mot2.run(amhat.RELEASE)
    mot3.run(amhat.RELEASE)
    mot4.run(amhat.RELEASE)
导入序列号
导入时间
随机输入
从Adafruit_MotorHAT进口Adafruit_MotorHAT作为amhat
从Adafruit_Motor进口Adafruit_DCMotor作为adamo
#创建运动对象
motHAT=amhat(地址=0x60)
mot1=MOSTAT.getMotor(1)
mot2=发动机(2)
mot3=发动机(3)
mot4=发动机(4)
#打开串行端口
ser=serial.serial('/dev/ttyACM0',115200)
#创建变量
#传感器
distMid=0.0
distLeft=0.0
distRight=0.0
#马达倍增器
m1Mult=1.0
m2Mult=1.0
m3Mult=1.0
m4Mult=1.0
#距离阈值
distThresh=12.0
distCutOff=30.0
#速度
速度定义=200
leftSpeed=speedDef
右速度=速度定义
周转时间=1.0
defTime=0.1
行车时间=除雾时间
def驱动电机(左CHNL=speedDef,右CHNL=speedDef,持续时间=defTime):
#通过乘以确定每个电机的速度
#该通道由电机倍增器控制
m1速度=左chnl*m1Mult
m2Speed=LEFT CHNL*m2Mult
m3Speed=RIGHTCHL*m3Mult
m4Speed=RIGHTCHL*m4Mult
#设置每个电机的速度。因为速度可以是
#负数,我们取绝对值
马达1.设定速度(防抱死制动系统(内部速度)(m1速度)))
发动机2.设定速度(abs(内部(m2Speed)))
发动机3.设定速度(防抱死制动系统(内部(M3速度)))
mot4.设定速度(abs(int(M4速度)))
#运转马达。如果通道为负,则运行
#相反。否则向前跑
如果(leftChnl<0):
mot1.运行(amhat.向后)
mot2.运行(amhat.向后)
其他:
mot1.快跑(向前移动)
mot2.快跑(向前移动)
如果(rightChnl>0):
mot3.运行(amhat.向后)
mot4.运行(amhat.向后)
其他:
mot3.快跑(向前移动)
mot4.快跑(向前移动)
#等待时间
睡眠时间(持续时间)
尝试:
而1:
#读取串行端口
val=ser.readline().decode('utf=8')
打印val
#解析序列字符串
parsed=val.split(',')
已解析=[x.rstrip()表示已解析中的x]
#仅当存在新值时才指定新值
#三个或更多可用
如果(len(解析)>2):
distMid=float(解析的[0]+str(0))
distLeft=float(解析的[1]+str(0))
distRight=float(解析[2]+str(0))
#应用截止距离
如果(distMid>distcutor):
distMid=distCutOff
如果(distLeft>distCutOff):
distLeft=distCutOff
如果(distRight>DistCupt):
不信任=不信任
#重置驱动时间
行车时间=除雾时间
#如果障碍物向左移动,则通过增加速度向右转向
#左速度和右速度负速度
#如果障碍物向右移动,则通过增加速度向左转向
#右速度和运行左速度为负
如果(左0):
leftSpeed=speedDef
rightSpeed=-speedDef
行车时间=周转时间
#驱动马达
驱动电机(左速、右速、驱动时间)
flushInput爵士()
除键盘中断外:
mot1.run(amhat.RELEASE)
mot2.run(amhat.RELEASE)
mot3.run(amhat.RELEASE)
mot4.运行(amhat.发布)

也许是某个地方的缩进问题?我已经玩了几个小时的代码,还没有真正取得任何进展。如果你们中有人能帮上忙,那就太棒了

听起来,可能应该通过串口提供数据的任何东西都没有为您的程序提供足够频繁的数据。我很想抓住这个错误再试一次(也许先等一会儿)。所以不是

try:
    while 1:
        # read the serial port
        val = ser.readline().decode('utf=8')
        print val


我实现了尝试。。。除了没有睡眠的块(睡眠不断出错)和它完全停止给我错误。。。问题是什么也没发生。所以我们克服了错误,但仍然没有任何进展。你没有明确地说出序列的另一边是什么,是Arduino吗?如果是这样,您可以尝试降低两端的串行速度,看看这是否有帮助。当然,还要仔细检查一下,在串行连接的一端使用的设置是否与另一端相同,以及您的Arduino是否确实在发送数据。您能否尝试在Pi上运行串行终端,以确认您可以使用所选设置与Arduino通信?
from time import sleep
...
try:
    while 1:
        # read the serial port
        try:
            val = ser.readline().decode('utf=8')
            print val
        except SerialException:
            sleep(0.01)  # Maybe don't do this, or mess around with the interval
            continue