From b030f980b4c4867d312450232595d092a2d4113d Mon Sep 17 00:00:00 2001 From: Kenneth Nielsen Date: Fri, 9 Aug 2019 10:33:12 +0200 Subject: [PATCH 1/2] Added Python 3 support --- PyExpLabSys/drivers/lascar.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/PyExpLabSys/drivers/lascar.py b/PyExpLabSys/drivers/lascar.py index ae12bd79..61cb45df 100644 --- a/PyExpLabSys/drivers/lascar.py +++ b/PyExpLabSys/drivers/lascar.py @@ -17,13 +17,20 @@ from __future__ import division, print_function +import sys import struct try: import hid except (ImportError, AttributeError): print("Cannot import hid, can be install with pip") -except SyntaxError: - print("This module makes use of hid, which is only available for Python2") + + +if sys.version_info.major == 3: + TEMP_START = 3 + HUMIDITY_START = 2 +else: + TEMP_START = '\x03' + HUMIDITY_START = '\x02' class ElUsbRt(object): @@ -49,10 +56,10 @@ def get_temperature_and_humidity(self): out = {} while len(out) < 2: string = self.dev.read(8) - if string.startswith('\x03'): + if string[0] == TEMP_START: frac, = struct.unpack('H', string[1:]) out['temperature'] = -200 + frac * 0.1 - elif string.startswith('\x02'): + elif string[0] == HUMIDITY_START: frac, = struct.unpack('B', string[1:]) out['humidity'] = frac * 0.5 return out @@ -61,7 +68,7 @@ def get_temperature(self): """Returns the temperature (in celcius, float)""" while True: string = self.dev.read(8) - if string.startswith('\x03'): + if string[0] == TEMP_START: frac, = struct.unpack('H', string[1:]) return -200 + frac * 0.1 @@ -69,4 +76,4 @@ def get_temperature(self): if __name__ == '__main__': DEV = ElUsbRt() while True: - print(DEV.get_temperature()) + print(DEV.get_temperature_and_humidity()) From 7cb6bc2bb1bc0c2511fd312792af8946ea31270d Mon Sep 17 00:00:00 2001 From: Kenneth Nielsen Date: Fri, 9 Aug 2019 12:38:20 +0200 Subject: [PATCH 2/2] Minor fix to keep the driver up to spec, necessary to work on Windows --- PyExpLabSys/drivers/lascar.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PyExpLabSys/drivers/lascar.py b/PyExpLabSys/drivers/lascar.py index 61cb45df..e148ca49 100644 --- a/PyExpLabSys/drivers/lascar.py +++ b/PyExpLabSys/drivers/lascar.py @@ -60,7 +60,7 @@ def get_temperature_and_humidity(self): frac, = struct.unpack('H', string[1:]) out['temperature'] = -200 + frac * 0.1 elif string[0] == HUMIDITY_START: - frac, = struct.unpack('B', string[1:]) + frac, = struct.unpack('B', string[1:2]) out['humidity'] = frac * 0.5 return out