example_et200s.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. import pyprofibus
  26. from pyprofibus import DpTelegram_SetPrm_Req, monotonic_time
  27. master = None
  28. try:
  29. # Parse the config file.
  30. config = pyprofibus.PbConf.fromFile("example_et200s.conf")
  31. # Create a PHY (layer 1) interface object
  32. phy = config.makePhy()
  33. # Create a DP class 1 master with DP address 1
  34. master = pyprofibus.DPM1(phy = phy,
  35. masterAddr = config.dpMasterAddr,
  36. debug = True)
  37. outData = {}
  38. # Create a slave descriptions.
  39. for slaveConf in config.slaveConfs:
  40. gsd = slaveConf.gsd
  41. # Create a slave description for an ET-200S.
  42. # The ET-200S has got the DP address 8 set via DIP-switches.
  43. slaveDesc = pyprofibus.DpSlaveDesc(identNumber = gsd.getIdentNumber(),
  44. slaveAddr = slaveConf.addr)
  45. # Create Chk_Cfg telegram
  46. slaveDesc.setCfgDataElements(gsd.getCfgDataElements())
  47. # Set User_Prm_Data
  48. dp1PrmMask = bytearray((DpTelegram_SetPrm_Req.DPV1PRM0_FAILSAFE,
  49. DpTelegram_SetPrm_Req.DPV1PRM1_REDCFG,
  50. 0x00))
  51. dp1PrmSet = bytearray((DpTelegram_SetPrm_Req.DPV1PRM0_FAILSAFE,
  52. DpTelegram_SetPrm_Req.DPV1PRM1_REDCFG,
  53. 0x00))
  54. slaveDesc.setUserPrmData(gsd.getUserPrmData(dp1PrmMask = dp1PrmMask,
  55. dp1PrmSet = dp1PrmSet))
  56. # Set various standard parameters
  57. slaveDesc.setSyncMode(slaveConf.syncMode)
  58. slaveDesc.setFreezeMode(slaveConf.freezeMode)
  59. slaveDesc.setGroupMask(slaveConf.groupMask)
  60. slaveDesc.setWatchdog(slaveConf.watchdogMs)
  61. # Register the ET-200S slave at the DPM
  62. master.addSlave(slaveDesc)
  63. # Set initial output data.
  64. outData[slaveDesc.slaveAddr] = bytearray((0x00, 0x00))
  65. # Initialize the DPM
  66. master.initialize()
  67. # Cyclically run Data_Exchange.
  68. while True:
  69. # Write the output data.
  70. for slaveDesc in master.getSlaveList():
  71. slaveDesc.setOutData(outData[slaveDesc.slaveAddr])
  72. # Run slave state machines.
  73. handledSlaveDesc = master.run()
  74. # Get the in-data (receive)
  75. if handledSlaveDesc:
  76. inData = handledSlaveDesc.getInData()
  77. if inData is not None:
  78. # In our example the output data shall be a mirror of the input.
  79. outData[handledSlaveDesc.slaveAddr][0] = inData[0] & 3
  80. outData[handledSlaveDesc.slaveAddr][1] = (inData[0] >> 2) & 3
  81. except pyprofibus.ProfibusError as e:
  82. print("Terminating: %s" % str(e))
  83. finally:
  84. if master:
  85. master.destroy()