test_dummy.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. self.assertFalse(slaveDesc.isConnecting())
  35. self.assertFalse(slaveDesc.isConnected())
  36. # Run slave initialization state machine.
  37. for i in range(25):
  38. slaveDesc.setMasterOutData(bytearray([1, ]))
  39. master.run()
  40. if i == 1:
  41. self.assertTrue(slaveDesc.isConnecting())
  42. self.assertFalse(slaveDesc.isConnected())
  43. # Check dummy-slave response to Data_Exchange.
  44. for i in range(100):
  45. print("testing %d" % i)
  46. self.assertFalse(slaveDesc.isConnecting())
  47. self.assertTrue(slaveDesc.isConnected())
  48. j = 0
  49. while True:
  50. j += 1
  51. self.assertTrue(j < 10)
  52. slaveDesc.setMasterOutData(bytearray([i, ]))
  53. master.run()
  54. ret = slaveDesc.getMasterInData()
  55. if j >= 5 and ret is not None:
  56. break
  57. self.assertEqual(bytearray(ret), bytearray([i ^ 0xFF, ]))