Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python3 support in Lascar temp and humidity driver #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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())