test_dummy.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from __future__ import division, absolute_import, print_function, unicode_literals
  2. from pyprofibus_tstlib import *
  3. initTest(__file__)
  4. import pyprofibus
  5. import pyprofibus.phy_dummy
  6. import pyprofibus.phy_serial
  7. class Test_DummyPhy(TestCase):
  8. def test_dummy_phy(self):
  9. phy = pyprofibus.phy_dummy.CpPhyDummySlave(debug=True)
  10. phy.setConfig(baudrate=19200)
  11. master = pyprofibus.DPM1(phy=phy,
  12. masterAddr=42,
  13. debug=True)
  14. slaveDesc = pyprofibus.DpSlaveDesc(gsd=None,
  15. slaveAddr=84)
  16. slaveDesc.setCfgDataElements([
  17. pyprofibus.DpCfgDataElement(pyprofibus.DpCfgDataElement.ID_TYPE_OUT),
  18. pyprofibus.DpCfgDataElement(pyprofibus.DpCfgDataElement.ID_TYPE_IN),
  19. ])
  20. slaveDesc.setUserPrmData(bytearray([1, 2, 3, 4, ]))
  21. slaveDesc.setSyncMode(True)
  22. slaveDesc.setFreezeMode(True)
  23. slaveDesc.setGroupMask(1)
  24. slaveDesc.setWatchdog(300)
  25. master.addSlave(slaveDesc)
  26. master.initialize()
  27. # Run slave initialization state machine.
  28. for i in range(25):
  29. slaveDesc.setOutData(bytearray([1, ]))
  30. master.run()
  31. # Check dummy-slave response to Data_Exchange.
  32. for i in range(100):
  33. print("testing %d" % i)
  34. j = 0
  35. while True:
  36. j += 1
  37. self.assertTrue(j < 10)
  38. slaveDesc.setOutData(bytearray([i, ]))
  39. master.run()
  40. ret = slaveDesc.getInData()
  41. if j >= 5 and ret is not None:
  42. break
  43. self.assertEqual(bytearray(ret), bytearray([i ^ 0xFF, ]))