DL/T645¶
Main implementation of DL/T645 protocol and utilities
Usage:
import serial
import dlt645
ser = serial.Serial(
"/dev/ttyUSB0",
baudrate=1200,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_EVEN,
stopbits=serial.STOPBITS_ONE,
timeout=1
)
station_addr = dlt645.get_addr(ser)
# requesting active energy
frame = dlt645.Frame(station_addr)
frame.data = "00000000"
ser.write(frame.dump())
framedata = dlt645.read_frame(dlt645.iogen(ser))
# the data will be the full payload (energy valu and data identification)
print(framedata.data)
# shorthand function to directly get the active energy value
dlt645.get_active_energy(station_addr, ser)
- dlt645.iogen(flo)¶
Simple data generator for a file-like object, returns bytes one by one.
- Parameters
flo – a file-like object instance
- dlt645.read_frame(readgen)¶
Read a frame from a data generator, return a
Frameinstance.- Parameters
readgen (generator) – a generator returning data one byte at a time
- dlt645.write_frame(flo, frame, awaken=True)¶
Write a frame to byte form to be written on a data line
- Parameters
flo – a file-like object instance
frame (dlt645.Frame) – a
Frameinstanceawaken (bool) – whether to prefix the message with wake up bytes
- class dlt645.Frame(addr=None, control=None)¶
DL/T645 frame representation with mechanisms to load/dump a frame from/into a data byte string.
- Parameters
addr (str) – station address
control (dict) – a constrol code representation
- compat¶
Protocol compatibitilty
- frame¶
Raw frame data
- data¶
data portion of a frame
- addr¶
Station address
- control¶
Structure representing the control code portion of a frame in a more human readable way
- load(framedata)¶
Load a payload into a frame.
- Parameters
framedata (bytearray) – a byte-like object holding a payload
- dump()¶
Dump a frame as a byte-like object.
- property checksum¶
Frame checksum
- is_valid(cs=None)¶
Checksum validation
- Parameters
cs – checksum byte
- dlt645.checksum(data)¶
Return the checksum of a byte-like object
- Parameters
data (bytearray) – a byte-like object containing data to use for the checksum
- dlt645.bytetostr(bdata)¶
Convert a byte-like object to a string
- Parameters
bdata (bytearray) – a byte-like payload
- dlt645.load_addr(data)¶
Read an address from a byte-like object, return a byte-like object representing the address.
- Parameters
data (bytearray) – a byte-like payload
- dlt645.dump_addr(addr)¶
Dump an address to a byte-like object.
- Parameters
addr (str) – a station address
- dlt645.load_ctrl(data)¶
Read control code information, return a dict structure representing the different control code parts.
- Parameters
data (bytearray) – a byte-like payload
- dlt645.dump_ctrl(control)¶
Dump a control code representation to a byte-like object.
- Parameters
control (dict) – a control code representation
- dlt645.load_data(data)¶
Read data information, return a byte-like structure representing the data.
- Parameters
data (bytearray) – a byte-like payload
- dlt645.dump_data(data)¶
Dump a data payload to a byte-like object.
- Parameters
data (str) – a data payload
- dlt645.get_addr(flo, r_flo=None)¶
Utility function to read a station’s address, returns the address.
- Parameters
flo – a file-like object instance for write
r_flo – a file-like object instance for read
- dlt645.get_active_energy(addr, flo, r_flo=None)¶
Utility function to directly read active energy field, returns the energy value in kWh.
A file-like object is required for the communication, if ‘r_flo’ is
Nonethen ‘flo’ will be used for both read and write. This is useful when using asocketserver.StreamRequestHandlerthat provides different file-like objects for read and write.- Parameters
addr (str) – a station address
flo – a file-like object instance for write
r_flo – a file-like object instance for read