Raise a more useful error message if the Melcor controller is not connected.
authorW. Trevor King <wking@drexel.edu>
Mon, 26 Mar 2012 16:43:58 +0000 (12:43 -0400)
committerW. Trevor King <wking@drexel.edu>
Mon, 26 Mar 2012 16:43:58 +0000 (12:43 -0400)
pypid/backend/melcor.py

index c3924d3e1095c78e138f8458f9c532935942be4c..eaf317a96777e51f435293910365505a9dd5ae14 100644 (file)
@@ -29,6 +29,10 @@ from . import PIDMixin as _PIDMixin
 from . import TemperatureMixin as _TemperatureMixin
 
 
+class MelcorError (Exception):
+    pass
+
+
 class Register (object):
     def __init__(self, name, value, direction='rw', reference=None, help=None):
         self.name = name
@@ -460,6 +464,10 @@ class MelcorBackend (_Backend, _ManualMixin, _PIDMixin, _TemperatureMixin):
             register.decimal = self._decimal
         rc = self._client.read_holding_registers(
             address=register.value, count=1, unit=self._controller)
+        if rc is None:
+            raise MelcorError(
+                ('could not read {} from {}.  Is the controller connected?'
+                 ).format(register, self._client))
         assert rc.function_code < 0x80
         value = rc.registers[0]
         v = register.decode(value, decimal=self._decimal)