aboutsummaryrefslogtreecommitdiffstats
path: root/example_s7-315-2dp.py
blob: d6a06ee3bebe99302f96baa785e4150c9ed4eb8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
#
# Simple pyprofibus example
#
# This example initializes an S7-315-2DP configured as slave,
# reads its input data and writes the data back to the module.
#

import pyprofibus

master = None
try:
	# Parse the config file.
	config = pyprofibus.PbConf.fromFile("example_s7-315-2dp.conf")

	# Create a DP master.
	master = config.makeDPM()

	# Create the slave descriptions.
	outData = {}
	for slaveConf in config.slaveConfs:
		slaveDesc = slaveConf.makeDpSlaveDesc()

		# Register the S7-315-2DP slave at the DPM
		master.addSlave(slaveDesc)

		# Set initial output data.
		outData[slaveDesc.slaveAddr] = bytearray((0x00, ))

	# Initialize the DPM
	master.initialize()

	# Cyclically run Data_Exchange.
	while True:
		# Write the output data.
		for slaveDesc in master.getSlaveList():
			slaveDesc.setOutData(outData[slaveDesc.slaveAddr])

		# Run slave state machines.
		handledSlaveDesc = master.run()

		# Get the in-data (receive) and set it as out-data (transmit).
		if handledSlaveDesc:
			inData = handledSlaveDesc.getInData()
			if inData is not None:
				# In our example the output data shall be a mirror of the input.
				outData[handledSlaveDesc.slaveAddr] = inData

except pyprofibus.ProfibusError as e:
	print("Terminating: %s" % str(e))
finally:
	if master:
		master.destroy()
bues.ch cgit interface