Skip to main content

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​

WordMeaning
connectionOne entry of s7:. It has a name: and a host:.
addressThe place of a value in the PLC: DB1,INT12.
rack, slotThe place of the CPU in the PLC station.
TSAPTransport Service Access Point. A number that identifies one end of the PLC connection.

How to connect an S7 PLC​

  1. Add s7: in brownfieldDevices:.
  2. Add an entry. Set name: to a short name, and host: to the IP address of the PLC.
  3. Set rack: and slot: for your PLC. See the table of keys.
  4. In mapping:, add an entry with node: and s7: <connection>:<address>.
  5. 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​

KeyRequiredMeaning
nameRequiredThe name of the connection. A mapping entry uses it.
hostRequiredThe IP address of the PLC.
portOptionalThe TCP port, from 1 to 65535. The default is 102.
rackOptionalThe rack number of the PLC. The default is 0. Use 0 for most S7-300, S7-1200 and S7-1500 PLCs.
slotOptionalThe 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.
localTSAPOptionalThe local TSAP, as a number from 0 to 65535: 0x0100. If you set it, you must also set remoteTSAP.
remoteTSAPOptionalThe remote TSAP, as a number from 0 to 65535: 0x0200. If you set it, you must also set localTSAP.
timeoutOptionalThe 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.
doNotOptimizeOptionalSet 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.
debugOptionalSet 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:

AddressMeaning
DB1,INT12An integer (INT) in data block 1, at byte 12.
DB2,REAL16A 32-bit floating point number (REAL) in data block 2, at byte 16.
DB1,REAL24.4An array of 4 REAL values, from byte 24.
DB3,X0.2Bit 2 of byte 0 in data block 3.
DB3,X0.4.4An 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.

  1. Set s7: to the address of the raw value.
  2. Set scale: to the multiplier, for example 0.1.
  3. Set offset: if necessary. The gateway adds it after the multiplication.
  4. Give the node a Double or Float data 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.

  • scale is 1 and offset is 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:

ConfigurationReason
scale: 0Each reading becomes the offset.
scale: .nan or offset: .infA value that is not finite makes each reading invalid.
scale on a Boolean or String nodeThe node has no numeric value.
note

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.

  1. In inputs:, give each input a name and an address: <connection>:<address>.
  2. In value:, write a jsonata expression. Use $<name> for an input.
  3. 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:.
warning

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​

CodeMeaningFix
IMPORT-E031The node: of a mapping entry does not exist.Correct the browse path and the namespace aliases.
INPUT-E015A Modbus input names a raw address that no poll table covers.Use a point name, or declare a point at that address.
DIR-E004A mapping entry sets direction: to write or both. The gateway cannot write to a device today.Remove direction:.