我应该使用哪个库,以及如何使用?
Python XBee似乎只能在API模式下发送命令,我找不到任何使用它来发送字符串的例子.也许我误解了API模式是什么,但我在文档中找不到有效载荷......
Digi的Python Socket扩展是否已融入Python?我似乎无法获得他们声称在我的Python(2.7.3rc2)中定义的任何常量,也无法找到如何在他们的网站上获得这些扩展.看起来这可能是一种传递字符串的方法,但我该如何使用呢?
如果Xbee作为串行设备连接到计算机,您可以使用诸如的串行库pySerial.以下是我刚刚完成的项目的一些代码片段.
# Connect to Xbee
self.ser = serial.Serial(port, baud, timeout=timeout)
# Send data (a string)
self.ser.write(packet)
# Read data
self.data += self.ser.read()
Run Code Online (Sandbox Code Playgroud)
我们在透明模式下使用Xbees - 您在一端写入的每个字节在另一端都可以看到读取.不需要特殊的Xbee库.
小智 7
如果你有一个非常简单的设置,只有两个XBees,我也建议使用pySerial,但如果你有更复杂的东西,那么你最好使用一个库.
python-xbee库使用起来非常简单,但缺少任何类型的综合文档.使用它发送和接收简单消息:
from xbee import XBee
from serial import Serial
PORT = '/dev/ttyUSB0'
BAUD = 9600
ser = Serial(PORT, BAUD)
xbee = XBee(ser)
# Send the string 'Hello World' to the module with MY set to 1
xbee.tx(dest_addr='\x00\x01', data='Hello World')
# Wait for and get the response
print(xbee.wait_read_frame())
ser.close()
Run Code Online (Sandbox Code Playgroud)
您可以执行以下命令发送AT命令:
xbee.at(frame_id='A', command='MY')
reply = xbee.wait_read_frame()
print(reply)
# Getting the integer value out of reply
import struct
print(struct.unpack('>h', reply['parameter'])[0])
Run Code Online (Sandbox Code Playgroud)
您可以将frame_id设置为任何字符串,并用于标识正确的答复.
| 归档时间: |
|
| 查看次数: |
17804 次 |
| 最近记录: |