Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions PyExpLabSys/drivers/lascar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -49,24 +56,24 @@ 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'):
frac, = struct.unpack('B', string[1:])
elif string[0] == HUMIDITY_START:
frac, = struct.unpack('B', string[1:2])
out['humidity'] = frac * 0.5
return out

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


if __name__ == '__main__':
DEV = ElUsbRt()
while True:
print(DEV.get_temperature())
print(DEV.get_temperature_and_humidity())