在Raspberry Pi上通过串行通信读取数字

发布于 2025-01-12 06:39:59 字数 1850 浏览 0 评论 0原文

我一直在尝试编写一个程序,让我的 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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文