example_et200s.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 pyprofibus
  25. import pyprofibus.phy_serial
  26. from pyprofibus.gsd.interp import GsdInterp
  27. from pyprofibus.dp import DpTelegram_SetPrm_Req
  28. # The serial port that we connect to
  29. port = "/dev/ttyAMA0"
  30. # Enable verbose debug messages?
  31. debug = True
  32. # Parse the GSD file.
  33. # And select the plugged modules.
  34. gsd = GsdInterp.fromFile("si03806a.gse", debug = False)
  35. gsd.setConfiguredModule("6ES7 138-4CA01-0AA0 PM-E DC24V")
  36. gsd.setConfiguredModule("6ES7 132-4BB30-0AA0 2DO DC24V")
  37. gsd.setConfiguredModule("6ES7 132-4BB30-0AA0 2DO DC24V")
  38. gsd.setConfiguredModule("6ES7 131-4BD01-0AA0 4DI DC24V")
  39. # Create a PHY (layer 1) interface object
  40. phy = pyprofibus.phy_serial.CpPhySerial(port = port,
  41. debug = debug)
  42. phy.setConfig(19200)
  43. # Create a DP class 1 master with DP address 1
  44. master = pyprofibus.DPM1(phy = phy,
  45. masterAddr = 2,
  46. debug = debug)
  47. # Create a slave description for an ET-200S.
  48. # The ET-200S has got the DP address 8 set via DIP-switches.
  49. et200s = pyprofibus.DpSlaveDesc(identNumber = gsd.getIdentNumber(),
  50. slaveAddr = 8)
  51. # Create Chk_Cfg telegram
  52. et200s.setCfgDataElements(gsd.getCfgDataElements())
  53. # Set User_Prm_Data
  54. dp1PrmMask = bytearray((DpTelegram_SetPrm_Req.DPV1PRM0_FAILSAFE,
  55. DpTelegram_SetPrm_Req.DPV1PRM1_REDCFG,
  56. 0x00))
  57. dp1PrmSet = bytearray((DpTelegram_SetPrm_Req.DPV1PRM0_FAILSAFE,
  58. DpTelegram_SetPrm_Req.DPV1PRM1_REDCFG,
  59. 0x00))
  60. et200s.setUserPrmData(gsd.getUserPrmData(dp1PrmMask = dp1PrmMask,
  61. dp1PrmSet = dp1PrmSet))
  62. # Set various standard parameters
  63. et200s.setSyncMode(True) # Sync-mode supported
  64. et200s.setFreezeMode(True) # Freeze-mode supported
  65. et200s.setGroupMask(1) # Group-ident 1
  66. et200s.setWatchdog(300) # Watchdog: 300 ms
  67. # Register the ET-200S slave at the DPM
  68. master.addSlave(et200s)
  69. try:
  70. # Initialize the DPM and all registered slaves
  71. master.initialize()
  72. # Cyclically run Data_Exchange.
  73. # 4 input bits from the 4-DI module are copied to
  74. # the two 2-DO modules.
  75. inData = 0
  76. while 1:
  77. outData = [inData & 3, (inData >> 2) & 3]
  78. inDataTmp = master.runSlave(et200s, outData)
  79. if inDataTmp is not None:
  80. inData = inDataTmp[0]
  81. except Exception as e:
  82. print("Terminating: %s" % str(e))
  83. master.destroy()