【发布时间】:2015-03-28 18:19:31
【问题描述】:
我正在尝试制作一个 Arduino Yun 报警系统。它需要向我的 Web 服务器发出请求以更新其统计信息。它还需要监控一个按钮和一个运动传感器。
Linux 端正在运行一个将发出 Web 请求的 python 脚本。我需要让 Arduino 将其状态发送到 python 脚本。在 python 脚本中,我需要从 Arduino 端读取。我可以使用print raw_input() 做到这一点,但我希望它仅在有可用内容时读取,如果没有可用内容,我不希望它阻塞。例如:
import time
while 1:
print "test"
time.sleep(3)
print raw_input()
time.sleep(3)
如果我运行它,我希望它打印出来:
test
(6 seconds later)
test
代替
test
(Infinite wait until I type something in)
我尝试过线程,但它们有点难以使用。
【问题讨论】:
-
你可以使用套接字吗?我的意思是,使用 Python 编写一些“迷你 Web 服务器”。打开套接字并等待任何连接的脚本。
-
有很多方法可以做到这一点(套接字、telnet、文件 I/O、队列等),但我认为 raw_input() 不应该是它
-
另外,sys.stdin 是一个文件对象,您可以使用
sys.stdin.read()读取它 -
thecodingforums.com/threads/timeout-at-command-prompt.353297 可能会指出你是一个有利可图的方向。
标签: python python-2.7 arduino stdin arduino-yun