import time


def try_read(instrument, register, function_code=4, times=3):
    """Tries to read the 'register' of the 'instrument' up to 'times' times 
    if need be because of read errors.  If no successful tries occurred 
    an error is raised.
    """
    for i in range(times):
        try:
            val = instrument.read_register(register, functioncode=function_code)
            time.sleep(0.05)
            return val
        except:
            if i == (times-1):
                # final try, give up and raise the exception
                raise
            else:
                # delay and try again
                time.sleep(0.1)