Dostal som sa k elektromeru ED 310 od firmy ZPA. Elektromer umožňuje získavať
namerané hodnoty cez rozhranie RS 485 a následne cez sériový port.
Existuje framework SmartHome.py pomocou, ktorého by malo byť možné z tohto
elektromeru získať požadované údaje, ale nepodarilo sa mi to. Preto som si
rýchlo napísal skript v Pythone, ktorý údaje prečíta. Ak máte k tejto
problematike nejaké otázky, komentáre a tak podobne, napíšte mi na môj e-mail.

Výpis programu získaný pomocou http://tohtml.com/python/:

#!/usr/bin/python
""" Reads data from electricity meter.
Copyright 2014 Marek Antozi
Author: pavuk@7gods.org (Marek Antozi)
Feel free to contact me with questions, comments, etc.
"""

import serial
import time

SERIAL_PORT = '/dev/ttyUSB0'
# Time in seconds
READ_TIMEOUT = 30.0

def open_port():
  """ Opens serial port for communication.
  Return:
    ser: created object of class serial.Serial
  """

  try:
    ser = serial.Serial(SERIAL_PORT, 300, timeout=10.0, \
      bytesize=serial.SEVENBITS, parity=serial.PARITY_EVEN, \
      stopbits=serial.STOPBITS_ONE)
  except ValueError, error:
    print 'ERROR: parameter are out of range, e.g. baud rate, data bits:', \
      str(error)
    exit(1)
  except serial.SerialException, error:
    print 'ERROR: the device on', SERIAL_PORT, \
      'can not be found or can not be configured:', str(error)
    exit(1)

  if not ser.isOpen():
    try:
      ser.open()
    except serial.SerialException, error:
      print 'ERROR: can not open', SERIAL_PORT, ':', str(error)
      exit(1)

  return ser

def main():
  """ Main function.
  """

  ser = open_port()
  if ser.isOpen():
    ser.flushInput()
    ser.flushOutput()
    ser.write('/?!\r\n')
    start_time = time.time()
    data_received = ''
    while True:
      if time.time() - start_time > READ_TIMEOUT:
        break
      out = ser.read(1)
      if out == '\x02':
        data_received = ''
        start_time = time.time()
        continue
      if out == '\x03':
        break
      data_received = data_received + out
    print data_received
    ser.close()
  else:
    print 'ERROR: port', SERIAL_PORT, 'is not open.'
    exit(1)

  return

if __name__ == "__main__":
  main()

exit(0)

Google reklama



Valid HTML 4.01!
Copyright 2014 PAVUK