Brownfield Siemens S7 Devices
Siemens S7 binding
The Siemens S7 binding allows you to connect to Siemens S7 PLCs (like S7-1200, S7-1500) using the TCP/IP protocol. This binding supports reading and data from/to the PLC. For more advanced use case, we recommend using the OPC UA binding, which provides a more comprehensive interface to the PLC. This binding is useful for legacy system integration or when the OPC UA server is not available on the PLC.
Configuration
brownfieldDevices:
s7:
- name: S7-1200
type: TCP
address: 10.20.30.40
port: 102
Mapping S7 data to your OPC UA Variable
You can use the mapping section in the configuration file to map S7 data to OPC UA Variables.
mapping:
- node: /di:DeviceSet/own:MyObject/own:MyInt32
s7: plc1:DB1,INT12
- node: /di:DeviceSet/own:MyObject/own:MyFloat1
s7: plc1:DB2,REAL16
- node: /di:DeviceSet/own:MyObject/own:MyFloat2
value: |
(
$mb := $s7("plc1:DB2,REAL16");
{
"value": $mb.value / 10.0 + 3.14,
"statusCode": $mb.statusCode
};
)
- node: /di:DeviceSet/own:MyObject/own:MyFloatArray
s7: plc1:DB1,REAL24.4
- node: /di:DeviceSet/own:MyObject/own:MyBoolArray
s7: plc1:DB3,X0.4.4
- node: /di:DeviceSet/own:MyObject/own:MyBool
s7: plc1:DB3,X0.2
S7 Data Types
The S7 binding supports various data types used in Siemens S7 PLCs. The following table lists the S7 data types and their corresponding OPC UA types:
| S7 Type | OPC UA Type | Description |
|---|---|---|
| INT | Int32 | 16-bit signed integer |
| DINT | Int32 | 32-bit signed integer |
| REAL | Float | 32-bit floating point number |
| LREAL | Double | 64-bit floating point number |
| BOOL | Boolean | 1-bit boolean value |
| BYTE | Byte | 8-bit unsigned integer |
| WORD | UInt16 | 16-bit unsigned integer |
| DWORD | UInt32 | 32-bit unsigned integer |
| STRING | String | String data type (up to 255 characters) |
| TIME | Duration | Time duration in milliseconds |
| DATE | Date | Date value |
| DATE_AND_TIME | DateTime | Date and time value |
automatic typ coercion
- We recommend using the correct S7 data type in the mapping to ensure that the data is correctly interpreted by the binding.
- However, the S7 binding automatically converts the S7 data types to the corresponding OPC UA data types when reading or writing data. For example, if you read an
INTfrom the PLC for a UAVariable taht contains aFloatdata type, the binding will convert theINTvalue to aFloatvalue before storing it in the OPC UA Variable.
S7 Addressing
The S7 binding uses the following addressing scheme:
DB<number>: Data block, e.g.,DB1,DB2INT<number>: Integer data type, e.g.,INT12REAL<number>: Real data type, e.g.,REAL16X<number>.<byte>.<bit>: Bit addressing, e.g.,X0.4.4(byte 0, bit 4)M<number>: Memory area, e.g.,M0.0(memory byte 0, bit 0)I<number>: Input area, e.g.,I0.0(input byte 0, bit 0)Q<number>: Output area, e.g.,Q0.0(output byte 0, bit 0)P<number>: Peripheral area, e.g.,P0.0(peripheral byte 0, bit 0)
Reading Array of Values
You can read arrays of values from the PLC by specifying the data type and the starting address. The binding will automatically convert the array elements to the corresponding OPC UA data types.
mapping:
- node: /di:DeviceSet/own:MyObject/own:MyIntArray
s7: plc1:DB1,INT12[0..9] # Read an array of 10 INT values from DB1 starting at address INT12
- automatic coercion of the array elements to the corresponding OPC UA data types is supported.
- the corresponding UA variable must be an array or a matrix type with valid dimensions.
Flexibility of the mapping
Notes
- The S7 binding supports reading and writing data to the PLC.
using value JSondata functions
You can use the $s7 Josonata function to read data from the PLC in a more flexible way. This allows you to perform calculations, transformations, and other operations on the S7 data before storing it in the OPC UA Variable.
mapping:
- node: /di:DeviceSet/own:MyObject/own:MyFloat2
value: |
(
$mb := $s7("plc1:DB2,REAL16");
{
"value": $mb.value / 10.0 + 3.14,
"statusCode": $mb.statusCode
};
)
```
The `$s7` function takes a string argument that specifies the PLC name, data block, and data type. The function returns a JSON object with the `value` and `statusCode` fields.