Exchanging data with an extension
Goal: the PLC asks software on the printer, such as an extension, to do something the built-in mappings don't cover, and waits for the answer.
This uses two named internal registers: one that the PLC writes and the extension reads, and one that the extension writes and the PLC reads.
Printer setup
| Address | Mapping | Name | Data type | Writable |
|---|---|---|---|---|
| 100 | Internal | plc_request | UINT16 | On |
| 101 | Internal | aurora_reply | UINT16 | Off |
aurora_reply is read-only for the PLC, but the extension can still write it
through the Aurora API.
Protocol
A simple request and reply protocol, using a sequence number so each side can tell a new message from an old one:
- The PLC writes a new, non-zero request number to
plc_request. - The extension sees the write, does the work, and writes the same number to
aurora_reply. - The PLC polls
aurora_replyuntil it equals the request number. Then the request is done, and the PLC can send the next one.
For more data, use more registers, or a BLOCK or STRING item, and write the
data before the request number.
PLC
from common import *
import time
request = 7
check(client.write_register(100, request))
while check(client.read_holding_registers(101, count=1)).registers[0] != request:
time.sleep(0.1)
print("done")
Extension
The extension uses the Aurora API Modbus methods. waitModbusValue blocks until the PLC writes the register, so the extension does not need to poll:
waitModbusValue {"name": "plc_request"}
OK {"generation":4,"name":"plc_request","value":6}
waitModbusValue {"name": "plc_request", "generation": 4}
OK {"generation":5,"name":"plc_request","value":7}
setModbusValue {"name": "aurora_reply", "value": 7}
OK null
The first call returns at once with the current state. Each later call passes the generation from the previous reply, and returns when the PLC writes again. In Python, over the Aurora API's TCP port:
import json
import socket
conn = socket.create_connection(("localhost", 10001)).makefile("rw")
def call(method, params):
conn.write(f"{method} {json.dumps(params)}\n")
conn.flush()
status, _, result = conn.readline().partition(" ")
if status != "OK":
raise RuntimeError(result)
return json.loads(result)
state = call("waitModbusValue", {"name": "plc_request"})
while True:
state = call("waitModbusValue",
{"name": "plc_request", "generation": state["generation"]})
request = state["value"]
# ... do the work for this request ...
call("setModbusValue", {"name": "aurora_reply", "value": request})
The generation counts writes, so the extension also sees a request that repeats the previous number. Named register values are saved, so after a restart both registers still hold the last request and reply.