python串口程序中,'b'怎么办? [重复]

问题描述 投票:0回答:1

串口程序试图从Python打印数据 让我向您展示我使用的代码。

import serial
import time

ser =serial.Serial(port="COM1",
                   baudrate=926100,
                   bytesize=serial.EIGHTBITS,
                   parity=serial.PARITY_NONE,
                   timeout=20)


ser.isOpen()                    ## open port 



print ("Sensortag's connected Succesfully!.")



while 1:

    res1 =ser.readline()

    res3=res1[13:21]          
    time.sleep(1.0)
    if res3 >=250:               
       print("Sensor has invalid value Please wait a moment.")
    elif res3 < 250 & res3>=200:
        print("The user's movements are abnormal. Please check.")
    else:
        print("The user Your movements are normal.")



print(res3)

首先,我想要的是在程序中打印数字,看是否超出一定范围。但问题就在这里。

Traceback (most recent call last):
      File "C:\Users\SEOI\AppData\Local\Programs\Python\Python37-32\ㅋㅋㅋㅋ.py",     line 37, in <module>
        if res3 >=250:               ## 3).When UART outputs a value, it outputs     the required parts
TypeError: '>=' not supported between instances of 'bytes' and 'int'

在下面的问题中,你已经确定了一个错误,因为我在程序中可以接收到的值不是数字 但是,无法知道

'b'
应该如何从文件输出的部分中删除该部分

我们使用滑动来解决下面的问题,但是已经输出了如图中的

'b'
。 我将把图片附在上面提到的
B
的打印输出部分。

enter image description here

++)刚刚查了一下,没看到B的图像方向对了,就写下来吧

b'0253.787' b'0261.909' b'0268.000' b'0263.939' b'0261.909' b'0259.878' b'0257.848' b'0255.818' b'0253.787' b'0290.333' b'0282.212' b'0276.121' b'0270.030' b'0265.969' b'0263.939' b'0261.909' b'0259.878' b'0261.909' b'0263.939' b'0261.909' b'0259.878' b'0257.848' b'0255.818' b'0253.787' b'0300.484' b'0485.242' b'0633.454'

python serial-port
1个回答
2
投票
b'... '

是python表示字节串的方式。错误是告诉你不能使用

>=
来比较字节和整数。

您需要在该行之前将字节字符串转换为整数。正如 DeepSpace 所指出的那样,有一种简单的方法可以做到这一点

integer = float(res3)
© www.soinside.com 2019 - 2024. All rights reserved.