Last active
August 29, 2015 14:08
-
-
Save Tafkas/74308fa7e544890e3592 to your computer and use it in GitHub Desktop.
Script to read SML data packages from a residential meter
This file contains 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
#!/usr/bin/env python | |
# encoding: utf-8 | |
""" | |
pysml.py | |
Created by Christian Stade-Schuldt on 2014-10-25. | |
""" | |
import sys | |
import os | |
import datetime | |
import serial | |
import time | |
import re | |
# SML regexes | |
complete_message_regex = '1b1b1b1b01010101.*1b1b1b1b.{8}' | |
meter_108_regex = '070100010800.{24}(.{8})0177' | |
meter_208_regex = '070100020800.{24}(.{8})0177' | |
meter_167_regex = '070100100700.{16}(.{8})0177' | |
def main(): | |
ser = serial.Serial('/dev/ttyUSB0', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=0 ) | |
tty_error = 0 | |
t = 10 | |
if tty_error == 0: | |
while t > 0: | |
t -=1 | |
data = ser.read(2048) | |
data = data.encode('hex') | |
if re.match(complete_message_regex, data): | |
m108 = re.search(meter_108_regex, data) | |
m208 = re.search(meter_208_regex, data) | |
m167 = re.search(meter_167_regex, data) | |
if m108 and m208 and m167: | |
m167 = int(m167.group(1),16) | |
# check if value is negative | |
if m167 > 0x7FFFFFFF: | |
m167 -= 0x100000000 | |
print int(m108.group(1),16)/10000.0 | |
print int(m208.group(1),16)/10000.0 | |
print m167/10.0 | |
t = 0 | |
time.sleep(1) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment