Created
March 8, 2023 21:01
-
-
Save jborbely/985ea5db06884fba200a6fa6944a4c82 to your computer and use it in GitHub Desktop.
Read the data registers of a Vaisala HMP110 probe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import struct | |
| # pip install pymodbus[serial] | |
| from pymodbus.client import ModbusSerialClient | |
| from pymodbus.exceptions import ModbusIOException | |
| def read_holding_registers(): | |
| def unpack(r, i): | |
| a, b = r.registers[i-1:i+1] | |
| return struct.unpack('<f', struct.pack('<HH', a, b))[0] | |
| while True: | |
| response = probe.read_holding_registers(address=0, count=28, slave=240) | |
| if not isinstance(response, ModbusIOException): | |
| return { | |
| 'relative_humidity': unpack(response, 1), | |
| 'temperature': unpack(response, 3), | |
| 'dewpoint': unpack(response, 9), | |
| 'absolute_humidity': unpack(response, 15), | |
| 'mixing_ratio': unpack(response, 17), | |
| 'wetbulb_temperature': unpack(response, 19), | |
| 'enthalpy': unpack(response, 27), | |
| } | |
| probe = ModbusSerialClient('COM10', method='rtu', baudrate=19200, stopbits=2, parity='N', timeout=1) | |
| probe.connect() | |
| print(read_holding_registers()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment