python 和 arduino:列表索引超出范围错误
python and arduino : list index out of range err
所以我试图从我的 arduino 中获取一些资源值和一些 id,但我不知道如何解决这个问题。
我没玩过python,这是我第一次,所以一定要简单一些...
这是我的python程序
import serial
import requests
import time
import json
ser = serial.Serial('/dev/ttyACM0', 9600, timeout = 1)
i = 0
collected = []
while (i < 15):
line = ser.readline()
if (line[:3] == '***'):
#print line
line = line.strip('*')
tokens = line[:-5].split('|')
#print tokens
list_tuple = (float(tokens[0]) , float(tokens[1]), float(tokens[2]), int(tokens[3]), int(tokens[4]), int(tokens[5]) )
#print list_tuple
collected.append(list_tuple)
i += 1
avg_temp = 0
avg_hum = 0
avg_lum = 0
id_t=0
id_h=0
id_l=0
for c in collected:
avg_temp += c[0]
avg_hum += c[1]
avg_lum += c[2]
id_t = c[3]
id_h = c[4]
id_l = c[5]
avg_temp = avg_temp/len(collected)
avg_hum = avg_hum/len(collected)
avg_lum = avg_lum/len(collected)
print "AVT: %.2f AVH: %.2f AVL: %.2f" % (avg_temp, avg_hum, avg_lum)
id_thing = id_t
id_thing_h = id_h
id_thing_l = id_l
v_type = 'temperature'
v_type_h = 'humidity'
v_type_l = 'luminosity'
# url = .....
#some http post requestes
#....
time.sleep(10)
print "Done"
ser.close()
这是错误:
Traceback (most recent call last):
File "ser.py", line 17, in <module>
list_tuple = (float(tokens[0]) , float(tokens[1]), float(tokens[2]), int(tokens[3]), int(tokens[4]), int(tokens[5]) )
IndexError: list index out of range
这是我尝试 post
的 arduino 数据
while(1){
float temp, humi;
int err;
if((err=dht11.read(humi, temp))==0)
{
Serial.println();
Serial.print("***");
Serial.print(temp);
Serial.print("|");
Serial.print(humi);
Serial.print("|");
Serial.print(ledPin);
Serial.print("|");
Serial.print(id_temp);
Serial.print("|");
Serial.print(id_hum);
Serial.print("|");
Serial.print(id_lum);
Serial.println();
}
}
IndexError: list index out of range
表示您正在阅读过去的数组边界。令牌中的元素少于 6 个。
你的台词[:-5] 会去掉很多结尾部分(我不确定你为什么要这样做)。因此,没有 6 个由 | 分隔的字段。 split() 调用给您的结果少于六个,然后您尝试访问 tokens[5](以及 tokens[4])失败,因为它们不存在。
一般来说,您还应该检查以确保列表的大小无论如何都是您想要的,否则任何格式错误的数据都会使您的程序崩溃。
tokens = line[:-5].split('|')
收件人:
tokens = line[:-2].split('|')
其他人已经尝试回答这个问题 - 但我认为基本的困惑没有解决
line[:-5]
将从一行中删除最后 5 个字符
例如,如果您的 line
是 'abcdefghijklm'
,那么 line[:-5]
将给出 'abcdefgh'
。
现在让我们看看您的 adruino 代码,具体如下行
Serial.print(id_lum)
现在,只要这些值大于 99999,就可以了,但是对于 10000 到 99999 之间的值,它会删除最后 5 位数字,但保留 |
,对于小于 1000 的值,实际上您的 id_hum
中的一些位也会被剥离。 (当然不是你想要的)。
总的来说,你没有 line[:-5]
就好了。那[-5]
跟你对6个领域感兴趣没有关系。
所以简单地改变你的线路
tokens = line[:-5].split('|')
到
tokens = line.strip().split('|')
应有尽有。额外的 strip()
是删除尾随的白色-space 如果有的话。
所以我试图从我的 arduino 中获取一些资源值和一些 id,但我不知道如何解决这个问题。 我没玩过python,这是我第一次,所以一定要简单一些...
这是我的python程序
import serial
import requests
import time
import json
ser = serial.Serial('/dev/ttyACM0', 9600, timeout = 1)
i = 0
collected = []
while (i < 15):
line = ser.readline()
if (line[:3] == '***'):
#print line
line = line.strip('*')
tokens = line[:-5].split('|')
#print tokens
list_tuple = (float(tokens[0]) , float(tokens[1]), float(tokens[2]), int(tokens[3]), int(tokens[4]), int(tokens[5]) )
#print list_tuple
collected.append(list_tuple)
i += 1
avg_temp = 0
avg_hum = 0
avg_lum = 0
id_t=0
id_h=0
id_l=0
for c in collected:
avg_temp += c[0]
avg_hum += c[1]
avg_lum += c[2]
id_t = c[3]
id_h = c[4]
id_l = c[5]
avg_temp = avg_temp/len(collected)
avg_hum = avg_hum/len(collected)
avg_lum = avg_lum/len(collected)
print "AVT: %.2f AVH: %.2f AVL: %.2f" % (avg_temp, avg_hum, avg_lum)
id_thing = id_t
id_thing_h = id_h
id_thing_l = id_l
v_type = 'temperature'
v_type_h = 'humidity'
v_type_l = 'luminosity'
# url = .....
#some http post requestes
#....
time.sleep(10)
print "Done"
ser.close()
这是错误:
Traceback (most recent call last):
File "ser.py", line 17, in <module>
list_tuple = (float(tokens[0]) , float(tokens[1]), float(tokens[2]), int(tokens[3]), int(tokens[4]), int(tokens[5]) )
IndexError: list index out of range
这是我尝试 post
的 arduino 数据while(1){
float temp, humi;
int err;
if((err=dht11.read(humi, temp))==0)
{
Serial.println();
Serial.print("***");
Serial.print(temp);
Serial.print("|");
Serial.print(humi);
Serial.print("|");
Serial.print(ledPin);
Serial.print("|");
Serial.print(id_temp);
Serial.print("|");
Serial.print(id_hum);
Serial.print("|");
Serial.print(id_lum);
Serial.println();
}
}
IndexError: list index out of range
表示您正在阅读过去的数组边界。令牌中的元素少于 6 个。
你的台词[:-5] 会去掉很多结尾部分(我不确定你为什么要这样做)。因此,没有 6 个由 | 分隔的字段。 split() 调用给您的结果少于六个,然后您尝试访问 tokens[5](以及 tokens[4])失败,因为它们不存在。
一般来说,您还应该检查以确保列表的大小无论如何都是您想要的,否则任何格式错误的数据都会使您的程序崩溃。
tokens = line[:-5].split('|')
收件人:
tokens = line[:-2].split('|')
其他人已经尝试回答这个问题 - 但我认为基本的困惑没有解决
line[:-5]
将从一行中删除最后 5 个字符
例如,如果您的 line
是 'abcdefghijklm'
,那么 line[:-5]
将给出 'abcdefgh'
。
现在让我们看看您的 adruino 代码,具体如下行
Serial.print(id_lum)
现在,只要这些值大于 99999,就可以了,但是对于 10000 到 99999 之间的值,它会删除最后 5 位数字,但保留 |
,对于小于 1000 的值,实际上您的 id_hum
中的一些位也会被剥离。 (当然不是你想要的)。
总的来说,你没有 line[:-5]
就好了。那[-5]
跟你对6个领域感兴趣没有关系。
所以简单地改变你的线路
tokens = line[:-5].split('|')
到
tokens = line.strip().split('|')
应有尽有。额外的 strip()
是删除尾随的白色-space 如果有的话。