Raspberry Pi、Python、XBee 到 Arduino Xbee.write() 错误
Posted
技术标签:
【中文标题】Raspberry Pi、Python、XBee 到 Arduino Xbee.write() 错误【英文标题】:Raspberry Pi, Python, XBee to Arduino Xbee.write() error 【发布时间】:2013-12-05 13:16:15 【问题描述】:我的Raspberry Pi 通过XBee 模块与我的Arduino 通话时遇到问题。
我有一个运行 websocket 的 Python 脚本,我希望通过网站控制我的 Arduino 和 Raspberry Pi 的引脚。
每当我调用控制 Raspberry Pi 的命令时,一切都很好。但是当我调用应该通过串行发送的命令时,脚本会中断并给我这个错误:
ERROR:tornado.application:Uncaught exception in /ws
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/tornado/websocket.py", line 303, in wrapper
return callback(*args, **kwargs)
File "websocket.py", line 101, in on_message
xbee.write("%s" % message)
File "/usr/local/lib/python2.7/dist-packages/serial/serialposix.py", line 491, in write
d = to_bytes(data)
File "/usr/local/lib/python2.7/dist-packages/serial/serialutil.py", line 76, in to_bytes
b.append(item) # this one handles int and str for our emulation and ints for Python 3.x
TypeError: an integer or string of size 1 is required
Connection was closed...
这是我的 Python 脚本:
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
import RPi.GPIO as GPIO
import serial
import time
xbee = serial.Serial("/dev/ttyAMA0", 9600)
#xbee.open()
time.sleep(2)
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18, GPIO.IN)
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)
GPIO.setup(25, GPIO.IN)
pin18 = GPIO.input(18)
pin23 = GPIO.input(23)
pin24 = GPIO.input(24)
pin25 = GPIO.input(25)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(25, GPIO.OUT)
class WSHandler(tornado.websocket.WebSocketHandler):
def open(self):
print "New connection was opened"
self.write_message("Connected!")
self.write_message("pin18 = %s \n pin23 = %s \n pin24 = %s \n pin25 = %s" % (pin18, pin23, pin24, pin25))
def on_message(self, message):
global pin18
global pin23
global pin24
global pin25
if (message == "pin18"):
print "%s" % message
if (pin18 == 1):
GPIO.output(18, GPIO.LOW)
pin18 = 0
else:
GPIO.output(18, GPIO.HIGH)
pin18 = 1
elif (message == "pin23"):
if (pin23 == 1):
GPIO.output(23, GPIO.LOW)
pin23 = 0
else:
GPIO.output(23, GPIO.HIGH)
pin23 = 1
elif (message == "pin24"):
if (pin24 == 1):
GPIO.output(24, GPIO.LOW)
pin24 = 0
else:
GPIO.output(24, GPIO.HIGH)
pin24 = 1
elif (message == "pin25"):
if (pin25 == 1):
GPIO.output(25, GPIO.LOW)
pin25 = 0
else:
GPIO.output(25, GPIO.HIGH)
pin25 = 1
elif (message == "alloff"):
GPIO.output(18, GPIO.HIGH)
pin18 = 1
GPIO.output(23, GPIO.HIGH)
pin23 = 1
GPIO.output(24, GPIO.HIGH)
pin24 = 1
GPIO.output(25, GPIO.HIGH)
pin25 = 1
elif (message == "allon"):
GPIO.output(18, GPIO.LOW)
pin18 = 0
GPIO.output(23, GPIO.LOW)
pin23 = 0
GPIO.output(24, GPIO.LOW)
pin24 = 0
GPIO.output(25, GPIO.LOW)
pin25 = 0
else:
self.write_message("%s\n" % message)
print "%s" % message
xbee.write("%s" % message)
def on_close(self):
print "Connection was closed..."
application = tornado.web.Application([
(r'/ws', WSHandler),
])
if __name__ == "__main__":
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(9090)
tornado.ioloop.IOLoop.instance().start()
有什么问题?
【问题讨论】:
【参考方案1】:我遇到了类似的问题,它与编码问题有关。如果我像这样附加了Unicode 字符,我会得到以下信息:
serial.write(command + u'\r\n')
一旦我删除了 u,它就可以正常工作了:
serial.write(command + '\r\n')
所以您可能想尝试以下方法:
xbee.write(message.encode('iso-8859-1'))
【讨论】:
以上是关于Raspberry Pi、Python、XBee 到 Arduino Xbee.write() 错误的主要内容,如果未能解决你的问题,请参考以下文章
XBee 在 Raspberry Pi 上使用 Java Lib [关闭]