简体   繁体   中英

Html -> Python cgi-> Serial port

I'm attempting to run a python cgi script to send some data to a serial port. The HTML server crashes when I try open and to set the serial port to a variable. Here is code, it recieves a color from the html page (red,blue,green):

    # 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.

Add following lines before write any body.

print 'Content-type: text/html'
print

Simply importing cgitb does not enable the exception handling feature. Explicitly enable it.

import cgitb
cgitb.enable()

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