我正在尝试使用每次发送消息时都会更新的变量(msg_no)更新字符串(MESSAGE)
我戳了一下,看起来msg_no变量正在按照我想要的方式进行更新,但是字符串(MESSAGE)没有得到更新(每次显示为0)。我不确定为什么,谢谢您的帮助。
import xbee
import time
msg_no = 0
# TODO: replace with the 64-bit address of your target device.
TARGET_64BIT_ADDR = b'\x00\x13\xA2\x00\x41\xB7\x6C\x8C'
MESSAGE = "Hello from router (Msg No. %s)" % (msg_no)
print(" +---------------------------------------+")
print(" | XBee MicroPython Transmit Data Sample |")
print(" +---------------------------------------+\n")
print("Sending data to %s >> %s" % (''.join('{:02x}'.format(x).upper() for x in TARGET_64BIT_ADDR),
MESSAGE))
try:
msg_no += 1
xbee.transmit(TARGET_64BIT_ADDR, MESSAGE)
print("Data sent successfully")
except Exception as e:
print("Transmit failure: %s" % str(e))
time.sleep(5)
try:
msg_no += 1
MESSAGE = MESSAGE
xbee.transmit(TARGET_64BIT_ADDR, MESSAGE)
print("Data sent successfully")
except Exception as e:
print("Transmit failure: %s" % str(e))
答案 0 :(得分:1)
MESSAGE = MESSAGE
没有任何效果,您可以做的是为消息创建一个模板,然后在要创建消息时使用它:
# put this line on top of the script
MESSAGE_TEMPLATE = "Hello from router (Msg No. %s)"
#some code
# use this whenever a message with number `msg_no` needs to be created
MESSAGE = MESSAGE_TEMPLATE % msg_no