example_et200s.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. # Create a slave descriptions.
  38. for slaveConf in config.slaveConfs:
  39. gsd = slaveConf.gsd
  40. # Create a slave description for an ET-200S.
  41. # The ET-200S has got the DP address 8 set via DIP-switches.
  42. slaveDesc = pyprofibus.DpSlaveDesc(identNumber = gsd.getIdentNumber(),
  43. slaveAddr = slaveConf.addr)
  44. # Create Chk_Cfg telegram
  45. slaveDesc.setCfgDataElements(gsd.getCfgDataElements())
  46. # Set User_Prm_Data
  47. dp1PrmMask = bytearray((DpTelegram_SetPrm_Req.DPV1PRM0_FAILSAFE,
  48. DpTelegram_SetPrm_Req.DPV1PRM1_REDCFG,
  49. 0x00))
  50. dp1PrmSet = bytearray((DpTelegram_SetPrm_Req.DPV1PRM0_FAILSAFE,
  51. DpTelegram_SetPrm_Req.DPV1PRM1_REDCFG,
  52. 0x00))
  53. slaveDesc.setUserPrmData(gsd.getUserPrmData(dp1PrmMask = dp1PrmMask,
  54. dp1PrmSet = dp1PrmSet))
  55. # Set various standard parameters
  56. slaveDesc.setSyncMode(slaveConf.syncMode)
  57. slaveDesc.setFreezeMode(slaveConf.freezeMode)
  58. slaveDesc.setGroupMask(slaveConf.groupMask)
  59. slaveDesc.setWatchdog(slaveConf.watchdogMs)
  60. # Register the ET-200S slave at the DPM
  61. master.addSlave(slaveDesc)
  62. # Initialize the DPM
  63. master.initialize()
  64. slaveDescs = master.getSlaveList()
  65. # Cyclically run Data_Exchange.
  66. # 4 input bits from the 4-DI module are copied to
  67. # the two 2-DO modules.
  68. inData = 0
  69. rtSum, runtimes, nextPrint = 0, [ 0, ] * 512, monotonic_time() + 1.0
  70. while True:
  71. start = monotonic_time()
  72. # Run slave state machines.
  73. for slaveDesc in slaveDescs:
  74. outData = [inData & 3, (inData >> 2) & 3]
  75. inDataTmp = master.runSlave(slaveDesc, outData)
  76. if inDataTmp is not None:
  77. inData = inDataTmp[0]
  78. # Print statistics.
  79. end = monotonic_time()
  80. runtimes.append(end - start)
  81. rtSum = rtSum - runtimes.pop(0) + runtimes[-1]
  82. if end > nextPrint:
  83. nextPrint = end + 3.0
  84. sys.stderr.write("pyprofibus cycle time = %.3f ms\n" %\
  85. (rtSum / len(runtimes) * 1000.0))
  86. except pyprofibus.ProfibusError as e:
  87. print("Terminating: %s" % str(e))
  88. finally:
  89. if master:
  90. master.destroy()