在Raspberry Pi上通过串行通信读取数字
我一直在尝试编写一个程序,让我的 Raspberry Pi 通过串行通信接收数字。我正在使用 Arduino 将这些数字传达给 Raspberry Pi。 Arduino 串行打印一个前缀字符,后跟数字:Serial.println("T"); Serial.println(483);
在程序中,我能够识别前缀,但是当我尝试读取以下数字时,我只能获取第一个数字:
ser = serial.Serial('/dev/ttyACM0', 9600, timeout = 0)
while True:
if ser.in_waiting > 0:
# If there's more than 20 bytes in the buffer, clear it.
if ser.in_waiting >= 20:
ser.reset_input_buffer()
temp = ser.readline().decode('utf-8')
# Check to see what the prefix of the data is
if (temp.strip().isalpha()):
# If we got a 'T', it's a temperature update. Save temperature as number and print
if (temp.strip() == 'T'):
temp = ser.readline().decode('utf-8')
if temp.strip().isdigit():
number = (temp.strip())
print(number)
在这种情况下,当我 print(number)
它输出数字 4 而不是 483。有谁知道出了什么问题吗?
带替换的软件:
if ser.in_waiting > 0:
# If there's more than 20 bytes in the buffer, clear it.
if ser.in_waiting >= 20:
ser.reset_input_buffer()
temp = ser.readline().decode('utf-8')
# Check to see what the prefix of the data is
if (temp.strip().isalpha()):
# If we got a 'T', it's a temperature update. Update the temperature
if (temp.strip() == 'B'):
b = ser.readline()
print([int(c) for c in b])
temp = b.decode('utf-8')
if temp.strip().isdigit():
number = (temp.strip())
print ("temp = " + temp)
Serial.println("T"); 之后输出Serial.println(483);
运行了三次:
[52]
temp = 4
[]
[]
[]
[]
[52]
temp = 4
[52]
temp = 4
I have been trying to write a program that allows my Raspberry Pi to receive numbers through serial communication. I am communicating these numbers to the Raspberry Pi using an Arduino. The Arduino serial prints a prefix character followed by the number: Serial.println("T"); Serial.println(483);
In the program, I am able to identify the prefix, but when I try to read the following number I am only able to get the first digit:
ser = serial.Serial('/dev/ttyACM0', 9600, timeout = 0)
while True:
if ser.in_waiting > 0:
# If there's more than 20 bytes in the buffer, clear it.
if ser.in_waiting >= 20:
ser.reset_input_buffer()
temp = ser.readline().decode('utf-8')
# Check to see what the prefix of the data is
if (temp.strip().isalpha()):
# If we got a 'T', it's a temperature update. Save temperature as number and print
if (temp.strip() == 'T'):
temp = ser.readline().decode('utf-8')
if temp.strip().isdigit():
number = (temp.strip())
print(number)
In this case, when I print(number)
it outputs the number 4 and not 483. Does anyone know what is wrong?
Software with substitutions:
if ser.in_waiting > 0:
# If there's more than 20 bytes in the buffer, clear it.
if ser.in_waiting >= 20:
ser.reset_input_buffer()
temp = ser.readline().decode('utf-8')
# Check to see what the prefix of the data is
if (temp.strip().isalpha()):
# If we got a 'T', it's a temperature update. Update the temperature
if (temp.strip() == 'B'):
b = ser.readline()
print([int(c) for c in b])
temp = b.decode('utf-8')
if temp.strip().isdigit():
number = (temp.strip())
print ("temp = " + temp)
Output after Serial.println("T"); Serial.println(483);
ran three times:
[52]
temp = 4
[]
[]
[]
[]
[52]
temp = 4
[52]
temp = 4
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论