Package: hart-protocol
hart-protocolA sans I/O Python implementation of theHighway Adressable Remote Transducer Protocol.IntroductionThis Python package contains tooling for encoding and decoding bytestrings for communication with HART peripherals.
HART has been implemented using a variety of transport layers---Bell 202, RS485, Ethernet, etc.
In persuit of simplicity and reusability, this package does not contain any interface capabilities.
Use something likepySerialfor transport.
Read thesans I/O manifestofor more motivation regarding this design pattern.Briefly, HART is an open protocol for industrial automation supported by multiple device manufacturers.
HART has a concept of "address", so that many peripherals can share the same communication channel.
HART has limited support for multiple controllers, and generic handheld controllers exist.
HART peripherals respond to numbered commands, which can be thought of as primativeremote procedure calls.
The standard specifies a number of universal commands which should be supported by any peripheral, and there are also so-called "common" commands which many peripherals implement.
It's strongly recommended that you check the documentation of your own peripheral---implementations may be inconsistent.
In addition to universal and common commands, it's likely that your peripheral implements many device-specific commands.This package aims to have complete and accurate support for all universal and common commands.
In addition, this package has tooling for packing and unpacking generic command data for device-specific commands.
This package is intentionally simple and narrowly scoped.
There is no documentation beyond this README.
Please open an issue or PR to the GitHub repository if you find any errors or missing functionality.Sending CommandsThe following functions return bytestrings that can be fed to your transport layer.Universal Commandscommandfunction0read_unique_identifier(address)1read_primary_variable(address)2read_loop_current_and_percent(address)3read_dynamic_variables_and_loop_current(address)6write_polling_address(address, new_short_address)11read_unique_identifier_associated_with_tag(tag)12read_message(address)13read_tag_descriptor_date(address)14read_primary_variable_information(address)15read_output_information(address)16read_final_assembly_number(address)17write_message(address, message)18write_tag_descriptor_date(address, tag, descriptor, date)19write_final_assembly_number(address, number)Common-Practice Commandscommandfunction37set_primary_variable_lower_range_value(address, value)38reset_configuration_changed_flag(address)42perform_master_reset(address)48read_additional_transmitter_status(address)50read_dynamic_variable_assignments(address)59write_number_of_response_preambles(address, number)66toggle_analog_output_mode(address)67trim_analog_output_zero(address)68trim_analog_output_span(address)123select_baud_rate(address, rate)Arbitrary additional command bytestrings can also be generated as shown below.
This is a device-specific command for Brooks GF40 Mass Flow Controllers, which takes an IEE-754 floating point number as well as a unique code.importstructimporthart_protocolcode=0value=32.1data=struct.pack(">Bf",code,value)command=hart_protocol.pack_command(address=123,command_id=236,data=data)Parsing ResponsesAll responses are parsed into named tuples.
Every single response will have the following keys.Generic Responsekeyvalueaddress<int>bytecount<int>command<int>command_name<str>data<bytes>full_response<bytes>device_status<bytes>response_code<bytes>You can parse the rawdataaccording to the particulars of your peripheral.
Certain standard responses are parsed further as shown below.Response 0keyvaluecommand_name"read_unique_identifier"command0device_id<bytes>hardware_revision_level<int>manufacturer_device_type<bytes>manufacturer_id<int>number_response_preamble_charachters<int>software_revision_level<int>transmitter_specific_command_revision_level<int>universal_command_revision_level<int>Response 1keyvaluecommand_name"read_primary_variable"command1primary_variable<float>Response 11keyvaluecommand_name"read_unique_identifier"command11device_id<bytes>hardware_revision_level<int>manufacturer_device_type<bytes>manufacturer_id<int>number_response_preamble_charachters<int>software_revision_level<int>transmitter_specific_command_revision_level<int>universal_command_revision_level<int>Many other universal and common responses are also parsed..., give it a try!Integration Example>>>importhart_protocol>>>importserial>>>>>>port=serial.Serial("/dev/ttyUSB0",19200,timeout=0.1)>>>port.parity="O">>>port.stopbits=1>>>tag=hart_protocol.tools.pack_ascii("06C22300517"[-8:])>>>port.write(hart_protocol.universal.read_unique_identifier_associated_with_tag(tag))>>>>>>unpacker=hart_protocol.Unpacker(port)>>>formsginunpacker:...print(msg)...>>>MaintainersBlaise Thompson