从距离传感器获取特定数据(距离)

时间:2018-07-02 09:55:35

标签: python python-3.x pyserial

我正在使用Python从接近传感器读取串行数据,我只想获取距离并消除它正在生成的所有其他数据。最初输出的数据采用某种Unicode(十六进制)形式。例如:

b'\xfc\x95\x92\x82\x00\x00F\x00\x1f\x04\xef\xff\xd5\x95\x00\n'

是初始输出。 当置于for循环中以访问该行中的数据时,它会给我以下数字。

252 149 146 130 0 0 70 0 31 4 239 255 213

到目前为止我写的代码是:

import serial
ser = serial.Serial(port = "COM5", baudrate = 230400,  bytesize = 
serial.EIGHTBITS, parity= serial.PARITY_NONE, timeout = 1)
try:
    ser.isOpen()
    print("serial Port is open")
 except:
    print("error")
    exit()
if (ser.isOpen()):
    try:

         while True:

            line = ser.readline()

            print (line)

            for data in line:
                print (data)                 
    except Exception:
                print( "Keyboard Interrupt")
else:
            print("cannnot open port")

在^上方提供的输出中,无论说“ 255”是一条新行的开始,并且距离测量值始终位于9列以内,然后只要看到“ 255”,便会再次重新开始。总结一下,“ 255”每16次发生一次,然后将开始新的一行数据。我需要能够在255出现后访问第九个数据项以获取距离。

以下是一个较长的输出块,以提供更好的示例:

"b'\xfc\xc6\x92O\x00\x00F\x00\x1f\x04\xf1\xff\xd5d\x00\n'

252 198 146 79 0 0 70 0 31 4 241 255 213 100 0 10 “

b'\xfc\xc1\x92P\x00\x00I\x00 \x04\xf1\xff\xd5d\x00\n'

252 193 146 80 0 0 73 0 32 4 241 255 213 100 0 10

b'\xfc\xc1\x92Q\x00\x00I\x00\x1f\x04\xf1\xff\xd5a\x00\n'

252 193 146 81 0 0 73 0 31 4 241 255 213 97 0 10

Code and output

0 个答案:

没有答案