Siemens S7

The S7 class provides a simplified interface for communicating with Siemens S7 PLCs (e.g., S7-300/400, S7-1200/1500). It manages the connection and provides a polling-based mechanism for efficiently reading and writing data to PLC memory addresses.

You must first create an instance of this class to interact with a PLC. The typical workflow is to connect, add variables (items) to a polling list, and then perform read/write operations on that list.


getStatus

Retrieves the current connectivity status of the client.

Output

A string representing the current state: disconnected, connecting, or connected.


initiateConnection

Establishes a connection to the target PLC.

Parameters

  • options: An object containing the PLC's connection details.

    • host: The IP address or hostname of the PLC.

    • port: The TCP port. Defaults to 102.

    • rack: The rack number where the CPU is located. Defaults to 0.

    • slot: The slot number of the CPU. Defaults to 2.

    • timeout: Connection timeout in milliseconds. Defaults to 5000.

Example

# options
host: 192.168.1.10
rack: 0
slot: 1

dropConnection

Terminates the TCP connection to the PLC.

Output

Returns true on success.


setAddressDictionary

Optionally defines human-readable aliases (tags) for specific PLC memory addresses. This allows you to use simple names like MOTOR_SPEED in read/write functions instead of raw addresses like DB1,REAL4.

Address Syntax

Addresses must follow the format <data block number.><memory area><data type><byte offset><.array length>.

  • Real Number: MR30 (MD30 as REAL)

  • Long Real (1200/1500): DB10,LR32

  • Integer: DB10,INT6 or DB10,I6

  • Integer Array: DB10,INT6.2 (An array of 2 INTs)

  • Boolean/Bit: DB10,X14.0

  • Boolean Array/Byte: DB10,X14.0.8 (An array of 8 BOOLs)

  • String: DB10,S20.30 (String at offset 20, max length 30)

  • Date and Time (Legacy): DB10,DT0

  • Date and Time (Newer PLCs): DB10,DTL0

Parameters

  • dictionary: An object that maps your chosen alias names to their corresponding S7 address strings.

Example

# dictionary
MOTOR_SPEED: DB1,REAL4
VALVE_STATUS: DB1,X10.2
ERROR_CODES: DB1,INT20.5

addItems

Adds one or more variables to an internal polling list. The readAllItems function reads all variables in this list in a single, optimized request.

Parameters

  • items: A single variable address (or alias) as a string, or an array of strings.

Example

# items
['MOTOR_SPEED', 'VALVE_STATUS']

removeItems

Removes one or more items from the internal polling list.

Parameters

  • items: An optional string or array of strings representing the items to remove. If this is omitted, all items are removed from the list.

Example

# items
VALVE_STATUS

writeItems

Writes one or more values to the corresponding variables in the PLC.

Note: You can only have one write operation in progress at a time.

Parameters

  • items: The variable (or array of variables) to write to.

  • values: The value (or array of values) to be written. The number of values must match the number of items.

Example

# items
MOTOR_SPEED
# values
1500.5

readAllItems

Reads the current values of all variables that have been added to the internal polling list via addItems.

Output

An object where keys are the variable addresses/aliases and values are their corresponding values read from the PLC. For example:

JSON

{
  "MOTOR_SPEED": 1499.98,
  "VALVE_STATUS": true
}

Last updated