Hello, I have a USB reading head and would like to use it to read the consumption. I already get this data but some data needs to be removed so that I can parse the consumption. The point is that all x55 should be removed. Can someone help me how this works in a Python script. This is the wake-up sequence for the heat meter and somehow it is always received by the reading head, probably through reflection.
In the log file you can see the received data. If you search for 4d there you will find the beginning. I want the sequence from 68 4d to the next 16. I would have to read this out.
Log file
I can’t edit the post so I’m replying here
Log File
import serial
import time
import binascii
import datetime
import meterbus
##logger.debug(data)
# === mbus_checksum ===============================================================================
def mbus_checksum(data, skip):
sum = 0
for i in range(0, len(data)):
if i >= skip:
sum = sum + int(data[i])
return bytearray([sum & 255])
# === check_result ================================================================================
def check_result(where, ser):
result = ser.read(1)
if result == b'\xe5':
return True
else:
if result is None:
return True
else:
logger.info(f'{where}: falsche Antwort: {binascii.hexlify(bytearray(result), " ")}')
return False
# === get_data ====================================================================================
def get_data(ser):
# Definiere die Aufwachsequenz
# 2.5: at 2400, 8N1 to send 2.2s of alternating bits
ser.write(b'\x55' * 528)
#Wartezeit(war vorher 0.350 hat hiermit funktioniert)
# time.sleep(2.0) # 2.0s sleep -> 0.8s break -> 1.2s until the buffer is empty ...
time.sleep(0.350)
# Wechsel der Parität zum auslesen der Daten
# 2.3: change parity
ser.parity=serial.PARITY_EVEN
# 2.7.1: do selection, use jokers for serial, manufacturer, ID, medium
# 17 chars, 0.08s outgoing
## selection = b'\x68\x04\x04\x68\x53\xFE\x50\x00\xA1\x16' SND UD 0
## selection = b'\x68\x0B\x0B\x68\x53\xFD\x52\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF' # aus dem Sharky Skript
##ser.write(selection)
##ser.write(mbus_checksum(selection, 4))
##ser.write(b'\x16')
# result arrives after 0.19s - 0.30s
##check_result('Selection', ser)
# 3.1: do application reset 0x50 (to read instant values)
# 10 chars, 0.05s outgoing app_reset = b'\x68\x04\x04\x68\x53\xFD\x50\x50'
##ser.write(app_reset)
##ser.write(mbus_checksum(app_reset, 4))
##ser.write(b'\x16')
# result arrives after 0.08s
##check_result('Application reset', ser)
# Daten lesen müssen 5 Bytes sein, beim Sharky wird Cheksum und Stopp Bit extra gesendet
# 3.2: do read data
# 5 chars, 0.02s
read_data = b'\x10\x5B\xFE\x59\x16'
## read_data = b'\x10\x7B\xFD' aus dem sharky Skript
ser.write(read_data)
##ser.write(mbus_checksum(read_data, 1)) #extra berehcnet und gesendet
##ser.write(b'\x16') # stopp Bit
# result arrives after 0.07s, is 0.71s long (ca. 173 bytes)
result = ser.read(800) # 173 bytes plus some reserve erhöht weil Reflexionen dabei sind, war vorher 200
if result is None:
logger.info('Keine Daten empfangen')
else:
# debug output (hex dump) of received data
# logger.info("Daten nicht umgewandelt")
#logger.info(result)
logger.info(f'diese Daten erhalten: {binascii.hexlify(bytearray(result), " ")}')
##telegram = meterbus.load(result)
##logger.info telegram.records[3].parsed_value
#weiß nicht ob notwendig steht in der Sharky WMZ Anleitung
# 2.7.2: do deselection
# 5 chars, 0.02s
##deselection = b'\x10\x40\xfd'
##ser.write(deselection)
## ser.write(mbus_checksum(deselection, 0))
##ser.write(b'\x16')
##check_result('Deselection', ser)
# return bytes received
return result
# === main ========================================================================================
logger.info('Starting up ...\n')
# 2.5: 2400, 8N1 to send 2.2s of alternating bits, long timeout due to slow response by Ultramess
ser = serial.Serial("/dev/ttyUSB1", baudrate=2400, bytesize=8, parity=serial.PARITY_NONE, stopbits=1, timeout=0.5)
while True:
logger.info('Lesen')
result = get_data(ser)
time.sleep(60.0)