How to speed up a 1-wire script

340 views Asked by At

I am using the following script on a raspberryPi, because the pi is slow it requires quite a large delay compared to using it on my mac. I was wondering if anyone can help improve any of the functions to help speed it up a bit.

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()
0

There are 0 answers