简体   繁体   中英

Python CGI communication with serial port

I am having issues in communicating to serial port via python cgi program. I am having two buttons in html for controlling two leds(LED1,LED2).

<html>
<h1>Test Page 2</h1>
<form name="input" action="/cgi-bin/myscript-2.py" method="get">
<input type="submit" value="LED1" name="Submit">
<input type="submit" value="LED2" name="Submit1">
</form>
</html>

Now my python program receives the result from here

#!/usr/bin/python
import cgi
import cgitb;
import serial
form = cgi.FieldStorage()
cgitb.enable()
port = "/dev/ttyACM0"
ser = serial.Serial(port, 9600, timeout=0)
if "LED1" in form:
    ser.write("hello")
elif "LED2" in form:
    ser.write("hello")
else:
    print "Couldn't determine which button was pressed."

Am always getting couldnt determine button.The apache error log is like this

[Sat Nov 08 12:41:41.579738 2014] [cgid:error] [pid 1080:tid 3004140352] [client 127.0.0.1:48698] malformed header from script 'myscript-2.py': Bad header: Couldn't determine which butto, referer: http://localhost/page2.html

If you use CGI-scripts you have to print the headers first.

Have a look at https://wiki.python.org/moin/CgiScripts . You are missing

print "Content-type: text/html"
print

as the first statements of the script.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM