mcsakoff / pydigitemp

Python implementation of 1-Wire protocol.
Other
16 stars 11 forks source link

Inproper device disconnect handle #4

Closed slavkoja closed 3 years ago

slavkoja commented 7 years ago

Hi,

while further playing with it i go to another exceptions troubles. I have simple test program:

import time

from digitemp.master import UART_Adapter
from digitemp.device import DS18B20
from digitemp.exceptions import DeviceError, AdapterError

device = "/dev/tty1wUSB"
sensID = "282E12D50400005A"

print ("Connecting to '%s'..." % device)

# infinite device loop
while True:
    try:
        bus    = UART_Adapter(device)
        print ("Connected to '%s'..." % device)
        sensor = DS18B20(bus, rom=sensID)

        # infinite reading loop
        while True:
            try:
                temp = sensor.get_temperature()
                #temp = round(temp * 4) / 4
                print ("{}:{}".format(sensID, temp))
            except AdapterError:
                print ("U")

            time.sleep(10)
    except KeyboardInterrupt:
        print ("Koniec...")
        break

bus.close()

Purpose of the testing is to catch the device disconnect and eventually reconnect it again (and continue with measuring) when device is connected again. With this code i did simple tests:

Although in both cases is the error the same, your code throws different exceptions in both cases:

This requires to import two additional modules (serial and termios) to catch these situations and catch both exceptions.

There is defined DeviceError exception in your code, then IMO both these situations have to be catched by module and have to raise DeviceError (eg. with text from original exception) exception, to make code more cleaner. Or is this exception intended for something other?

mcsakoff commented 3 years ago

Fixed in 94bdfec