pylogix

repository·master·Indexed 20 days ago

https://github.com/dmroeder/pylogix

A Python communication driver for reading and writing tag values from Rockwell Automation ControlLogix, CompactLogix, and Micro8xx PLCs over Ethernet I/P. Version 1.1.5 supports Python 2 and 3 with no dependencies. It provides a PLC class for managing connections, supporting batch reads/writes, tag list retrieval, PLC time synchronization, and custom CIP messages via the Message() method.

Tokens
6.9K
Snippets
24
Records
41
Agent score
72%

What's inside pylogix

  1. Supported PLC hardware

    master

    Pylogix is a communication driver for Rockwell Automation PLCs over Ethernet I/P.

    Supported Hardware:

    • ControlLogix
    • CompactLogix
    • Micro8xx (via comm.Micro800 = True)
    • RSEmulate (may require additional configuration)

    NOT Supported:

    • PLC5
    • SLC
    • MicroLogix
    • Any other brands (pylogix is specifically tested against the models above).
  2. Handling User Defined Types (UDT) reads

    master
    Currently, pylogix does not support reading the structure of a UDT directly. To read a UDT, you must read the data as raw bytes and then manually unpack the bytes according to the UDT definition. There is currently no way to write raw data directly to a UDT.
  3. Supported PLC models and brands

    master

    pylogix is designed specifically for Allen-Bradley/Rockwell Automation controllers. It does not support other brands like Omron or Siemens, nor does it support older legacy models like PLC5, SLC, or MicroLogix.

    Supported Models:

    • CompactLogix
    • ControlLogix
    • Micro8xx
    • Emulate (local only)
    • SoftLogix (local and remote)
    • CCW Simulator
  4. Understand the Response object structure

    master

    Almost all pylogix methods return data wrapped in a Response class. This class contains three members:

    1. TagName: The name of the tag. This may be None for methods where tags aren't applicable (e.g., GetPLCTime).
    2. Value: The data returned. The type varies: it could be a single value (int, float, str), a list of values (when reading arrays or multiple tags), or an lgx_device type (for discovery/properties).
    3. Status: The success or error status of the operation.
    ret = comm.Read("MyTag")
    print(ret.TagName, ret.Value, ret.Status)
  5. Configure PLC connection parameters

    master

    When initializing a PLC instance, you may need to configure specific connection parameters depending on your hardware:

    • IPAddress: Set the IP address of the PLC.
    • ProcessorSlot: If the PLC is in a slot other than zero (common in ControlLogix), specify the slot number.
    • Micro800: If you are communicating with a Micro8xx PLC, you must set this flag to True because the communication path is different.
    # Example configuration for a ControlLogix in slot 2
    comm.ProcessorSlot = 2
    
    # Example configuration for a Micro800
    comm.Micro800 = True
  6. Optimize performance by providing data types to Read/Write

    master

    By default, pylogix maintains a KnownTags dictionary to store tag names and data types, avoiding redundant requests for type information.

    For maximum performance, especially when reading/writing large numbers of unique tags, you can provide the data type up front during a Read or Write operation. This adds the tag and type to KnownTags immediately and skips the initial type-exchange step.

    You can inspect the atomic data type values used by the library by printing comm.CIPTypes.

  7. How MicroPython file execution works (boot.py vs main.py)

    master

    MicroPython treats two files specially during the boot process:

    1. boot.py: This file loads first when the device boots. It is typically used for low-level setup, such as configuring network connections (LAN/WAN) or defining utility functions.
    2. main.py: This file runs immediately after boot.py finishes. It will continue to run until you manually interrupt it by entering the REPL.

    When using the 01_micropython_example from this repository, you should configure your Wi-Fi credentials in connect_wan() within boot.py, and configure your PLC IP address and tags within main.py.

  8. Understand the difference between Controller Tags and Program Tags

    master

    When using pylogix to interact with RSLogix5000/Studio 5000 projects, it is important to distinguish between tag scopes:

    • Controller Tags: These are in the global scope. They can be accessed by any program within the PLC controller.
    • Program Tags: These are in the local scope. They are specific to a single program (e.g., MainProgram).

    Warning: Even if a Controller Tag and a Program Tag share the same name (e.g., bool_01), they are treated as two distinct entities by the PLC. Ensure you are targeting the correct scope when specifying tag names in your Python code.

  9. Configure ProcessorSlot for Emulate and SoftLogix

    master

    When using pylogix with Rockwell software emulators, you must ensure the ProcessorSlot is set to 2 in your PLC object configuration.

    • Emulate: Works locally only. Set ProcessorSlot to 2.
    • SoftLogix: Works locally and remotely. Set ProcessorSlot to 2.
  10. Implement error logging for pylogix operations

    master

    Since pylogix does not raise exceptions for communication or tag errors, you must manually check the .Status attribute of the returned object. To implement robust logging, use the datetime library to timestamp errors and write them to a file.

    When writing to a log file, call log.flush() immediately after writing an error to ensure the entry is saved to disk in the event of an application crash. Always ensure you call log.close() at the end of your application lifecycle to release the file handle.

    import datetime
    
    now = datetime.datetime.now()
    log = open("log.txt", "a+")
    
    # Example operation
    ret = Read(plc_tag)
    
    if ret.Status == "Success":
        # Handle successful read
        tags_list.append(ret.TagName + "|" + str(ret.Value))
    else:
        # Log the error with timestamp and status
        log.write("%s Save Error: %s tag %s\n" % (now.strftime("%c"), ret.TagName, ret.Status))
        log.flush()  # Ensures log is written even if the app crashes
    
    # At the end of the application
    log.close()
  11. Enable Ethernet I/P encapsulation in FactoryTalk View Studio ME

    master

    To enable the required encapsulation setting for RSEmulate communication via FactoryTalk View Studio ME (FTVSME), follow these steps:

    1. In FTVSME, go to the very bottom of the Explorer and switch to the Communication tab.
    2. Right-click on the Ethernet driver and select Properties.
    3. Switch to the Advanced tab.
    4. Check the Encapsulation checkbox.