analog_in.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. from io_module import IO_Module
  2. from modbus import Modbus
  3. from log_reader import AnalogInputLogReader
  4. import colorama
  5. from colorama import Fore
  6. from time import sleep
  7. from serial import Serial
  8. from mb_registers import AI_REGS
  9. import matplotlib.pyplot as plt
  10. import matplotlib.animation as animation
  11. ai_name = {'AIN_1': 0, 'AIN_2': 1, 'AIN_3': 2, 'AIN_4': 3, 'AIN_5': 4,
  12. 'AIN_6': 5, 'AIN_7': 6, 'AIN_8': 7, 'AIN_9': 8, 'AIN_10': 9,
  13. 'AIN_11': 10, 'AIN_12': 11, 'V_ISO_CL': 12, 'V_ISO': 13,
  14. 'CRNT_LIM_U_BFR_R': 14, 'CRNT_LIM_U_ABFR_R': 15}
  15. class IO_AnalogInput(IO_Module):
  16. GRAPTH_LEN = 100
  17. def __init__(self, modbus: Modbus):
  18. self.modbus = modbus
  19. super().__init__(self.modbus)
  20. self.log = AnalogInputLogReader(self.modbus)
  21. self.fig = plt.figure(1)
  22. self.input = self.fig.add_subplot(1, 1, 1)
  23. self.input.set_title('input_1')
  24. self.x = [0]
  25. self.data = []
  26. self.graph_input = ai_name['AIN_1']
  27. '''Чтение параметров'''
  28. # Режим работы входов
  29. def get_inputs_state(self):
  30. data = self.modbus.read_holding_registers(AI_REGS['ain_state'], 1)
  31. return format(data[0], '012b')
  32. def get_inputs_mode(self):
  33. data = self.modbus.read_holding_registers(AI_REGS['ain_mode'], 1)
  34. return format(data[0], '012b')
  35. def get_inputs_alarm(self):
  36. data = self.modbus.read_holding_registers(AI_REGS['ain_alarm'], 1)
  37. return format(data[0], '012b')
  38. def get_raw_inputs(self):
  39. data = self.modbus.read_holding_registers(AI_REGS['ain_raw'], 16)
  40. return data
  41. '''Установка параметров'''
  42. def set_inputs_state(self, val):
  43. self.modbus.write_holding_register(AI_REGS['ain_state'], val)
  44. def set_inputs_mode(self, val):
  45. self.modbus.write_holding_register(AI_REGS['ain_mode'], val)
  46. def set_ext_sens_power(self, val):
  47. self.modbus.write_holding_register(AI_REGS['esens_pow'], val)
  48. '''Настройки входов'''
  49. def print_inputs(self):
  50. print(Fore.GREEN + '____________________________________________')
  51. print(Fore.GREEN + 'Analog inputs settings:')
  52. # Значения состояний входов вкл./выкл. (битовое поле)
  53. print('Inputs state [bit field] :', Fore.GREEN + self.get_inputs_state())
  54. # Режим измерения входов напряжение или ток. (битовое поле)
  55. print('Inputs mode [bit field] :', Fore.GREEN + self.get_inputs_mode())
  56. '''Вывод параметров'''
  57. def print_raw_inputs(self):
  58. data = self.get_raw_inputs()
  59. print(f"[ADC raw] IN_1: {data[0]}, IN_2: {data[1]}, IN_3: {data[2]}, IN_4: {data[3]}")
  60. print(f"[ADC raw] IN_5: {data[4]}, IN_6: {data[5]}, IN_7: {data[6]}, IN_8: {data[7]}")
  61. print(f"[ADC raw] IN_9: {data[8]}, IN_10: {data[9]}, IN_11: {data[10]}, IN_12: {data[11]}")
  62. print(f"[ADC raw] V_ISO_CL: {data[12]}, V_ISO: {data[13]}")
  63. print(f"[ADC raw] CRNT_LIM_U_BFR_R: {data[14]}, CRNT_LIM_U_ABFR_R: {data[15]}")
  64. '''Вывод данных на график'''
  65. def show_graph(self, channel: str):
  66. self.graph_input = ai_name[channel]
  67. ani = animation.FuncAnimation(self.fig, self.draw_raw_inputs, interval=50)
  68. plt.show()
  69. def draw_raw_inputs(self, i):
  70. data = self.get_raw_inputs()
  71. self.data.append(data[self.graph_input])
  72. self.input.clear()
  73. self.input.plot(self.x, self.data)
  74. if len(self.data) == self.GRAPTH_LEN:
  75. self.data.pop(0)
  76. self.x.pop(0)
  77. self.x.append(self.x[-1] + 1)
  78. # print(self.in1)
  79. # print(self.x)
  80. def main():
  81. colorama.init(autoreset=True)
  82. serial_port = Serial('COM22', 115200, timeout=0.05, parity='N', xonxoff=False)
  83. modbus_tester = Modbus(serial_port, 1)
  84. # modbus_tester.MB_DEBUG = True
  85. # dev_tester = IO_Digital(modbus_tester)
  86. ai = IO_AnalogInput(modbus_tester)
  87. '''Режим работы аналоговых входов'''
  88. for i in range(1000):
  89. ai.print_inputs()
  90. sleep(1)
  91. # print(ai.get_inputs_state())
  92. # ai.set_inputs_state(0b1000_1010_1010)
  93. # print(ai.get_inputs_state())
  94. # sleep(1)
  95. # ai.set_inputs_state(0b0)
  96. # print(ai.get_inputs_mode())
  97. # ai.set_inputs_mode(0b00001)
  98. # print(ai.get_inputs_mode())
  99. '''Питание внешних датчиков'''
  100. # ai.set_ext_sens_power(1)
  101. '''Аварии аналоговых входов'''
  102. # for i in range(100):
  103. # print(ai.get_inputs_alarm())
  104. # sleep(1)
  105. # ai.get_raw_inputs()
  106. # ai.print_raw_inputs()
  107. # ai.show_graph('AIN_2')
  108. # print(ai.sys.get_system_vars())
  109. '''Сохранение настроек'''
  110. # ai.sys.save_sattings()
  111. '''Обновление прошивки'''
  112. # serial_port.timeout = 1
  113. # modbus_tester.MB_DEBUG = True
  114. # ai.updater.update('fw.bin', 'MAI_12')
  115. if __name__ == '__main__':
  116. main()