【发布时间】:2013-06-16 00:25:02
【问题描述】:
因此,我使用cherrypy 和pyserial 编写了一个Web 界面来与Arduino Uno 交互。它非常完整,我唯一缺少的,并且我一直试图弄清楚一天的事情是不断读取 Arduino 发送的数据,并自动显示一个包含 html 内消息的 div代码。我可以在控制台中显示它,但我无法返回实际的 html。其实用return是不行的,只能用print,不方便,因为我要的是html页面中的数据,而不是console。 我尝试了很多方法来做到这一点。
这是我的代码,非常简单。常量函数不断读取从 Arduino 发送的数据,并将其发送到控制台。我希望它像实时更新一样将其发送到 html 代码。我该怎么做?
# -*- coding: Utf-8 -*-
import cherrypy, arduino, time, threading
ser=arduino.Serial('COM4', 9600)
def constant():
while True:
m=''
print('running')
while True:
print('sub_running')
byte=(ser.read().encode('Utf-8'))
if byte=='*':
break
m=m+byte
time.sleep(1)
print(m)
time.sleep(1)
class website(object):
def index(self):
return '''
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script><script src="annexes/functions.js"></script>
<link rel=stylesheet type=text/css media=screen href="/annexes/design.css">
<form action="command" method="POST">
<input type="submit" name="command" value="Turn the LED on" text="hey"/>
<input type="submit" name="command" value="Turn the LED off"/>
</form>
'''
index.exposed=True
def command(self, command):
m=''
if command=='Turn the LED on':
ser.write('1')
if command=='Turn the LED off':
ser.write('0')
self.index
command.exposed=True
_thread = threading.Thread(target=constant)
_thread.setDaemon(True)
_thread.start()
cherrypy.quickstart(website(), config='config.conf')
【问题讨论】:
标签: python arduino cherrypy pyserial python-multithreading