example_et200s.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python3
  2. #
  3. # Simple pyprofibus example
  4. #
  5. # This example initializes an ET-200S slave, reads input
  6. # data and writes the data back to the module.
  7. #
  8. # The hardware configuration is as follows:
  9. #
  10. # v--------------v----------v----------v----------v----------v
  11. # | IM 151-1 | PM-E | 2 DO | 2 DO | 4 DI |
  12. # | STANDARD | DC24V | DC24V/2A | DC24V/2A | DC24V |
  13. # | | | | | |
  14. # | | | | | |
  15. # | ET 200S | | | | |
  16. # | | | | | |
  17. # | | | | | |
  18. # | 6ES7 | 6ES7 | 6ES7 | 6ES7 | 6ES7 |
  19. # | 151- | 138- | 132- | 132- | 131- |
  20. # | 1AA04- | 4CA01- | 4BB30- | 4BB30- | 4BD01- |
  21. # | 0AB0 | 0AA0 | 0AA0 | 0AA0 | 0AA0 |
  22. # ^--------------^----------^----------^----------^----------^
  23. #
  24. import sys
  25. sys.path.insert(0, "..")
  26. import pyprofibus
  27. def main(confdir=".", watchdog=None):
  28. master = None
  29. try:
  30. # Parse the config file.
  31. config = pyprofibus.PbConf.fromFile(confdir + "/example_et200s.conf")
  32. # Create a DP master.
  33. master = config.makeDPM()
  34. # Create the slave descriptions.
  35. outData = {}
  36. for slaveConf in config.slaveConfs:
  37. slaveDesc = slaveConf.makeDpSlaveDesc()
  38. # Set User_Prm_Data
  39. dp1PrmMask = bytearray((pyprofibus.dp.DpTelegram_SetPrm_Req.DPV1PRM0_FAILSAFE,
  40. pyprofibus.dp.DpTelegram_SetPrm_Req.DPV1PRM1_REDCFG,
  41. 0x00))
  42. dp1PrmSet = bytearray((pyprofibus.dp.DpTelegram_SetPrm_Req.DPV1PRM0_FAILSAFE,
  43. pyprofibus.dp.DpTelegram_SetPrm_Req.DPV1PRM1_REDCFG,
  44. 0x00))
  45. slaveDesc.setUserPrmData(slaveConf.gsd.getUserPrmData(dp1PrmMask=dp1PrmMask,
  46. dp1PrmSet=dp1PrmSet))
  47. # Register the ET-200S slave at the DPM
  48. master.addSlave(slaveDesc)
  49. # Set initial output data.
  50. outData[slaveDesc.name] = bytearray((0x00, 0x00))
  51. # Initialize the DPM
  52. master.initialize()
  53. # Cyclically run Data_Exchange.
  54. while True:
  55. # Write the output data.
  56. for slaveDesc in master.getSlaveList():
  57. slaveDesc.setMasterOutData(outData[slaveDesc.name])
  58. # Run slave state machines.
  59. handledSlaveDesc = master.run()
  60. # Get the in-data (receive)
  61. if handledSlaveDesc:
  62. inData = handledSlaveDesc.getMasterInData()
  63. if inData is not None:
  64. # In our example the output data shall be a mirror of the input.
  65. outData[handledSlaveDesc.name][0] = inData[0] & 3
  66. outData[handledSlaveDesc.name][1] = (inData[0] >> 2) & 3
  67. # Feed the system watchdog, if it is available.
  68. if watchdog is not None:
  69. watchdog()
  70. except pyprofibus.ProfibusError as e:
  71. print("Terminating: %s" % str(e))
  72. return 1
  73. finally:
  74. if master:
  75. master.destroy()
  76. return 0
  77. if __name__ == "__main__":
  78. import sys
  79. sys.exit(main())