我一直在尝试找到以下函数的计时,该函数用于查找连接在串行端口上的ZigBee
模块的地址。但是当我试图用timeit.Timer()
方法来寻找时间的时候。它没有响应执行所需的时间。你知道吗
from xbee import ZigBee
from timeit import TImer
import serial
def ByteToHex(bytestring):
return ''.join(["%02X" % ord(x) for x in bytestring]).strip()
def self_add_tx():
s = serial.Serial('COM12', 9600)
xb = ZigBee(s)
xb.send('at',
command='SH')
self_frame_h = xb.wait_read_frame()
self_addh = self_frame_h['parameter']
xb.send('at',
command='SL')
self_frame_l = xb.wait_read_frame()
s.close()
self_addl = self_frame_l['parameter']
self_add = self_addh + self_addl
return ByteToHex(self_add)
print Timer(self_add_tx()).timeit()
当我执行程序时,它会给我以下错误:
Traceback (most recent call last):
File "I:/HUB/Python_learning/gateway_url_pi/gateway_url_pi_865_mhz/test3.py", line 24, in <module>
print Timer(self_add_tx()).timeit()
File "C:\Python27\lib\timeit.py", line 129, in __init__
compile(setup + '\n' + stmt, dummy_src_name, "exec")
File "<timeit-src>", line 2
0013A20040EA6DEC
^
SyntaxError: invalid syntax
我无法理解这个错误。有人知道这个问题吗?我的代码没有这个Timer.timeit()
就可以完美地工作。你知道吗
您需要将函数作为字符串传递给
Timer()
。在当前语法中,它执行self_add_tx()
,并将结果传递给Timer()
执行。你知道吗请尝试以下语法:
相关问题 更多 >
编程相关推荐