我在 raspberryPi 上使用以下脚本,因为 pi 很慢,与在我的 Mac 上使用它相比,它需要相当大的延迟。我想知道是否有人可以帮助改进任何功能以帮助加快速度。
import serial
import time
port = '/dev/ttyUSB0'
ser = serial.Serial(port , baudrate=9600, bytesize=8, parity=serial.PARITY_NONE, stopbits=1, timeout=0)
romIDlist = []
romIDrevList = []
delay = .6
def sensors():
#issue a device search (f) to get romID & call the function to get the romID
x = ser.write('\\f28')
result = romIDprocess(x)
#append the first romID founf to the romID list
romIDlist.append(result)
#search for next ROMID
while result.find('+')!= -1:
#issue a next device search (n) to get additional devices
id = ser.write('n')
#call the function to get the romID
result = romIDprocess(id)
#append the romID to the romID list
romIDlist.append(result)
sensorList = reverseList(romIDlist)
print sensorList
ser.write("r")
time.sleep(delay)
ser.close()
#pass the romIDlist to method reverseList for reversal of the romID's and return it as the sensorList
def romIDprocess(result):
time.sleep(delay)
#put the romID that was found into a list
x = list(ser.read(ser.inWaiting()))
#join the items in the list together
romID = ''.join(x[0:18])
return romID
#reverse the romid to put it in the correct order
def reverseList(romIDlist):
for i in romIDlist:
#change each romID into a list
id = list(i[2:18]) #deletes the + & -
#separate the list by two's for the octets
id = [i+j for i,j in zip(id[::2],id[1::2])]
#reverse the romID
id.reverse()
#join the items in the list together
idRev = ''.join(id[0:18])
#put the romID into a list
romIDrevList.append(idRev)
return romIDrevList
sensors()