SwitchTester.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import argparse
  2. import sys
  3. import pyvisa
  4. import re
  5. import time
  6. from sys import exit
  7. if __name__ == '__main__':
  8. parser = argparse.ArgumentParser(prog='SCPI device programmer ',
  9. description='Tool for writing device model and serial number')
  10. parser.add_argument('number_of_switches', metavar = 's', type = int, nargs = '+',
  11. help = 'serial number for device')
  12. #parser.add_argument('model', metavar = 'm', type = int, nargs = '+',
  13. # help = 'device model')
  14. args = parser.parse_args()
  15. isRequest = False
  16. rm = pyvisa.ResourceManager()
  17. print('Число переключений: {0}'.format(args.number_of_switches[0]))
  18. VID = '0x2226'
  19. PID = '0x0065'
  20. for device in rm.list_resources():
  21. try:
  22. instr = rm.open_resource(device)
  23. if VID in device and PID in device:
  24. #Device detected and it's switchboard
  25. print('Device detected and its switchboard')
  26. print(instr.query('*IDN?'))
  27. print("Start test")
  28. experiment_start_time = time.time_ns()
  29. connectedPortIndex = 0
  30. connectedPortIndex2 = 1
  31. number_of_switches = args.number_of_switches[0]
  32. for i in range(number_of_switches):
  33. #print("Номер итерации: ", i)
  34. #print('CTRL:PORT {0},{1}'.format(connectedPortIndex, 0))
  35. instr.write('CTRL:PORT {0},{1}'.format(connectedPortIndex, connectedPortIndex2))
  36. if isRequest:
  37. nums = instr.query('CTRL:PORT?')
  38. portIndexes = re.findall(r'\d+', nums)
  39. #print(portIndexes)
  40. if(connectedPortIndex != int(portIndexes[0])):
  41. print("Порт не успел скоммутироваться!")
  42. break
  43. #print("Коммутируем порт: ", connectedPortIndex, connectedPortIndex2)
  44. connectedPortIndex += 1
  45. connectedPortIndex2 += 1
  46. if(connectedPortIndex > 12):
  47. connectedPortIndex = 0
  48. if(connectedPortIndex2 > 12):
  49. connectedPortIndex2 = 0
  50. time.sleep(10/1000)
  51. experiment_end_time = time.time_ns();
  52. experiment_time = experiment_end_time - experiment_start_time
  53. print("Затраченное время на эксперимент, мс: ", experiment_time / 1e6)
  54. print("Время переключения порта, мс: ", experiment_time / number_of_switches / 1e6)
  55. break;
  56. except:
  57. continue
  58. #print("Not connected device")
  59. rm.close()
  60. exit(0)