test_dummy.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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.conf
  6. import pyprofibus.dp
  7. import pyprofibus.dp_master
  8. import pyprofibus.phy_dummy
  9. import pyprofibus.phy_serial
  10. class Test_DummyPhy(TestCase):
  11. def test_dummy_phy(self):
  12. phy = pyprofibus.phy_dummy.CpPhyDummySlave(debug=True, echoDX=True)
  13. phy.setConfig(baudrate=19200)
  14. master = pyprofibus.DPM1(phy=phy,
  15. masterAddr=42,
  16. debug=True)
  17. conf = pyprofibus.conf.PbConf._SlaveConf()
  18. conf.addr = 84
  19. conf.inputSize = 1
  20. conf.outputSize = 1
  21. conf.diagPeriod = 0
  22. slaveDesc = pyprofibus.dp_master.DpSlaveDesc(conf)
  23. slaveDesc.setCfgDataElements([
  24. pyprofibus.dp.DpCfgDataElement(pyprofibus.dp.DpCfgDataElement.ID_TYPE_OUT),
  25. pyprofibus.dp.DpCfgDataElement(pyprofibus.dp.DpCfgDataElement.ID_TYPE_IN),
  26. ])
  27. slaveDesc.setUserPrmData(bytearray([1, 2, 3, 4, ]))
  28. slaveDesc.setSyncMode(True)
  29. slaveDesc.setFreezeMode(True)
  30. slaveDesc.setGroupMask(1)
  31. slaveDesc.setWatchdog(300)
  32. master.addSlave(slaveDesc)
  33. master.initialize()
  34. # Run slave initialization state machine.
  35. for i in range(25):
  36. slaveDesc.setMasterOutData(bytearray([1, ]))
  37. master.run()
  38. # Check dummy-slave response to Data_Exchange.
  39. for i in range(100):
  40. print("testing %d" % i)
  41. j = 0
  42. while True:
  43. j += 1
  44. self.assertTrue(j < 10)
  45. slaveDesc.setMasterOutData(bytearray([i, ]))
  46. master.run()
  47. ret = slaveDesc.getMasterInData()
  48. if j >= 5 and ret is not None:
  49. break
  50. self.assertEqual(bytearray(ret), bytearray([i ^ 0xFF, ]))