提问人:VK6FUN 提问时间:9/5/2023 最后编辑:Peter MortensenVK6FUN 更新时间:9/10/2023 访问量:38
pyserial 无法将字符串转换为浮点数 ''
pyserial cannot convert string to float ''
问:
这是我正在处理的一些代码。这个想法是使用远程串行终端向它发送舵机角度,并让它定期报告舵机的当前位置。
#!/usr/bin/python3
import time
import serial
import io
from gpiozero.pins.pigpio import PiGPIOFactory
from gpiozero import Device, AngularServo
sport = serial.Serial('/dev/rfcomm0', timeout=6)
sio = io.TextIOWrapper(io.BufferedRWPair(sport, sport))
Device.pin_factory = PiGPIOFactory()
tservo = AngularServo(17,
min_angle= -90,
max_angle= 90,
min_pulse_width= 1/1000,
max_pulse_width= 2/1000)
while True:
if len(sio.readline()) > 0:
go_to = float(sio.readline())
tservo.angle = go_to
angle_string = str(tservo.angle) + '\n'
tuned_to = (angle_string.encode())
sport.write(tuned_to)
time.sleep(5)
它有点工作,但是当我尝试发送号码时,我收到这些错误消息:
Sep 5 12:16:28 magloop1 rfcomm[2354]: go_to = float(sio.readline())
Sep 5 12:16:28 magloop1 rfcomm[2354]: ValueError: could not convert string to float: ''
我尝试使用 TextIOWrapper,但它没有什么不同。
答: 暂无答案
评论
sio.readline()