example_dummy.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. def main(watchdog=None):
  8. master = None
  9. try:
  10. # Parse the config file.
  11. config = pyprofibus.PbConf.fromFile("example_dummy.conf")
  12. # Create a DP master.
  13. master = config.makeDPM()
  14. # Create the slave descriptions.
  15. outData = {}
  16. for slaveConf in config.slaveConfs:
  17. slaveDesc = slaveConf.makeDpSlaveDesc()
  18. # Set User_Prm_Data
  19. dp1PrmMask = bytearray((pyprofibus.dp.DpTelegram_SetPrm_Req.DPV1PRM0_FAILSAFE,
  20. pyprofibus.dp.DpTelegram_SetPrm_Req.DPV1PRM1_REDCFG,
  21. 0x00))
  22. dp1PrmSet = bytearray((pyprofibus.dp.DpTelegram_SetPrm_Req.DPV1PRM0_FAILSAFE,
  23. pyprofibus.dp.DpTelegram_SetPrm_Req.DPV1PRM1_REDCFG,
  24. 0x00))
  25. slaveDesc.setUserPrmData(slaveConf.gsd.getUserPrmData(dp1PrmMask=dp1PrmMask,
  26. dp1PrmSet=dp1PrmSet))
  27. # Register the slave at the DPM
  28. master.addSlave(slaveDesc)
  29. # Set initial output data.
  30. outData[slaveDesc.name] = bytearray((0x42, 0x24))
  31. # Initialize the DPM
  32. master.initialize()
  33. # Run the slave state machine.
  34. while True:
  35. # Write the output data.
  36. for slaveDesc in master.getSlaveList():
  37. slaveDesc.setOutData(outData[slaveDesc.name])
  38. # Run slave state machines.
  39. handledSlaveDesc = master.run()
  40. # Get the in-data (receive)
  41. if handledSlaveDesc:
  42. inData = handledSlaveDesc.getInData()
  43. if inData is not None:
  44. # In our example the output data shall be the inverted input.
  45. outData[handledSlaveDesc.name][0] = inData[1]
  46. outData[handledSlaveDesc.name][1] = inData[0]
  47. # Feed the system watchdog, if it is available.
  48. if watchdog is not None:
  49. watchdog()
  50. except pyprofibus.ProfibusError as e:
  51. print("Terminating: %s" % str(e))
  52. return 1
  53. finally:
  54. if master:
  55. master.destroy()
  56. return 0
  57. if __name__ == "__main__":
  58. import sys
  59. sys.exit(main())