Skip to content

Siemens S7 Communication Protocol Library

The Zerynth Siemens S7 Communication Protocol Library can be used to ease the connection to a Siemens Programmable Logic Controller (PLC) hosting an S7 Server instance.

It allows to make your device act as an S7 client which can connect to the server and start interacting with it.

Important

Siemens S7 Communication Protocol Library is supported only for ESP32 devices.

The library instantiates a Client() object when init() function is called, the object is then accessible through client variable:

import s7

s7.init()
s7.client.connect(my_addr)

The Client class

class Client

class Client()

Create an S7 client. Since only one client can be active, creation is automatically handled by init() module function.

Client.connect

connect(addr, rack=0, slot=2)

Arguments:

  • addr – S7 server address
  • rack – S7 server rack
  • slot – S7 server slot

Connect to S7 server.

Client.readarea

readarea(area, dbnumber, start, amount)

Arguments:

  • area – S7 server area to read
  • dbnumber – S7 server db to read, ignored if area != S7AreaDB
  • start – read offset
  • amount – read amount as number of words to read, note that different areas have different wordsize (automatically handled by the library)

Read an S7 server area. Returns a bytes or a shorts object whether chosen area has a wordsize of a single byte or two bytes.

Client.writearea

writearea(area, dbnumber, start, buf)

Arguments:

  • area – S7 server area to read
  • dbnumber – S7 server db to read, ignored if area != S7AreaDB
  • start – write offset
  • buf – buffer to write, must be a bytarray or a shortarray whether chosen area has a wordsize of a single byte or two bytes.

Write buf to an S7 server area.

Client.readmultivars

readmultivars(items)

Arguments: items – read descriptors.

Perform multiple reads within a single call. A tuple of read descriptors must be passed, containing tuples describing each read step:

items = ((s7.S7AreaDB, 1, 16, 10), (s7.S7AreaDB, 2, 16, 10))
bufs = s7.client.readmultivars(items)

The above example defines a tuple item containig two descriptors, the first one to read from DB area, from DB number 1, with an offset of 16, and amount of 10. The second to perform the same operation but on DB number 2.

As it is clear in the example, descriptor params have to be placed in the same order (and with the same meaning) expected by readarea() method.

Returns a tuple of bytes or shorts objects each corresponding to a single read result.

Client.writemultivars

writemultivars(items)

Arguments: items – write descriptors.

Perform multiple writes withing a single call. A tuple of write descriptors must be passed, containing tuples describing each write step:

items = ((s7.S7AreaDB, 1, 16, bytes([1,2,3,4,5,6,7,8])), (s7.S7AreaDB, 2, 16, bytes([1,2,3,4,5,6,7,8])))
s7.client.writemultivars(items)

The above example defines a tuple item containig two descriptors, the first one to write to DB area, DB number 1, with an offset of 16, the sequence of bytes [1,2,3,4,5,6,7,8]. The second to perform the same operation but on DB number 2.

As it is clear in the example, descriptor params have to be placed in the same order (and with the same meaning) expected by writearea() method.

Library Functions

init

init()

Init module, after this call client global variable is available containing a Client() instance.