from io_module import IO_Module from modbus import Modbus from log_reader import AnalogInputLogReader import colorama from colorama import Fore from time import sleep from serial import Serial from mb_registers import AI_REGS import matplotlib.pyplot as plt import matplotlib.animation as animation ai_name = {'AIN_1': 0, 'AIN_2': 1, 'AIN_3': 2, 'AIN_4': 3, 'AIN_5': 4, 'AIN_6': 5, 'AIN_7': 6, 'AIN_8': 7, 'AIN_9': 8, 'AIN_10': 9, 'AIN_11': 10, 'AIN_12': 11, 'V_ISO_CL': 12, 'V_ISO': 13, 'CRNT_LIM_U_BFR_R': 14, 'CRNT_LIM_U_ABFR_R': 15} class IO_AnalogInput(IO_Module): GRAPTH_LEN = 100 def __init__(self, modbus: Modbus): self.modbus = modbus super().__init__(self.modbus) self.log = AnalogInputLogReader(self.modbus) self.fig = plt.figure(1) self.input = self.fig.add_subplot(1, 1, 1) self.input.set_title('input_1') self.x = [0] self.data = [] self.graph_input = ai_name['AIN_1'] '''Чтение параметров''' # Рожим работы входов def get_inputs_mode(self): data = self.modbus.read_holding_registers(AI_REGS['ain_mode'], 1) return format(data[0], '012b') def get_inputs_alarm(self): data = self.modbus.read_holding_registers(AI_REGS['ain_alarm'], 1) return format(data[0], '012b') def get_raw_inputs(self): data = self.modbus.read_holding_registers(AI_REGS['ain_raw'], 16) return data '''Установка параметров''' def set_inputs_mode(self, val): self.modbus.write_holding_register(AI_REGS['ain_mode'], val) def set_ext_sens_power(self, val): self.modbus.write_holding_register(AI_REGS['esens_pow'], val) '''Вывод параметров''' def print_raw_inputs(self): data = self.get_raw_inputs() print(f"[ADC raw] IN_1: {data[0]}, IN_2: {data[1]}, IN_3: {data[2]}, IN_4: {data[3]}") print(f"[ADC raw] IN_5: {data[4]}, IN_6: {data[5]}, IN_7: {data[6]}, IN_8: {data[7]}") print(f"[ADC raw] IN_9: {data[8]}, IN_10: {data[9]}, IN_11: {data[10]}, IN_12: {data[11]}") print(f"[ADC raw] V_ISO_CL: {data[12]}, V_ISO: {data[13]}") print(f"[ADC raw] CRNT_LIM_U_BFR_R: {data[14]}, CRNT_LIM_U_ABFR_R: {data[15]}") '''Вывод данных на график''' def show_graph(self, channel: str): self.graph_input = ai_name[channel] ani = animation.FuncAnimation(self.fig, self.draw_raw_inputs, interval=50) plt.show() def draw_raw_inputs(self, i): data = self.get_raw_inputs() self.data.append(data[self.graph_input]) self.input.clear() self.input.plot(self.x, self.data) if len(self.data) == self.GRAPTH_LEN: self.data.pop(0) self.x.pop(0) self.x.append(self.x[-1] + 1) # print(self.in1) # print(self.x) def main(): colorama.init(autoreset=True) serial_port = Serial('COM24', 115200, timeout=0.05, parity='N', xonxoff=False) modbus_tester = Modbus(serial_port, 1) # dev_tester = IO_Digital(modbus_tester) ai = IO_AnalogInput(modbus_tester) '''Режим работы аналоговых входов''' # print(ai.get_inputs_mode()) # ai.set_inputs_mode(0b00000) # print(ai.get_inputs_mode()) '''Питание внешних датчиков''' # ai.set_ext_sens_power(1) '''Аварии аналоговых входов''' # for i in range(100): # print(ai.get_inputs_alarm()) # sleep(1) # ai.get_raw_inputs() # ai.print_raw_inputs() # ai.show_graph('AIN_1') if __name__ == '__main__': main()