简体   繁体   English

HTML-> Python CGI->串行端口

[英]Html -> Python cgi-> Serial port

I'm attempting to run a python cgi script to send some data to a serial port. 我正在尝试运行python cgi脚本以将一些数据发送到串行端口。 The HTML server crashes when I try open and to set the serial port to a variable. 当我尝试打开并将串行端口设置为变量时,HTML服务器崩溃。 Here is code, it recieves a color from the html page (red,blue,green): 这是代码,它从html页面接收一种颜色(红色,蓝色,绿色):

    # Import modules for CGI handling 
import cgi, cgitb

# Import Pyserial
import serial

# Set Serial port
ser = serial.Serial('COM3', 9600) #This causes 500 - Internal Server Error

# Create instance of FieldStorage 
form = cgi.FieldStorage() 

# Get data from fields
led = form.getvalue('led_color')

if led == ("red"):
 print "<html>"
 print "<br>"
 print "red/do red script shit"
 print "</html>"

 ser.write('1')

The code does not print HTTP header. 该代码不打印HTTP标头。

Add following lines before write any body. 在写任何正文之前添加以下行。

print 'Content-type: text/html'
print

Simply importing cgitb does not enable the exception handling feature. 简单地导入cgitb不会启用异常处理功能。 Explicitly enable it. 明确启用它。

import cgitb
cgitb.enable()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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