我在Python上开发了一个小型的Web服务器。其目的是在命令(此处为RLY71)中转换URL的参数(例如http://192.168.0.17:6066/?rly=7&status=1&port=ttyUSB0
),并将其写入连接Arduino的串行端口(此处为/ dev / ttyUSB0)。 / p>
这是WebServer代码(许多" print"用于调试目的):
__author__ = 'deadbird'
import time
import BaseHTTPServer
from urlparse import urlparse, parse_qs
import serial
from time import sleep
HOST_NAME = '192.168.0.17'
PORT_NUMBER = 6066
class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_HEAD(s):
s.send_response(200)
s.send_header("Content-type", "text/html")
s.end_headers()
def do_GET(s):
port = -1
status = -1
rly = -1
"""Respond to a GET request."""
s.send_response(200)
s.send_header("Content-type", "text/html")
s.end_headers()
s.wfile.write("<html><head><title>Piscine</title></head>")
parameters = parse_qs(urlparse(s.path).query)
if parameters.has_key('rly'):
rly = parameters['rly'][0]
if parameters.has_key('status'):
status = parameters['status'][0]
if parameters.has_key('port'):
port = parameters['port'][0]
if port != -1 and status !=-1 and rly !=-1:
#ser = serial.Serial("/dev/ttyUSB0")
#ser.write("RLY11\n")
#ser.close()
try:
print "All parameters ok, sending command\n"
print "Opening port: {0}".format(port)
ser = serial.Serial("/dev/{0}".format(port), 9600, timeout=1)
print "Opened port \'{0}\'".format(ser.name)
if ser.isOpen():
print "Port successfully opened"
print "isWritable = {0}".format(ser.writable())
else:
print "Cannot open port :("
command = "RLY{0}{1}".format(rly, status)
print "Command = \'{0}\'".format(command)
print "Writing command on serial port"
written = ser.write("RLY{0}{1}\n".format(rly, status))
print "Bytes written = {0}".format(written)
print "Reading result"
ret = ser.readline()
if len(ret) == 0:
s.wfile.write("NOTHING DONE")
else:
print ret
s.wfile.write(ret)
ser.flushOutput()
ser.flushInput()
ser.close()
except (serial.SerialException, serial.SerialTimeoutException) as e:
s.wfile.write(e)
s.wfile.write("</body></html>")
if __name__ == '__main__':
server_class = BaseHTTPServer.HTTPServer
httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)
这项工作完全适用于安装了PySerial的Mac。当我在RaspberryPi上运行相同的脚本时,没有任何反应。例如,当我使用URL http://192.168.0.17:6066/?rly=7&status=1&port=ttyUSB0
时,控制台上的结果如下:
192.168.0.16 - - [19/Apr/2014 20:59:28] "GET /?rly=7&status=1&port=ttyUSB0 HTTP/1.1" 200 -
All parameters ok, sending command
Opening port: ttyUSB0
Opened port '/dev/ttyUSB0'
Port successfully opened
isWritable = True
Command = 'RLY71'
Writing command on serial port
Bytes written = 6
Reading result
192.168.0.16 - - [19/Apr/2014 20:59:30] "GET /favicon.ico HTTP/1.1" 200 -
我尝试将端口输入用户&#34; pi&#34;,将其修改为777,没有任何变化。
我又做了一些测试。如果脚本:我写了这个剥离版本import serial
ser = serial.Serial("/dev/ttyUSB0")
ser.write("RLY11\n")
ser.close()
当我将其作为.py文件运行时,会失败。但是当我使用python交互式shell从命令行运行它(即:每行一行)时,它可以工作!我完全不知道该怎么做,有人可以帮忙吗?
答案 0 :(得分:0)
发现错误,但无法解释原因。现在我在开始时打开串口并在httpd.server_close()
之后立即关闭它。现在它完美无缺,但我无法解释原因。