one program a PLC?) How do I talk Python to it? But what about RaspberryPi? https: // jonasneubert.com / pycon2019 @jonemo Q: What was THE acronym “PLC” for again? A: Programmable Logic Controller slides: twitter:
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
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
diagram, graphical - ST: Structured text, textual - IL (deprecated):Instruction list, textual - SFC: Sequential function chart , has elements to organize programs for sequential and parallel control processing, graphical. - C/C++ - Matlab/Simulink - Custom graphical wiring diagram tools IEC-61131-3
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
[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 ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ RS-232/485 Ethernet TCP/IP CAN bus see also: my spreadsheet of PLC brands 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
III. Utility libraries like rawsocketpy and dnet are handy for non-TCP/IP level networking. “Open Standard” Protocols Modbus TCP Modbus ASCII/RTU pymodbus, pyModbusTCP, cpppo pymodbus, MinimalModbus ✓ ✓ ✓ RS-232/485 Ethernet TCP/IP CAN bus Protocol Python Packages Ethernet/IP (TCP) Profinet (TCP) OPC-UA pycomm, cpppo GH: devkid/profinet opcua, asyncua, opcua-client (GUI), opcua-webclient ✓ ✓ ✓ ✓ ✓ ✓ EtherCAT pysoem ✓ ✓ Profibus-DP CANopen SAE J1939 OPC Windows OLE ✓ ✓ pyprofibus canopen j1939 OpenOPC Many more with no Python support. Standard library sockets and ctypes are your friends! Modbus is universal 40 years old this year! see also: “List of Automation Protocols“ on Wikipedia Ethernet based “Fieldbus” standards Many more with no Python support. Many require custom hardware or cables. Other TCP/IP based standards “Office-to-Machine networking”
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
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
- 11 packages first published within the last 12 months - 24 packages with new releases within the past 12 months Many Python 3-only packages, few Python 2-only packages Most activity in Europe* * based on supported brands and package maintainer website information Overlap with the much more active home automation community e.g. homeassistant has support for two PLC brands built-in
from a . Hardware Works in crazy environments EM interference protection ESD protected, isolated I/O 24VDC power, DIN rail mount Expected life measured in decades Supply chain guarantees Safety certifications Software (c.f. IEC-61131-3) Realtime guarantees PID control and Motion Control Zero-downtime code changes Industry-standard programming languages Support for common fieldbus protocols
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 - PiLC (bues.ch/cms/automation/pilc.html) Runs the awlsim PLC simulation software (an open source Python package!) on RaspberryPi. awlsim implements a subset of the languages and features of Siemens’ STEP 7.
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) FreeOpcIUa, 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
work with me? https://www.zymergen.com/careers/ Watching this on Youtube? The best ways to contact me are Twitter (@jonemo) and email ([email protected]) Open Space for questions, errata, and playing with PLCs and traffic signals Friday (today!) 3pm, Room 21 #PyConOpenSpace http://pycon.us/os My last slide
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
PLC PORT = 50505 # Arbitrary port I used in Ladder Logic with socket.socket() as s: s.connect((HOST, PORT)) s.sendall(b'REQ') print(s.recv(1024)) # → ACK s.sendall(b'DISCO') print(s.recv(1024)) # → ACK