|
这是一个今天看来似乎有点久远的问题,但是值得被记录和澄清一下!
那是在2022年1月份参与的一个项目中遇到的问题,大概需求是利用 SHT40-AD1B-R2 芯片,读取环境温度。其实就是通过 i2c 与这个温度传感器建立通讯,然后读取温湿度信息,对于上位机的我而言,需要解决的就是将从传感器中读回的数据换算为以摄氏度为单位的十进制数值。这个当时可能困扰了我一两天,但是当时还是被我找到方法"解决"了。
大概在2022年6月份的时候,在梳理 Python 知识点的过程中,我猛然间发现似乎不用我当时那么复杂的解决方法,但我却吭哧吭哧搞了好久。就好比不知已有"打火机",自己愚蠢且努力地搞了一套"钻木取火工具"。
现在时2024年7月份,事情过去都两年了,才忽然想起要整理下这个问题。由于当时对数据对象的认识不足以及python掌握的不够清楚,当我现在再次确认当前的解决办法时,发现了一些问题。现在就来解一解这个两年前的bug。
- """
- 当时在看到读回的数据为类似 `b'fX'`、`b'f4'`、`b'd\xac\xd6\xa5\x9f\x1e'` 这样的结果时,一时间有点摸不着头脑,
- 于是请教了客户那边的软件工程师,客户给我做了一个示例。
- 有了客户的示例和指导后,我就奔着目标开干了:
- 将遇到的\x开头的2位16进制字符记为16进制,非\x开头的16进制的字符转为 ascii 码!
- """
- # Yuan:I2C 读取温度返回的信息, 有点奇怪: b'f4' 、b'fX' 、b'f`5\x95\xA6\xE1'
- # Kim:什麼型號的溫度感測元件
- # Yuan:SHT40-AD1B-R2
- # Kim:
- b'f4' 算出來是24.866483558 °C
- 我覺得應該是正常的
- b'f4' = 0x66, 0x34
- 0x6634 = 26164
- ((26164/65535)*175) - 45 = 24.866483558
- b'f'' = 0x66, 0x27
- ' 這個的ascii code應該是0x27
- # Yuan:是用 ascii code 转换成16进制的对吗?
- # Kim:
- Yes,因為那是byte array
- 除非看到\x
- 不然就都要轉ASCII
- # Yuan:好的,\x开头的都是2位16进制, 对吗
- # Kim:Yes
复制代码 [code]#!/usr/bin/env python3"""思路:将 “\x+2位字符” 部分直接记录位 Hex 值,将非“\x+2位字符”的单个字符转换为 ASCII 码,于是,我突发奇想从utf-8中找了几个希腊字母(因为他们不在ascii表中,避免了重复),先将 “\x+2位字符” 这类已知的Hex字串替换成希腊字母,然后,再遍历替换后的字符串,遇到的非希腊字母,就将其从 ASCII 码,转换为对应的Hex形式。最终,将两类Hex字符汇总成纯Hex字符的字符串,再统一转换为十进制数值,再进行下一步温度的计算。"""import retemp_list = [ b'dW', b'd^', b'dB', b'd>', b'dP', b'dD', b'dS', b'dQ', b'dP', b'dO', b'dK', b'', b'dU', b'df', b'd_', b'dd(\xa6\x99\x95', b'd\xa4o\xa5\xc2,', b'd\xb0\xe8\xa5\xddA', b'd\xa3\xf8\xa5\xaa.', b'd\xb0\xe8\xa5\xac\x88', b'd\xb8Q\xa5\xb3\xe5', b'd\xac\xd6\xa5\x8b\x99', b'd\xaap\xa5\xa41', b'd\xae\xb4\xa5\xa7b', b'd\xa7 {} ==> Hex: {} ==> DEG: {} {}".format(_data, val_ret, data_pool, degc, unit)) print(f"{_data} ==> {val_ret} ==> Hex: {data_pool} ==> DEG: {degc} {unit}") else: result = '-- no data --' return resultif __name__ == '__main__': for i, temp in enumerate(temp_list): # print('{}: {} {}'.format(str(i).zfill(2), get_temperature(temp), temp)) get_temperature(temp)"""输出:b'dW' ==> [] ==> Hex: ['0x64', '0x57'] ==> DEG: 23.59 °Cb'd^' ==> [] ==> Hex: ['0x64', '0x5e'] ==> DEG: 23.61 °Cb'dB' ==> [] ==> Hex: ['0x64', '0x42'] ==> DEG: 23.54 °Cb'd>' ==> [] ==> Hex: ['0x64', '0x3e'] ==> DEG: 23.53 °Cb'dP' ==> [] ==> Hex: ['0x64', '0x50'] ==> DEG: 23.57 °Cb'dD' ==> [] ==> Hex: ['0x64', '0x44'] ==> DEG: 23.54 °Cb'dS' ==> [] ==> Hex: ['0x64', '0x53'] ==> DEG: 23.58 °Cb'dQ' ==> [] ==> Hex: ['0x64', '0x51'] ==> DEG: 23.58 °Cb'dP' ==> [] ==> Hex: ['0x64', '0x50'] ==> DEG: 23.57 °Cb'dO' ==> [] ==> Hex: ['0x64', '0x4f'] ==> DEG: 23.57 °Cb'dK' ==> [] ==> Hex: ['0x64', '0x4b'] ==> DEG: 23.56 °Cb'dU' ==> [] ==> Hex: ['0x64', '0x55'] ==> DEG: 23.59 °Cb'df' ==> [] ==> Hex: ['0x64', '0x66'] ==> DEG: 23.63 °Cb'd_' ==> [] ==> Hex: ['0x64', '0x5f'] ==> DEG: 23.61 °Cb'dd(\xa6\x99\x95' ==> ['a6', '99', '95'] ==> Hex: ['0x64', '0x64', '0x28', '0xa6', '0x99', '0x95'] ==> DEG: 23.63 °Cb'd\xa4o\xa5\xc2,' ==> ['a4', 'a5', 'c2'] ==> Hex: ['0x64', '0xa4', '0x6f', '0xa5', '0xc2', '0x2c'] ==> DEG: 23.8 °Cb'd\xb0\xe8\xa5\xddA' ==> ['b0', 'e8', 'a5', 'dd'] ==> Hex: ['0x64', '0xb0', '0xe8', '0xa5', '0xdd', '0x41'] ==> DEG: 23.83 °Cb'd\xa3\xf8\xa5\xaa.' ==> ['a3', 'f8', 'a5', 'aa'] ==> Hex: ['0x64', '0xa3', '0xf8', '0xa5', '0xaa', '0x2e'] ==> DEG: 23.8 °Cb'd\xb0\xe8\xa5\xac\x88' ==> ['b0', 'e8', 'a5', 'ac', '88'] ==> Hex: ['0x64', '0xb0', '0xe8', '0xa5', '0xac', '0x88'] ==> DEG: 23.83 °Cb'd\xb8Q\xa5\xb3\xe5' ==> ['b8', 'a5', 'b3', 'e5'] ==> Hex: ['0x64', '0xb8', '0x51', '0xa5', '0xb3', '0xe5'] ==> DEG: 23.85 °Cb'd\xac\xd6\xa5\x8b\x99' ==> ['ac', 'd6', 'a5', '8b', '99'] ==> Hex: ['0x64', '0xac', '0xd6', '0xa5', '0x8b', '0x99'] ==> DEG: 23.82 °Cb'd\xaap\xa5\xa41' ==> ['aa', 'a5', 'a4'] ==> Hex: ['0x64', '0xaa', '0x70', '0xa5', '0xa4', '0x31'] ==> DEG: 23.81 °Cb'd\xae\xb4\xa5\xa7b' ==> ['ae', 'b4', 'a5', 'a7'] ==> Hex: ['0x64', '0xae', '0xb4', '0xa5', '0xa7', '0x62'] ==> DEG: 23.83 °Cb'd\xa7 |
|