example_dummy_inputonly.py 2.0 KB

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