Co2 k30传感器不能与Raspberry Pi一起使用3
问题描述:
我有以下用于检测CO2水平的代码。 以下是关于模型的一些信息。Co2 k30传感器不能与Raspberry Pi一起使用3
CO2传感器 - K30 树莓裨3
我已经做pi和K-30之间的连接按照下面文献 http://www.co2meters.com/Documentation/AppNotes/AN137-K30-sensor-raspberry-pi-uart.pdf
下面是我的Python代码
import serial
import time
ser = serial.Serial("/dev/ttyS0",baudrate =9600,timeout = .5)
print " AN-137: Raspberry Pi3 to K-30 Via UART\n"
ser.flushInput()
time.sleep(1)
for i in range(1,21):
ser.flushInput()
time.sleep(1)
ser.write("\xFE\x44\x00\x08\x02\x9F\x25")
time.sleep(1)
resp = ser.read(7)
high = ord(resp[3])
low = ord(resp[4])
co2 = (high*256) + low
print "i = ",i, " CO2 = " +str(co2)
time.sleep(.5)
我没有获得一致的输出。
我得到的东西下面
[email protected]:~/i2c $ sudo python test-co2.py
AN-137: Raspberry Pi3 to K-30 Via UART
i = 1 CO2 = 2458
i = 2 CO2 = 2457
i = 3 CO2 = 2448
Traceback (most recent call last):
File "test-co2.py", line 16, in <module>
high = ord(resp[3])
IndexError: string index out of range
[email protected]:~/i2c $ sudo python test-co2.py
AN-137: Raspberry Pi3 to K-30 Via UART
i = 1 CO2 = 2207
Traceback (most recent call last):
File "test-co2.py", line 16, in <module>
high = ord(resp[3])
IndexError: string index out of range
[email protected]:~/i2c $
感谢所有帮助?
答
high = ord(resp[3])
IndexError: string index out of range
这意味着字符串resp
的长度为0对于该特定呼叫和代码试图指向串的第三元件。这就是为什么你的索引超出范围。
如果您尝试在每次迭代中看到len(rep)
,您将在发生错误的特定迭代中得到0,但由于您在开始时有数据,这意味着您至少能够读取您的串行端口。
可能问题在于传感器电源/ gnd甚至RX/TX引脚的连接松动。
你可以分享你究竟做了什么从PI3的串行端口获取数据或尝试做:sudo chmod g + r/dev/ttyS0?
+0
这是1/2回答和1/2问题,请只回答问题,如果您需要询问另一个问题您[问]界面。 –
您应该在编制索引之前检查结果内容。显然,你所期望的结果列较少。 – Carcigenicate