Siemens S7 devices
Use an S7 connection to read values from a Siemens S7 PLC over TCP. A mapping entry reads one PLC address and publishes it on one node. The gateway reads from the PLC. It does not write to the PLC today.
Words on this page
| Word | Meaning |
|---|---|
| connection | One entry of s7:. It has a name: and a host:. |
| address | The place of a value in the PLC: DB1,INT12. |
| rack, slot | The place of the CPU in the PLC station. |
| TSAP | Transport Service Access Point. A number that identifies one end of the PLC connection. |
How to connect an S7 PLC
- Add
s7:inbrownfieldDevices:. - Add an entry. Set
name:to a short name, andhost:to the IP address of the PLC. - Set
rack:andslot:for your PLC. See the table of keys. - In
mapping:, add an entry withnode:ands7: <connection>:<address>. - Start the gateway.
brownfieldDevices:
s7:
- name: plc1 # A mapping entry uses this name: `s7: plc1:DB1,INT12`
host: 10.20.30.40
port: 102
rack: 0
slot: 1
mapping:
- node: /ua:Objects/di:DeviceSet/own:MyObject/own:MyInt32
s7: plc1:DB1,INT12
- node: /ua:Objects/di:DeviceSet/own:MyObject/own:MyFloat1
s7: plc1:DB2,REAL16
- node: /ua:Objects/di:DeviceSet/own:MyObject/own:MyFloatArray
s7: plc1:DB1,REAL24.4
- node: /ua:Objects/di:DeviceSet/own:MyObject/own:MyBoolArray
s7: plc1:DB3,X0.4.4
- node: /ua:Objects/di:DeviceSet/own:MyObject/own:MyBool
s7: plc1:DB3,X0.2
Keys of a connection
| Key | Required | Meaning |
|---|---|---|
name | Required | The name of the connection. A mapping entry uses it. |
host | Required | The IP address of the PLC. |
port | Optional | The TCP port, from 1 to 65535. The default is 102. |
rack | Optional | The rack number of the PLC. The default is 0. Use 0 for most S7-300, S7-1200 and S7-1500 PLCs. |
slot | Optional | The slot number of the CPU. The default is 1. Use 2 for an S7-300 or S7-400. Use 0 or 1 for an S7-1200 or S7-1500. |
localTSAP | Optional | The local TSAP, as a number from 0 to 65535: 0x0100. If you set it, you must also set remoteTSAP. |
remoteTSAP | Optional | The remote TSAP, as a number from 0 to 65535: 0x0200. If you set it, you must also set localTSAP. |
timeout | Optional | The time-out in ms of each request and of the connection. The value must be more than 0. Without timeout, a request times out after 1500 ms and the connection after 5000 ms. |
doNotOptimize | Optional | Set to true to turn off the batching of read requests. Use it when a PLC gives an error on a batched read. The default is false. |
debug | Optional | Set to true to get more information in the log. |
The gateway connects with rack and slot.
If you set localTSAP and remoteTSAP, the gateway connects with the two TSAPs. Then it does not use rack and slot.
The gateway refuses a connection with only one of the two TSAPs.
The gateway reads the PLC every 500 ms.
The key reference lists every key, including tags:.
Addresses
An address in a mapping entry has the form <connection>:<address>. The gateway reads these forms:
| Address | Meaning |
|---|---|
DB1,INT12 | An integer (INT) in data block 1, at byte 12. |
DB2,REAL16 | A 32-bit floating point number (REAL) in data block 2, at byte 16. |
DB1,REAL24.4 | An array of 4 REAL values, from byte 24. |
DB3,X0.2 | Bit 2 of byte 0 in data block 3. |
DB3,X0.4.4 | An array of 4 bits, from bit 4 of byte 0. |
The gateway converts the PLC value to the data type of the node. For example, an INT value goes to a Float node as a Float.
For an array, the node must have valueRank: 1.
How to scale a raw value
A PLC often stores a value as a scaled integer: a temperature in tenths of a degree.
Use scale: and offset: in the mapping entry. The gateway calculates raw * scale + offset.
- Set
s7:to the address of the raw value. - Set
scale:to the multiplier, for example 0.1. - Set
offset:if necessary. The gateway adds it after the multiplication. - Give the node a
DoubleorFloatdata type.
mapping:
- node: /ua:Objects/di:DeviceSet/own:MyObject/own:BoilerTempCelsius
s7: plc1:DB1,INT2
scale: 0.1
offset: 0
A raw value of 214 gives 21.4.
scaleis 1 andoffsetis 0 when you omit them.- The gateway scales each element of an array.
- The node must have a numeric data type. A scale on an integer node truncates the fraction.
The gateway does not start in these cases:
| Configuration | Reason |
|---|---|
scale: 0 | Each reading becomes the offset. |
scale: .nan or offset: .inf | A value that is not finite makes each reading invalid. |
scale on a Boolean or String node | The node has no numeric value. |
On a Modbus connection, scale: is a key of the point, not of the mapping entry. See Modbus.
How to calculate a value from several inputs
Use value: and inputs: when a node needs a calculation, or more than one input.
- In
inputs:, give each input a name and an address:<connection>:<address>. - In
value:, write a jsonata expression. Use$<name>for an input. - Return a value, or
{ "value": ..., "statusCode": ... }.
brownfieldDevices:
s7:
- name: plc1
host: 10.20.30.40
modbus:
- name: M1
type: TCP
address: 192.168.1.40
points:
- name: Temperature
at: holding:10
type: int16
mapping:
- node: /ua:Objects/di:DeviceSet/own:MyObject/own:MixedIndex
inputs:
raw: plc1:DB2,REAL16
temp: M1:Temperature
value: $raw.value / 10 + $temp.value
Each input has the fields value, statusCode and sourceTimestamp.
An input can come from an S7, Modbus or Ethernet/IP connection. The gateway finds the protocol from the connection name.
A connection name that two connections use is an error when the configuration loads.
- The gateway reads each address one time, even when two inputs name it.
- The gateway reads the inputs at the same time.
- If an input is not Good, the gateway does not run the expression. It publishes the first status that is not Good, in the order of
inputs:.
The gateway reads each declared input at each evaluation, also when the expression does not use it.
For a conditional read, call $s7("<connection>:<address>") inside value:.
A Modbus input with a raw address must be in the polled registers. If it is not, the gateway reports INPUT-E015 and lists the polled registers.
A Modbus input with a point name always works.
How to call $s7 in an expression
An inline call still works. The inputs: form is better, because the editor and the checks can see the inputs.
mapping:
- node: /ua:Objects/di:DeviceSet/own:MyObject/own:MyFloat2
value: |
(
$mb := $s7("plc1:DB2,REAL16");
{ "value": $mb.value / 10.0 + 3.14, "statusCode": $mb.statusCode }
)
$s7 takes one string: <connection>:<address>. It returns { value, statusCode }.
Errors
| Code | Meaning | Fix |
|---|---|---|
IMPORT-E031 | The node: of a mapping entry does not exist. | Correct the browse path and the namespace aliases. |
INPUT-E015 | A Modbus input names a raw address that no poll table covers. | Use a point name, or declare a point at that address. |
DIR-E004 | A mapping entry sets direction: to write or both. The gateway cannot write to a device today. | Remove direction:. |