From ea31636cb3bc16e97783a2ba3595314a55f90e9f Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Mon, 23 Jan 2012 14:06:31 -0500 Subject: [PATCH] Fix decimal handling for Melcor floats based on DECIMAL_1. The previous (buggy) implementation was confusing register.decimal and MelcorBackend._decimal, leading to UnboundLocalError: local variable 'decimal' referenced before assignment --- pypid/backend/melcor.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pypid/backend/melcor.py b/pypid/backend/melcor.py index 1d9d0f7..1777da8 100644 --- a/pypid/backend/melcor.py +++ b/pypid/backend/melcor.py @@ -95,16 +95,14 @@ class FloatRegister (Register): return _struct.unpack('h', _struct.pack('H', melcor))[0] / decimal def encode(self, value, **kwargs): - if self.decimal: - decimal = self.decimal - elif self.decimal_offset: + decimal = self.decimal + if self.decimal_offset: decimal *= self.decimal_offset return self._float2melcor(value, decimal) def decode(self, value, decimal=None): - if self.decimal: - decimal = self.decimal - elif self.decimal_offset: + decimal = self.decimal + if self.decimal_offset: decimal *= self.decimal_offset return self._melcor2float(value, decimal) @@ -456,8 +454,10 @@ class MelcorBackend (_Backend, _ManualMixin, _PIDMixin, _TemperatureMixin): register = self._register[register_name] if 'r' not in register.direction: raise ValueError(register_name) - if register.needs_decimal and not self._decimal: - self._decimal = self._get_decimal() + if register.needs_decimal: + if not self._decimal: + self._decimal = self._get_decimal() + register.decimal = self._decimal rc = self._client.read_holding_registers( address=register.value, count=1, unit=self._controller) assert rc.function_code < 0x80 -- 2.26.2