c1218.connection

Classes

class c1218.connection.Connection(*args, **kwargs)
__init__(*args, **kwargs)

This is a C12.18 driver for serial connections. It relies on PySerial to communicate with an ANSI Type-2 Optical probe to communicate with a device (presumably a smart meter).

Parameters:
  • device (str) – A connection string to be passed to the PySerial library. If PySerial is new enough, the serial_for_url function will be used to allow the user to use a rfc2217 bridge.
  • c1218_settings (dict) – A settings dictionary to configure the C1218 parameters of ‘nbrpkts’ and ‘pktsize’ If not provided the default settings of 2 (nbrpkts) and 512 (pktsize) will be used.
  • serial_settings (dict) – A PySerial settings dictionary to be applied to the serial connection instance.
  • toggle_control (bool) – Enables or disables automatically settings the toggle bit in C12.18 frames.
  • enable_cache (bool) – Cache specific, read only tables in memory, the first time the table is read it will be stored for retreival on subsequent requests. This is enabled only for specific tables (currently only 0 and 1).
flush_table_cache()
get_table_data(tableid, octetcount=None, offset=None)

Read data from a table. If successful, all of the data from the requested table will be returned.

Parameters:
  • tableid (int) – The table number to read from (0x0000 <= tableid <= 0xffff)
  • octetcount (int) – Limit the amount of data read, only works if the meter supports this type of reading.
  • offset (int) – The offset at which to start to read the data from.
login(username=u'0000', userid=0, password=None)

Log into the connected device.

Parameters:
  • username (str) – the username to log in with (len(username) <= 10)
  • userid (int) – the userid to log in with (0x0000 <= userid <= 0xffff)
  • password (str) – password to log in with (len(password) <= 20)
Return type:

bool

logoff()

Send a logoff request.

Return type:bool
run_procedure(process_number, std_vs_mfg, params=u'')

Initiate a C1219 procedure, the request is written to table 7 and the response is read from table 8.

Parameters:
  • process_number (int) – The numeric procedure identifier (0 <= process_number <= 2047).
  • std_vs_mfg (bool) – Whether the procedure is manufacturer specified or not. True is manufacturer specified.
  • params (str) – The parameters to pass to the procedure initiation request.
set_table_cache_policy(cache_policy)
set_table_data(tableid, data, offset=None)

Write data to a table.

Parameters:
  • tableid (int) – The table number to write to (0x0000 <= tableid <= 0xffff)
  • data (str) – The data to write into the table.
  • offset (int) – The offset at which to start to write the data (0x000000 <= octetcount <= 0xffffff).
start()

Send an identity request and then a negotiation request.

stop(force=False)

Send a terminate request.

Parameters:force (bool) – ignore the remote devices response
class c1218.connection.ConnectionBase(device, c1218_settings={}, serial_settings=None, toggle_control=True, **kwargs)
__init__(device, c1218_settings={}, serial_settings=None, toggle_control=True, **kwargs)

This is a C12.18 driver for serial connections. It relies on PySerial to communicate with an ANSI Type-2 Optical probe to communicate with a device (presumably a smart meter).

Parameters:
  • device (str) – A connection string to be passed to the PySerial library. If PySerial is new enough, the serial_for_url function will be used to allow the user to use a rfc2217 bridge.
  • c1218_settings (dict) – A settings dictionary to configure the C1218 parameters of ‘nbrpkts’ and ‘pktsize’ If not provided the default settings of 2 (nbrpkts) and 512 (pktsize) will be used.
  • serial_settings (dict) – A PySerial settings dictionary to be applied to the serial connection instance.
  • toggle_control (bool) – Enables or diables automatically settings the toggle bit in C12.18 frames.
close()

Send a terminate request and then disconnect from the serial device.

read(size)

Read raw data from the serial connection. This function is not meant to be called directly.

Parameters:size (int) – The number of bytes to read from the serial connection.
recv(full_frame=False)

Receive a C1218Packet, the payload data is returned.

Parameters:full_frame (bool) – If set to True, the entire C1218 frame is returned instead of just the payload.
send(data)

This sends a raw C12.18 frame and waits checks for an ACK response. In the event that a NACK is received, this function will attempt to resend the frame up to 3 times.

Parameters:data (str, C1218Packet) – the data to be transmitted
write(data)

Write raw data to the serial connection. The CRC must already be included at the end. This function is not meant to be called directly.

Parameters:data (str) – The raw data to write to the serial connection.