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

What is a PLC? And how do I talk Python to it?

F932a58935f19875076d7b2232a20fd3?s=47 Jonas Neubert
November 03, 2019

What is a PLC? And how do I talk Python to it?

Presented at North Bay Python 2019

F932a58935f19875076d7b2232a20fd3?s=128

Jonas Neubert

November 03, 2019
Tweet

More Decks by Jonas Neubert

Other Decks in Programming

Transcript

  1. Jonas Neubert WHAT IS A PLC? AND HOW DO I

    TALK PYTHON TO IT ? North Bay Python 2019
  2. PROGRAMMABLE LOGIC CONTROLLER

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

    PLC Inside
  4. PLC Inside

  5. Photo by Gonz DDL on Unsplash https://unsplash.com/photos/a1Lm99Kkqtg PLC Inside PLC

    Inside PLC Inside
  6. Photo by Zhang JR on Unsplash https://unsplash.com/photos/sCkqeWNGqMw PLC Inside

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

    PLC Inside
  8. PLC Inside

  9. PLC Inside

  10. Photo by Vladimir Kudinov on Unsplash https://unsplash.com/photos/OwWIfjoVkbI PLC Inside PLC

    Inside PLC Inside PLC Inside PLC Inside
  11. Usually not a PLC Inside

  12. Photo by Alex D’Alessio on Unsplash https://unsplash.com/photos/sCkqeWNGqMw PLC Inside

  13. PLC Inside

  14. What is a PLC? (and how do I program it?)

    How do I talk Python to it? (and when not to) Table of Contents https: // jonasneubert.com / nbpy2019 @jonemo Q: What was THE acronym “PLC” for again? A: Programmable Logic Controller slides: twitter:
  15. “Compact” Fixed form factor Photo of Siemens Logo by Stehfun

    at Wikimedia Commons (CC-BY-SA 3.0 Unported) https://commons.wikimedia.org/wiki/File:Siemens_Logo.jpg Photo of Beckhoff CX by Beckhoff Automation GmbH at Wikimedia Commons (CC-BY-SA-3.0-DE) https://commons.wikimedia.org/wiki/File:Embedded-PC_Beckhoff_CX1020.jpg This is what a PLC looks like* “Modular” Backplane with fixed number of slots “Expansible” Infinitely* stackable modules * when it’s new
  16. This is what a PLC looks like* * once it’s

    installed
  17. “CPU” DI 4-20mA DI 24V AI 5V DQ 24V Motion

    Control Serial Port Power Supply PLC with 6 modules Quick Intro to “Field Devices” Sensors Actuators
  18. The Input-Process-Output Cycle Step 1: Input Scan Step 2: Program

    Scan Step 3: Output Scan Read process variables from Field Instruments into PLC memory Create “Process Image In” (PII) Execute application program (this is the Programmable Logic!) Creates “Process Image Out (PIQ)” STEP 2 is the one we have control over! Read “Process Image Out” (PIQ) Set output signals
  19. PLC Programming Demo

  20. Ladder Logic RUNGs HOT rail

  21. Ladder Logic Contacts RUNGs HOT rail COILS

  22. Ladder Logic Photo of elevator logic by Tom Magliery on

    Flickr (CC-BY-NC-SA-2.0) https://www.flickr.com/photos/mag3737/306386128
  23. Source: https://news.ycombinator.com/item?id=275587 (content edited for length) Ladder Logic

  24. Ladder Logic Still the most popular language for programming PLCs.

    Common denominator across all PLC brands. Part of curriculum for most Automation Engineering degrees. A language optimized for debugging, not writing.
  25. pymodbus Demo

  26. Process Data Process Parameters Sensor Signals Actuator Signals The Two

    Control Loops of “Industrial Internet of Things” Field Devices PLC 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 a.k.a. Industry 4.0
  27. Vendor/Brand Specific Protocols Protocol Ethernet/IP S7 ADS DirectNet DF1 PCOM

    [unnamed?] Vendor/Brand Allen Bradley Siemens S7 Series Beckhoff Koyo (Autom. Direct) Allen Bradley Unitronics Fatek Python libraries pycomm python-snap7 chwiede.pyads bfabio.pyads, adshli, pyads clickplc df1 pcom fatek-fbs-lib Technology Ethernet + TCP/IP Ethernet + TCP/IP Ethernet + TCP/IP RS-232/485 RS-232/485 Ethernet + TCP/IP Ethernet + TCP/IP see also: http://blog.jonasneubert.com/2019/04/27/list-of-plc-brand-names-and-products/ and “List of PLC Manufacturers“ on Wikipedia Best Case Scenario: You’re using one of these PLC BrAnds Otherwise: go to next slide Difficulty Level 1: pip-install-let’s-see-what-happens
  28. Example: pyads import pyads plc = pyads.Connection('10.23.19.3.1.1', pyads.PORT_SPS1) plc.open() plc.read_by_name('GLOBAL.RED_ON',

    pyads.PLCTYPE_BOOL) # → False plc.write_by_name('GLOBAL.YELLOW_DURATION', 300, pyads.PLCTYPE_INT) @plc.notification(pyads.PLCTYPE_BOOL) def callback(handle, name, timestamp, value): print(f"Notification: {name} has new value {value}") plc.add_device_notification('GLOBAL.PEDESTRIAN_WAITING', callback)
  29. Example: pyads import pyads plc = pyads.Connection('10.23.19.3.1.1', pyads.PORT_SPS1) plc.open() plc.read_by_name('GLOBAL.RED_ON',

    pyads.PLCTYPE_BOOL) # → False plc.write_by_name('GLOBAL.YELLOW_DURATION', 300, pyads.PLCTYPE_INT) @plc.notification(pyads.PLCTYPE_BOOL) def callback(handle, name, timestamp, value): print(f"Notification: {name} has new value {value}") plc.add_device_notification('GLOBAL.PEDESTRIAN_WAITING', callback) Reference Variables by Name
  30. Example: pyads import pyads plc = pyads.Connection('10.23.19.3.1.1', pyads.PORT_SPS1) plc.open() plc.read_by_name('GLOBAL.RED_ON',

    pyads.PLCTYPE_BOOL) # → False plc.write_by_name('GLOBAL.YELLOW_DURATION', 300, pyads.PLCTYPE_INT) @plc.notification(pyads.PLCTYPE_BOOL) def callback(handle, name, timestamp, value): print(f"Notification: {name} has new value {value}") plc.add_device_notification('GLOBAL.PEDESTRIAN_WAITING', callback) Subscription to value changes
  31. “Open Standard” Protocols Protocol Modbus TCP Ethernet/IP (TCP) Profinet (TCP)

    OPC-UA Python libraries pymodbus, pyModbusTCP, cpppo pycomm, cpppo GH: devkid/profinet opcua, asyncua, opcua-client (GUI), opcua-webclient Technology Ethernet + TCP/IP Ethernet + TCP/IP Ethernet + TCP/IP Ethernet + TCP/IP see also: “List of Automation Protocols“ on Wikipedia Difficulty Level 2: Ethernet cables and IP addresses … and many more with no existing Python libraries. You can DIY an implementation using sockets and ctypes from the Python standard library! Our old friend modbus lives on this slide!
  32. Example: opcua-client Screenshot from project Readme: https://github.com/FreeOpcUa/opcua-client-gui/blob/c103b3f/README.md the “UA” is

    important! Previous versions of OPC are Windows-only legacy technology
  33. Example: opcua-client Screenshot from project Readme: https://github.com/FreeOpcUa/opcua-client-gui/blob/c103b3f/README.md Data and Methods

    are discoverable in a tree of nested objects
  34. “Open Standard” Protocols Protocol EtherCAT Python libraries pysoem Technology Ethernet

    Difficulty Level 3: Networking without IP addresses . No Python support: CC-Link IE, Ethernet Powerlink, PROFINET, Ethernet/IP SERCOS III. Utility libraries like rawsocketpy and dnet are handy for implementing Ethernet-but-not-TCP/IP protocols.
  35. “Open Standard” Protocols Protocol Profibus-DP CANopen SAE J1939 OPC (not

    OPC-UA) Python libraries pyprofibus canopen j1939 OpenOPC Technology RS-232/485 CAN bus CAN bus Windows OLE Difficulty Level 4: What are these weird cables? These protocols use cables and connectors that require converters or other specialized hardware in order to connect to most computers made in the 2010s. pyserial is the canonical tool for working with RS-232 and RS-485, python-can is a common starting point for working with CAN bus. Photo of RS-485 connector by Gerben Wierda on Flickr (CC-BY-SA-2.0) https://www.flickr.com/photos/45480083@N03/4227669345/
  36. ? Raspberry Pi is a trademark of the Raspberry Pi

    Foundation
  37. ? MicroPython Logo, copyright Damien P. George

  38. Jonas’ PLC Shopping Guide How much it’s going to cost,

    approximately If you want to do this: Try out ladder logic Try software the pros use Program your like a PLC Buy a cheap “real” PLC Hack some real gear Learn about the market leaders Contribute to a Python project Own a traffic signal Then take a look at that: OpenPLC, plcfiddle.com List of free software in appendix Codesys for , OpenPLC Automation Direct, Siemens Logo! Ebay1, industrial surplus sales Allen Bradley (US), Siemens (Europe) FreeOpcUA, package list in appendix County/state surplus auctions, Ebay 1 On Ebay, this is where you want to look: Business & Industrial → Automation, Motors & Drives → Control Systems & PLCs → PLC Processors free free $70 $200 $500 — — $250
  39. What’s the URL for these slides again? https://jonasneubert.com/nbpy2019 Want to

    work with me? https://www.zymergen.com/careers/ Watching this on Youtube? The best ways to contact me are Twitter (@jonemo) and email (jn@jonasneubert.com) Thank you
  40. Free PLC programming software Software packages that support multiple brands/platforms

    Codesys — editor is free download, addons and device-specific runtimes are not free Inforteam OpenPCS — same concept and pricing model as Codesys, but smaller market share logi.CAD3 — base edition is free download OpenPLC — open source Vendor/brand specific software platforms Beckhoff Twincat — trial license may be renewed indefinitely for personal use ABB — free basic version Vendors whose programming software is generally free: Automation Direct, Bedrock Automation, Phoenix Contact, Unistream. Generally, smaller brands offer cheap or free software while market leaders use software as a revenue stream. Avoid over-simplified “basic editions” that don’t resemble the full version or don’t use IEC-61131-3 languages, for example Allen Bradley’s “Connected Components Workbench”. Software with free trial versions that have reasonable limitations Siemens TIA Portal — 21 day free trial, requires registration Schneider Electric EcoStruxure Machine Expert — 42 day free trial Panasonic — limits program complexity, no time limit Keyence — only opens 50 times
  41. List of Python packages mentioned in this presentation • pycomm

    • python-snap7 • chwiede.pyads • bfabio.pyads • adshli • pyads • clickplc • df1 • pcom • fatek-fbs-lib • pymodbus • pyModbusTCP • cpppo • pymodbus • MinimalModbus • devkid/profinet (Github link, no PyPI project) • opcua (part of the FreeOpcUa project) • asyncua (part of the FreeOpcUa project) • opcua-client (part of the FreeOpcUa project) • opcua-webclient • pysoem • pyprofibus • canopen • j1939 • OpenOPC • rawsocketpy • Dnet From the standard library: • socket • ctypes
  42. Forums and Online Communities https://reddit.com/r/PLC/ — surprisingly friendly community, by

    Reddit standards https://control.com https://www.mrplc.com Books Sorry, I couldn’t find any books on the topic that seemed worth their price tag.