example_s7-315-2dp.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env python3
  2. #
  3. # Simple pyprofibus example
  4. #
  5. # This example initializes an S7-315-2DP configured as slave,
  6. # reads its input data and writes the data back to the module.
  7. #
  8. import pyprofibus
  9. master = None
  10. try:
  11. # Parse the config file.
  12. config = pyprofibus.PbConf.fromFile("example_s7-315-2dp.conf")
  13. # Create a DP master.
  14. master = config.makeDPM()
  15. # Create the slave descriptions.
  16. outData = {}
  17. for slaveConf in config.slaveConfs:
  18. slaveDesc = slaveConf.makeDpSlaveDesc()
  19. # Register the S7-315-2DP slave at the DPM
  20. master.addSlave(slaveDesc)
  21. # Set initial output data.
  22. outData[slaveDesc.slaveAddr] = bytearray((0x00, ))
  23. # Initialize the DPM
  24. master.initialize()
  25. # Cyclically run Data_Exchange.
  26. while True:
  27. # Write the output data.
  28. for slaveDesc in master.getSlaveList():
  29. slaveDesc.setOutData(outData[slaveDesc.slaveAddr])
  30. # Run slave state machines.
  31. handledSlaveDesc = master.run()
  32. # Get the in-data (receive) and set it as out-data (transmit).
  33. if handledSlaveDesc:
  34. inData = handledSlaveDesc.getInData()
  35. if inData is not None:
  36. # In our example the output data shall be a mirror of the input.
  37. outData[handledSlaveDesc.slaveAddr] = inData
  38. except pyprofibus.ProfibusError as e:
  39. print("Terminating: %s" % str(e))
  40. finally:
  41. if master:
  42. master.destroy()