analog_in.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. # Режим измерения входов (0 - напряжение или 1 - ток. (битовое поле))
  33. def get_inputs_mode(self):
  34. data = self.modbus.read_holding_registers(AI_REGS['ain_mode'], 1)
  35. return format(data[0], '012b')
  36. def get_input_gain(self, input):
  37. data = self.modbus.read_holding_registers(AI_REGS['ain_gain'] + input - 1, 1)
  38. return data[0]
  39. def get_inputs_alarm(self):
  40. data = self.modbus.read_holding_registers(AI_REGS['ain_alarm'], 1)
  41. return format(data[0], '012b')
  42. def get_raw_inputs(self):
  43. data = self.modbus.read_holding_registers(AI_REGS['ain_raw'], 16)
  44. return data
  45. '''Установка параметров'''
  46. def set_inputs_state(self, val):
  47. self.modbus.write_holding_register(AI_REGS['ain_state'], val)
  48. def set_inputs_mode(self, val):
  49. self.modbus.write_holding_register(AI_REGS['ain_mode'], val)
  50. def set_input_gain(self, input, value):
  51. # if value not in (1, 2, 4, 8, 16, 32, 64, 128):
  52. # return None
  53. self.modbus.write_holding_register(AI_REGS['ain_gain'] + input - 1, value)
  54. def set_ext_sens_power(self, val):
  55. self.modbus.write_holding_register(AI_REGS['esens_pow'], val)
  56. '''Настройки входов'''
  57. def print_inputs(self):
  58. print(Fore.GREEN + '____________________________________________')
  59. print(Fore.GREEN + 'Analog inputs settings:')
  60. # Значения состояний входов вкл./выкл. (битовое поле)
  61. print('Inputs state [bit field] :', Fore.GREEN + self.get_inputs_state())
  62. # Режим измерения входов напряжение или ток. (битовое поле)
  63. print('Inputs mode [bit field] :', Fore.GREEN + self.get_inputs_mode())
  64. # Коэффициенты усиления
  65. for i in range(1, 13):
  66. print(f'Gain factor channel {i} : ', end='')
  67. print(Fore.GREEN + str(self.get_input_gain(i)))
  68. '''Вывод параметров'''
  69. def print_raw_inputs(self):
  70. data = self.get_raw_inputs()
  71. print(f"[ADC raw] IN_1: {data[0]}, IN_2: {data[1]}, IN_3: {data[2]}, IN_4: {data[3]}")
  72. print(f"[ADC raw] IN_5: {data[4]}, IN_6: {data[5]}, IN_7: {data[6]}, IN_8: {data[7]}")
  73. print(f"[ADC raw] IN_9: {data[8]}, IN_10: {data[9]}, IN_11: {data[10]}, IN_12: {data[11]}")
  74. print(f"[ADC raw] V_ISO_CL: {data[12]}, V_ISO: {data[13]}")
  75. print(f"[ADC raw] CRNT_LIM_U_BFR_R: {data[14]}, CRNT_LIM_U_ABFR_R: {data[15]}")
  76. '''Вывод данных на график'''
  77. def show_graph(self, channel: str):
  78. self.graph_input = ai_name[channel]
  79. ani = animation.FuncAnimation(self.fig, self.draw_raw_inputs, interval=50)
  80. plt.show()
  81. def draw_raw_inputs(self, i):
  82. data = self.get_raw_inputs()
  83. self.data.append(data[self.graph_input])
  84. self.input.clear()
  85. self.input.plot(self.x, self.data)
  86. if len(self.data) == self.GRAPTH_LEN:
  87. self.data.pop(0)
  88. self.x.pop(0)
  89. self.x.append(self.x[-1] + 1)
  90. # print(self.in1)
  91. # print(self.x)
  92. def main():
  93. colorama.init(autoreset=True)
  94. serial_port = Serial('COM22', 115200, timeout=0.05, parity='N', xonxoff=False)
  95. modbus_tester = Modbus(serial_port, 1)
  96. # modbus_tester.MB_DEBUG = True
  97. # dev_tester = IO_Digital(modbus_tester)
  98. ai = IO_AnalogInput(modbus_tester)
  99. '''Режим работы аналоговых входов'''
  100. # ai.print_inputs()
  101. '''Установка коэффициентов усиления. Тесты'''
  102. for i in range(1, 13):
  103. ai.set_input_gain(i, i+21)
  104. # for i in range(1000):
  105. # ai.print_inputs()
  106. # sleep(1)
  107. # print(ai.get_inputs_state())
  108. # ai.set_inputs_state(0b1000_1010_1010)
  109. # print(ai.get_inputs_state())
  110. # sleep(1)
  111. # ai.set_inputs_state(0b0)
  112. # print(ai.get_inputs_mode())
  113. # ai.set_inputs_mode(0b00001)
  114. # print(ai.get_inputs_mode())
  115. '''Питание внешних датчиков'''
  116. # ai.set_ext_sens_power(1)
  117. '''Аварии аналоговых входов'''
  118. # for i in range(100):
  119. # print(ai.get_inputs_alarm())
  120. # sleep(1)
  121. # ai.get_raw_inputs()
  122. # ai.print_raw_inputs()
  123. # ai.show_graph('AIN_2')
  124. # print(ai.sys.get_system_vars())
  125. '''Сохранение настроек'''
  126. # ai.sys.save_sattings()
  127. '''Обновление прошивки'''
  128. # serial_port.timeout = 1
  129. # modbus_tester.MB_DEBUG = True
  130. # ai.updater.update('fw.bin', 'MAI_12')
  131. if __name__ == '__main__':
  132. main()