簡體   English   中英

與串行端口的Python CGI通信

[英]Python CGI communication with serial port

我在通過python cgi程序與串行端口通信時遇到問題。 我在html中有兩個按鈕,用於控制兩個led(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>

現在我的python程序從這里接收結果

#!/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."

我總是無法確定按鈕.apache錯誤日志是這樣的

[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

如果使用CGI腳本,則必須先打印標題。

看看https://wiki.python.org/moin/CgiScripts 你不見了

print "Content-type: text/html"
print

作為腳本的第一條語句。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM