8.8. Python examples

8.8.1. Device interaction

8.8.1.1. Pull from a M340 PLC

from pprint import pprint
from peat import M340, datastore, initialize_peat

# Calling initialize_peat is not required, but recommended.
# It initializes logging, as well as file output folders.
initialize_peat({"DEBUG": 1, "VERBOSE": True})

dev = datastore.get("192.0.2.230")
pull_succeeded = M340.pull(dev)
print(pull_succeeded)  # bool, true if pull was successful
pprint(device.export())  # print the data

8.8.1.2. Push firmware to a ControlLogix PLC

This uploads (“pushes”) a new firmware image to a Allen-Bradley ControlLogix PLC.

from pathlib import Path
from peat import ControlLogix, datastore

fw_path = Path("clx-firmware.dmk")
dev = datastore.get("192.0.2.200")
push_succeeded = ControlLogix.push(dev, fw_path, "firmware")
print(push_succeeded)  # bool, true if push was successful

8.8.2. Parsing

8.8.2.1. Parsing a SEL project file

RDB file exported from SEL ACCelerator.

from pathlib import Path
from peat import SELRelay

input_file = Path("examples/devices/sel/sel_351s/351S5_106.rdb")
dev = SELRelay.parse(input_file)
print(dev.export())

8.8.2.2. Parsing a M340 project file

Extracts the Structured Text configuration from a M340 APX project file blob.

from pathlib import Path
from peat import M340, datastore

input_file = Path("project-file.apx")
dev = M340.parse(input_file)
print(dev.export())

8.8.2.3. Parsing a L5X file

Parse a .L5X file exported from Rockwell Studio5000.

from pprint import pprint
from pathlib import Path
from peat import L5X

input_file = Path("examples/devices/l5x/basetest.L5X")
dev = L5X.parse(input_file)
pprint(dev.export())

8.8.3. Other examples

8.8.3.1. Listing supported vendors

Utilize the module API to print the vendor name and ID (short name) for every device module included with PEAT.

import json
from peat import module_api

# This creates a dictionary with the key being the module name,
# and the value being a dictionary with the vendor id and name.
# If this Python syntax is unfamiliar, I recommend reading about
# "python dictionary comprehensions" (and other comprehensions).
identifiers = {
   name: {"id": device.vendor_id, "name": device.vendor_name}
   for name, device in module_api.modules.items()
}

# This converts the dict to JSON format, making it easier to
# read on the command line, as well as usable by other tools
print(json.dumps(identifiers, indent=4))