I have a python code which send serial command to a device and get reply from it.
def control(command):
serialport= serial.Serial ("/dev/ttyAMA0", 9600, timeout=0.5)
serialport.write(command)
return serialport.readlines(1)
I have a dictionary which map some codes to pass in to command
. Then i will print the reply out.
response = control(code_map.get(key,None))
print response, type(response)
EDIT: example of the print out is ['\\x03O?0201\\r'] <type 'list'>
After getting the reply, i have to check for some characters in the reply as such:
if "O?" in response[0]:
if "01" in response[0]:
if "0100" in response[0]:
status="off"
else:
status="on"
else:
status="off"
It works fine if it only reply once. But when there are 2 or more requests, it raise that index error and it won't display the status.
Here is my javascript to post and get reply for status:
$('#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");
}
});
}
Traceback:
[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
Perhaps your response is an empty list
>>> response = []
>>> response[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
Perhaps your device is not always fast enough to respond to this
serialport.write(command)
return serialport.readlines(1)
Particularly if you have 2 requests overlapping, you'll need to make sure that your program is supplying them to the device one at a time.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.