example_dummy_oneslave.py 1.9 KB

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