Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python & PLCs: A step by step guide

Jonas Neubert
October 02, 2020

Python & PLCs: A step by step guide

Presented at PyCon India 2020

Jonas Neubert

October 02, 2020
Tweet

More Decks by Jonas Neubert

Other Decks in Technology

Transcript

  1. Jonas Neubert
    Python & PLCs
    A STEP BY STEP GUIDE
    PYCON INDIA 2020

    View full-size slide

  2. Programmable Logic Controller
    Trivia:
    Used to be called Programmable Controller (PC).
    Then the Personal Computer got invented in the 1980s
    and the “L” got added to “PLC”.

    View full-size slide

  3. LC Inside
    PLC Inside
    PLC Inside
    PLC
    Inside
    PLC Inside
    PLC Inside

    View full-size slide

  4. PLC
    Inside
    PLC
    Inside
    PLC
    Inside
    PLC
    Inside

    View full-size slide

  5. By Dusso Janladde at at Wikimedia Commons (CC-BY-SA 3.0)
    https://commons.wikimedia.org/wiki/File:Nitro_coaster.jpg
    PLC Inside

    View full-size slide

  6. Photo by Daniel Åhs Karlsson at at Wikimedia Commons (CC-BY-SA 3.0)
    https://commons.wikimedia.org/wiki/File:OTIS_GEN_2_Comfort_Roof_Taket_(Daniel_Åhs).jpg
    PLC Inside

    View full-size slide

  7. PLC
    Sensors (Input) Actuators (Output)

    View full-size slide

  8. who am I
    Jonas Neubert
    Software Engineer and Automation Engineer
    working in biotech industry
    from Germany, now living in Oakland, California
    followup to previous talks about automation, PLCs
    (check out all my previous talks here: https://jonasneubert.com/talks)
    Current Local Time: 12:55 AM
    why this talk

    View full-size slide

  9. Table of Contents
    What is a PLC?
    Mini Tutorial
    Frequently Asked Questions
    https: // jonasneubert.com / pyconindia2020
    @jonemo / [email protected]
    Q: What was THE acronym “PLC” for again?
    A: Programmable Logic Controller
    slides:
    contact:
    intro:
    part 1:
    part 2:

    View full-size slide

  10. Part 1:
    Mini Tutorial: Blink Traffic Signal

    View full-size slide

  11. Step 1
    Figure out what type of PLC you are working with
    My PLC is model P1-540 from
    Automation Direct

    View full-size slide

  12. Step 2
    Create a network (or RS-232)
    connection to your PLC
    - My PLC has IP address 192.168.1.9
    - My laptop is configured with IP 192.168.1.8 and subnet
    mask 255.255.255.0
    - Connected via Ethernet through a Netgear switch

    View full-size slide

  13. Step 3
    Program the PLC to do something
    (use the vendor software for this)
    I have already programmed my PLC to blink the traffic light. The
    programming software was a free download from the vendor’s website.

    View full-size slide

  14. NON-PYTHON
    LANGUAGE AHEAD
    Ladder Logic
    ● Graphical programming language
    ● Very common for PLCs
    ● Used in this example
    ● You don’t have to understand it

    View full-size slide

  15. Step 4
    Assign PLC program variables
    to Modbus registers

    View full-size slide

  16. Step 4
    Assign PLC program variables
    to Modbus registers
    “master”
    client
    “slave”
    server
    Modbus protocol
    over Ethernet
    Ladder
    Logic

    View full-size slide

  17. Step 4
    Assign PLC program variables
    to Modbus registers
    https://en.wikipedia.org/wiki/Modbus#Modbus_object_types

    View full-size slide

  18. Step 4
    Assign PLC program variables
    to Modbus registers
    BlinkColor
    (1=Red, 2=Yellow, 3=Green)
    First holding register
    Modbus address: 40001
    Python index: 0
    BlinkIntervalMS
    Second holding register
    Modbus address: 40002
    Python index: 1
    https://en.wikipedia.org/wiki/Modbus#Modbus_object_types

    View full-size slide

  19. Step 5
    pip install [your-favorite-modbus-library]
    Package
    pymodbus
    modbus-tk
    pyModbusTCP
    MinimalModbus
    umodbus
    micropython-modbus
    ctmodbus
    License
    BSD
    LGPL
    MIT
    Apache 2.0
    MPL 2.0
    GPL
    GPL
    Latest
    Release
    Sep 2020
    May 2020
    Oct 2018
    Aug 2019
    Feb 2020

    Aug 2020











    TCP
    Serial
    Oldest and most popular, sparse docs
    Similar to pymodbus, sparse docs
    TCP only, simple API
    RTU & ASCII master only, detailed docs
    protocol only, Flask-like server API
    MicroPython port of modbus-tk
    security-focused REPL for pymodbus







    master






    slave
    Stars
    1.1k
    287
    104
    143
    110

    26
    This is the one I will
    used in the example

    View full-size slide

  20. Step 6
    Read/write Modbus registers via the network
    from pyModbusTCP.client import ModbusClient
    c = ModbusClient("192.168.1.9")
    c.read_holding_registers(0)
    c.write_single_register(0, 3)

    View full-size slide

  21. Step 1: Figure out type of PLC
    Step 2: Create network connection
    Step 3: Program PLC
    Step 4: Assign Modbus registers
    Step 5: pip install [your-favorite-modbus-library]
    Step 6: Read/write Modbus registers

    View full-size slide

  22. Part 2:
    Frequently Asked Questions

    View full-size slide

  23. Q: Can I program my PLC in Python?
    A: No.
    1) Not an IEC-61131 language
    2) Not exactly made for realtime execution

    View full-size slide

  24. Process Data
    Process Parameters
    Sensor Signals
    Actuator Signals
    The Two Control Loops of “IIoT”
    Field Devices PLC Logic Python Code
    “Inner Loop” “Outer Loop”
    milliseconds per iteration
    interfaces to the physical world
    timing critical & safety critical logic
    release process might be regulated
    minutes to months per iteration
    interfaces to databases and APIs
    optimization & customization
    regular software release cycle

    View full-size slide

  25. Process Data
    Process Parameters
    Sensor Signals
    Actuator Signals
    The Two Control Loops of “IIoT”
    Field Devices PLC Logic Python Code
    “Inner Loop” “Outer Loop”
    milliseconds per iteration
    interfaces to the physical world
    timing critical & safety critical logic
    release process might be regulated
    minutes to months per iteration
    interfaces to databases and APIs
    optimization & customization
    regular software release cycle

    View full-size slide

  26. Q: Is Modbus the only PLC
    communication protocol?
    A: Modbus was the first protocol developed for this
    purpose, and there are many others:
    Ethernet/IP, OPC-UA, ProfiNET, Profibus, DeviceNET,
    EtherCAT, ADS, DirectNet, BACnet, ...
    see also: “List of Automation Protocols“ on Wikipedia
    https://en.wikipedia.org/wiki/List_of_automation_protocols

    View full-size slide

  27. Q: Help! My Python script won’t
    connect to the PLC!
    Many problems encountered when working with PLCs
    are actually networking/connection problems:
    - Know the basics of networking: IP addresses, ports,
    subnet masks, etc
    - Firewalls and other security software
    - Your PLC might not be on the network at all!

    View full-size slide

  28. Q: Why not just use a
    RaspberryPi/Arduino/CircuitPython Board?
    Out of the box, these devices do not meet the requirements
    that PLCs are designed for.
    ?
    Raspberry Pi is a trademark of the Raspberry Pi Foundation

    View full-size slide

  29. Q: Can I simulate a PLC?
    - Write a Modbus slave using pymodbus
    - PLC Fiddle, (plcfiddle.com, free)
    web based playground for ladder logic
    - Free/trial version of a vendor software with simulator:
    CODESYS, Twincat, Rockwell CCW, Automation Direct DoMore
    - CODESYS for RaspberryPi (codesys.com, €50)
    Cross-platform IEC 61131-3 editor and runtime. The editor is free, the
    RaspberryPi runtime is €50.
    - OpenPLC (openplcproject.com, free)
    A IEC-61131-3 editor (written in Python) and runtime for various targets,
    including RaspberryPi.
    List of free software and training resources:
    https://www.reddit.com/r/PLC/comments/ehu2u3/read_first_how_to_learn_plcs_and_get_into_the/

    View full-size slide

  30. Q: Can you* help me with my PLC question?
    When asking for help, always include this info:
    - Brand and model of PLC
    - PLC Programming Software
    - Communication protocol
    - Python code snippet
    A sketch and/or photo of your setup really helps!
    * This advice probably works with asking advice from anyone. Of course, you are welcome
    to contact me with questions about this talk! However, for most PLC questions (and most
    Python questions) I am far from an expert. Make sure to check the documentation and
    training materials from your PLC maker first, and also check out PLC related online forums
    like reddit.com/r/PLC, mrplc.com, plctalk.net.

    View full-size slide

  31. What’s the URL for these slides again?
    https://jonasneubert.com/pyconindia2020
    Sorry, I didn’t leave time for questions.
    The best ways to contact me are
    Twitter (@jonemo) and email ([email protected])
    My last slide

    View full-size slide