example_dummy.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python3
  2. #
  3. # Simple pyprofibus dummy example using dummy PHY.
  4. # This example can be run without any PB hardware.
  5. #
  6. import pyprofibus
  7. master = None
  8. try:
  9. # Parse the config file.
  10. config = pyprofibus.PbConf.fromFile("example_dummy.conf")
  11. # Create a DP master.
  12. master = config.makeDPM()
  13. # Create the slave descriptions.
  14. outData = {}
  15. for slaveConf in config.slaveConfs:
  16. slaveDesc = slaveConf.makeDpSlaveDesc()
  17. # Register the slave at the DPM
  18. master.addSlave(slaveDesc)
  19. # Set initial output data.
  20. outData[slaveDesc.slaveAddr] = bytearray((0x42, 0x24))
  21. # Initialize the DPM
  22. master.initialize()
  23. # Run the slave state machine.
  24. while True:
  25. # Write the output data.
  26. for slaveDesc in master.getSlaveList():
  27. slaveDesc.setOutData(outData[slaveDesc.slaveAddr])
  28. # Run slave state machines.
  29. handledSlaveDesc = master.run()
  30. # Get the in-data (receive)
  31. if handledSlaveDesc:
  32. inData = handledSlaveDesc.getInData()
  33. if inData is not None:
  34. # In our example the output data shall be the inverted input.
  35. outData[handledSlaveDesc.slaveAddr][0] = inData[1]
  36. outData[handledSlaveDesc.slaveAddr][1] = inData[0]
  37. except pyprofibus.ProfibusError as e:
  38. print("Terminating: %s" % str(e))
  39. finally:
  40. if master:
  41. master.destroy()