Python Index Error索引超出范围的列表
Python Index Error List of index out of range
我有一个python代码,它发送串行命令到设备并从它得到回复。
def control(command):
serialport= serial.Serial ("/dev/ttyAMA0", 9600, timeout=0.5)
serialport.write(command)
return serialport.readlines(1)
我有一个字典,映射一些代码传递到command
。然后我将把回复打印出来。
response = control(code_map.get(key,None))
print response, type(response)
编辑:打印出来的例子是[''x03O?0201'r'] <type 'list'>
收到回复后,我必须检查回复中的一些字符,如:
if "O?" in response[0]:
if "01" in response[0]:
if "0100" in response[0]:
status="off"
else:
status="on"
else:
status="off"
如果它只回复一次就可以了。但当有2个或更多请求时,它会引发索引错误,它不会显示状态。
这是我的javascript发布和得到回复的状态:
$('#monitor').click(function(){
setInterval(function(){
$('#status_table tr [id^="monitor_"]:checked').each(function () {
monitoring($(this).parents('tr'));
});
},15000);
});
function monitoring($row) {
$('#test').append("checked");
fbType = $row.find('td:nth-child(3)').html();
fbNum = $row.find('td:nth-child(4)').html();
eachStatus =$row.find('td:nth-child(5)').attr('id');
$('#test').append(fbType + ' '+ fbNum+' '+ eachStatus +'<br>');
$.post('/request', {inputText: fbNum,key_pressed: fbType.toString()}).done(function (reply) {
if (reply == "on") {
$('#status_table tr #status_'+eachStatus).append("on");
$('#test').append("on");
} else if (reply =="off") {
$('#status_table tr #'+eachStatus).empty().append("off");
$('#test').append("off");
}
});
}
回溯:
[03/May/2013:14:36:35] HTTP Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/_cprequest.py", line 656, in respond
response.body = self.handler()
File "/usr/local/lib/python2.7/dist-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/lib/encoding.py", line 188, in __call__
self.body = self.oldhandler(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/_cpdispatch.py", line 34, in __call__
return self.callable(*self.args, **self.kwargs)
File "remote_control_keypad_new52.py", line 677, in request
if "O?" in response[0]:
IndexError: list index out of range
[03/May/2013:14:36:35] HTTP
Request Headers:
Content-Length: 31
REFERER: http://localhost:8080/
HOST: localhost:8080
ORIGIN: http://localhost:8080
CONNECTION: Keep-Alive
Remote-Addr: 127.0.0.1
ACCEPT: */*
USER-AGENT: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-us) AppleWebKit/535+ (KHTML, like Gecko) Version/5.0 Safari/535.22+ Midori/0.4
X-REQUESTED-WITH: XMLHttpRequest
ACCEPT-LANGUAGE: en-us;q=0.750
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
ACCEPT-ENCODING: gzip, deflate
127.0.0.1 - - [03/May/2013:14:36:35] "POST /request HTTP/1.1" 500 1460 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-us) AppleWebKit/535+ (KHTML, like Gecko) Version/5.0 Safari/535.22+ Midori/0.4"
[''x03O?0101'r'x03O?0201'r'] <type 'list'>
on
127.0.0.1 - - [03/May/2013:14:36:36] "POST /request HTTP/1.1" 200 2 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-us) AppleWebKit/535+ (KHTML, like Gecko) Version/5.0 Safari/535.22+ Midori/0.4"
Button one is assigned with Response number None
Button one is assigned with Response number None
Button one is assigned with Response number None
output number selected is 01
R!None
R!None
R!None
R!None
Button one is assigned with Response number None
Button one is assigned with Response number None
Button one is assigned with Response number None
output number selected is 02
R!None
R!None
R!None
R!None
[03/May/2013:14:36:50] HTTP Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/_cprequest.py", line 656, in respond
response.body = self.handler()
File "/usr/local/lib/python2.7/dist-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/lib/encoding.py", line 188, in __call__
self.body = self.oldhandler(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/_cpdispatch.py", line 34, in __call__
return self.callable(*self.args, **self.kwargs)
File "remote_control_keypad_new52.py", line 674, in request
response = comfort_control(code_map.get(key,None))
File "remote_control_keypad_new52.py", line 639, in comfort_control
return serialport.readlines(1)
File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 456, in read
raise SerialException('device reports readiness to read but returned no data (device disconnected?)')
SerialException: device reports readiness to read but returned no data (device disconnected?)
[03/May/2013:14:36:50] HTTP
Request Headers:
Content-Length: 31
REFERER: http://localhost:8080/
HOST: localhost:8080
ORIGIN: http://localhost:8080
CONNECTION: Keep-Alive
Remote-Addr: 127.0.0.1
ACCEPT: */*
USER-AGENT: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-us) AppleWebKit/535+ (KHTML, like Gecko) Version/5.0 Safari/535.22+ Midori/0.4
X-REQUESTED-WITH: XMLHttpRequest
ACCEPT-LANGUAGE: en-us;q=0.750
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
ACCEPT-ENCODING: gzip, deflate
127.0.0.1 - - [03/May/2013:14:36:50] "POST /request HTTP/1.1" 500 1839 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-us) AppleWebKit/535+ (KHTML, like Gecko) Version/5.0 Safari/535.22+ Midori/0.4"
[''x03O?0101'r'x03O?0201'r'] <type 'list'>
on
127.0.0.1 - - [03/May/2013:14:36:51] "POST /request HTTP/1.1" 200 2 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-us) AppleWebKit/535+ (KHTML, like Gecko) Version/5.0 Safari/535.22+ Midori/0.4"
^C[03/May/2013:14:36:56] ENGINE Keyboard Interrupt: shutting down bus
[03/May/2013:14:36:56] ENGINE Bus STOPPING
也许你的回复是一个空列表
>>> response = []
>>> response[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
也许你的设备并不总是足够快来响应这个
serialport.write(command)
return serialport.readlines(1)
特别是当你有两个请求重叠时,你需要确保你的程序一次一个地向设备提供它们。
相关文章:
- 范围和列表元素在 DIV 中换行
- angularjs中列表中单个项目的独立范围
- 选中的Angular HTML列表将所选值放入应用程序范围的变量和跨度中
- 选择绑定到同一范围的按钮,单击时会相互触发.如何从angular.js中的两个选择列表按钮解除范围绑定
- 如何在 DOM 中循环浏览输入范围滑块列表并使用每个循环捕获值
- 谷歌地图或其他服务,以获取指定范围内的地方列表
- 获取单击的列表项的范围内的文本
- 无论如何,在页面加载后填充Page_Load范围内的空对象列表
- 在 JavaScript 中获取给定日期范围内所有日期的列表
- Keystone.js嵌套承诺 -> foreach -> 列表查找范围问题
- 如何使用 javascript 访问保存在请求范围内的数组或列表
- 在列表项的文本之前放置带有图像的范围
- 如何创建一个具有给定范围、四分位数和中位数的数字列表
- 在视图中使用compile in指令和ng repeat的组合会阻止正确更新范围元素列表
- 限制下拉列表的范围
- 如何将项目列表从创建许多选择标记的指令添加到控制器范围
- 修改Meteor-React待办事项列表示例,显示一个时间范围内创建的列表项
- Python Index Error索引超出范围的列表
- 在新记录列表中保存整个范围的(网格)记录
- 如何在控制台中获取特定的控制器范围列表 - angularjs