Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/480.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Python索引错误索引列表超出范围_Javascript_Python_List_Indexing - Fatal编程技术网

Javascript Python索引错误索引列表超出范围

Javascript Python索引错误索引列表超出范围,javascript,python,list,indexing,Javascript,Python,List,Indexing,我有一个python代码,它向一个设备发送串行命令并从中得到回复 def control(command): serialport= serial.Serial ("/dev/ttyAMA0", 9600, timeout=0.5) serialport.write(command) return serialport.readlines(1) 我有一本字典,它把一些代码映射到command。然后我会把答复打印出来 response = control(code

我有一个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']

收到回复后,我必须检查回复中的一些字符,例如:

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");
                }
            });
        }
$(“#监视器”)。单击(函数(){
setInterval(函数(){
$('#status_table tr[id^=“monitor”]:checked')。每个(函数(){
监控($(this.parents('tr'));
});
},15000);
});
功能监控($row){
$('#test')。追加(“选中”);
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')。追加(fbType+''+fbNum+''+eachStatus+'
'); $.post('/request',{inputText:fbNum,按键_按下:fbType.toString()}).done(函数(回复){ 如果(回复==“打开”){ $(“#status#u table tr#status#”+eachStatus.append(“on”); $('#test')。附加(“on”); }否则如果(答复=“关闭”){ $(“#status#table tr#”+eachStatus.empty().append(“off”); $('#test')。附加(“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
[03/May/2013:14:36:35]HTTP回溯(最近一次呼叫最后一次):
文件“/usr/local/lib/python2.7/dist packages/CherryPy-3.2.2-py2.7.egg/CherryPy/_cprequest.py”,第656行,作为响应
response.body=self.handler()
文件“/usr/local/lib/python2.7/dist packages/CherryPy-3.2.2-py2.7.egg/CherryPy/lib/encoding.py”,第188行,在调用中__
self.body=self.oldhandler(*args,**kwargs)
文件“/usr/local/lib/python2.7/dist packages/CherryPy-3.2.2-py2.7.egg/CherryPy/_cpdispatch.py”,第34行,在调用中__
返回self.callable(*self.args,**self.kwargs)
请求中第677行的文件“远程控制键盘新52.py”
如果响应[0]中的“O”:
索引器:列表索引超出范围
[03/May/2013:14:36:35]HTTP
请求标头:
内容长度:31
推荐人:http://localhost:8080/
主机:本地主机:8080
来源:http://localhost:8080
连接:保持活力
远程地址:127.0.0.1
接受:*/*
用户代理:Mozilla/5.0(Macintosh;U;Intel Mac OS X;en-us)AppleWebKit/535+(KHTML,如Gecko)版本/5.0 Safari/535.22+Midori/0.4
X-request-WITH:XMLHttpRequest
接受英语:en-us;q=0.750
内容类型:application/x-www-form-urlencoded;字符集=UTF-8
接受编码:gzip,deflate
127.0.0.1--[03/May/2013:14:36:35]“发布/请求HTTP/1.1”500 1460http://localhost:8080/“Mozilla/5.0(Macintosh;U;Intel Mac OS X;en-us)AppleWebKit/535+(KHTML,类似Gecko)版本/5.0 Safari/535.22+Midori/0.4”
['\x03O?0101\r\x03O?0201\r']
在…上
127.0.0.1--[03/May/2013:14:36:36]“发布/请求HTTP/1.1”200 2http://localhost:8080/“Mozilla/5.0(Macintosh;U;Intel Mac OS X;en-us)AppleWebKit/535+(KHTML,类似Gecko)版本/5.0 Safari/535.22+Midori/0.4”
按钮1被分配了响应号None
按钮1被分配了响应号None
按钮1被分配了响应号None
选择的输出编号为01
R没有一个
R没有一个
R没有一个
R没有一个
按钮1被分配了响应号None
按钮1被分配了响应号None
按钮1被分配了响应号None
选择的输出编号为02
R没有一个
R没有一个
R没有一个
R没有一个
[03/May/2013:14:36:50]HTTP回溯(最近一次通话):
文件“/usr/local/lib/python2.7/dist packages/CherryPy-3.2.2-py2.7.egg/CherryPy/_cprequest.py”,第656行,作为响应
response.body=self.handler()
文件“/usr/local/lib/python2.7/dist packages/CherryPy-3.2.2-py2.7.egg/CherryPy/lib/encoding.py”,第188行,在调用中__
self.body=self.oldhandler(*args,**kwargs)
文件“/usr/local/lib/python2.7/dist packages/CherryPy-3.2.2-py2.7.egg/CherryPy/_cpdispatch.py”,第34行,在调用中__
返回self.callable(*self.args,**self.kwargs)
请求中第674行的文件“远程控制键盘新52.py”
响应=舒适度控制(代码\映射获取(键,无))
舒适控制中的第639行文件“远程控制键盘新52.py”
返回serialport.readlines(1)
文件“/usr/lib/python2.7/dist packages/serial/serialposix.py”,第456行,已读
raise SerialException('设备报告准备读取,但未返回任何数据(设备已断开连接?))
SerialException:设备报告准备读取,但未返回任何数据(设备已断开连接?)
[03/May/2013:14:36:50]HTTP
请求标头:
内容长度:31
推荐人:http://localhost:8080/
主机:本地主机:8080
来源:http://localhost:8080
连接:保持活力
远程地址:127.0.0.1
接受:*/*
用户代理:Mozilla/5.0(Macintosh;U;Intel Mac OS X;en-us)AppleWebKit/535+(KHTML,如Gecko)版本/5.0 Safari/535.22+Midori/0.4
X-request-WITH:XMLHttpRequest
接受英语:en-us;q=0.750
内容类型:application/x-www-form-urlencoded;字符集=UTF-8
接受编码:gzip,deflate
127.0.0.1--[03/May/2013:14:36:50]“发布/请求HTTP/1.1”500 1839http://localhost:8080/“Mozilla/5.0(Macintosh;U;Intel Mac OS X;en-us)AppleWebKit/535+(KHTML,类似Gecko)版本/5.0 Safari/535.22+Midori/0.4”
['\x03O?0101\r\x03O?0201\r']
在…上
127.0.0.1--[03/May/2013:14:36:51]“发布/请求HTTP/1.1”200 2http://localhost:8080/“Mozilla/5.0(Macintosh;U;Intel Mac OS X;en-us)AppleWebKit/535+(KHTML,类似Gecko)版本/5.0 Safari/535.22+Midori/0.4”
^C[03/May/2013:14:36:56]发动机键盘中断:关闭总线
[03/May/2013:14:36:56]
>>> 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)