from configparser import ConfigParser import os class ConfigManager: def __init__(self, ini_path="setup.ini"): self.ini_path = ini_path self.config = ConfigParser() def read_config(self): """读取配置文件""" if not os.path.exists(self.ini_path): return None self.config.read(self.ini_path, encoding='utf-8') return self.config def create_default_config(self, num): """创建默认配置""" self.config.add_section('Sys_config') self.config.set('Sys_config', 'log_time', '10') self.config.set('Sys_config', 'hex_send', '2') self.config.set('Sys_config', 'hex_receive', '2') self.config.set('Sys_config', 'add_date', '0') self.config.set('Sys_config', 'cr_lf', '2') self.config.set('Sys_config', 'auto_sav_log', '0') self.config.set('Sys_config', 'auto_sav_db', '2') self.config.set('Sys_config', 'add_crc_rtu', '0') self.config.add_section('Serial_config') self.config.set('Serial_config', 'port', 'None') self.config.set('Serial_config', 'baudrate', '9600') self.config.set('Serial_config', 'parity', 'N') self.config.add_section('Net_config') self.config.set('Net_config', 'port', '9000') self.config.add_section('Modbus_config') self.config.set('Modbus_config', 'funcode', 'F4') self.config.set('Modbus_config', 'position', '5') self.config.add_section('DisHex_config') for i in range(num): idx = f'{i:02}' rowname = f'row{idx}' self.config.set('DisHex_config', rowname, '|||||0') self.config.add_section('Quick_config') for i in range(num): idx = f'{i:02}' button_name = f'Button{idx}' # 格式化按钮名称,确保两位数 self.config.set('Quick_config', button_name, '') with open(self.ini_path, 'w', encoding='utf-8') as f: self.config.write(f) def get_dishex_row(self, row): """获取十六进制显示的行""" idx = f'{row:02}' return self.config.get('DisHex_config', f'row{idx}') def set_dishex_row(self, row, value_str): """设置十六进制显示的行""" idx = f'{row:02}' self.config.set('DisHex_config', f'row{idx}', value_str) with open(self.ini_path, 'w', encoding='utf-8') as f: self.config.write(f) def get_quick_row(self, row): """获取快速发送的行""" index_str = f'{row:02}' # 确保编号为两位数字形式 button_name = f"button{index_str}" return self.config.get('Quick_config', button_name) def set_quick_buttion(self, row, value_str): """设置快速发送的行""" index_str = f'{row:02}' # 确保编号为两位数字形式 button_name = f"button{index_str}" self.config.set('Quick_config', button_name, value_str) with open(self.ini_path, 'w', encoding='utf-8') as f: self.config.write(f) def get_funcode(self): """获取功能码""" return self.config.get('Modbus_config', 'funcode') def get_position(self): """获取位置""" return int(self.config.get('Modbus_config', 'position')) def get_log_time(self): """获取日志时间""" return int(self.config.get('Sys_config', 'log_time')) def get_port(self): """获取串口""" return self.config.get('Serial_config', 'port') def get_baudrate(self): """获取波特率""" return self.config.get('Serial_config', 'baudrate') def get_parity(self): """获取奇偶校验""" return self.config.get('Serial_config', 'parity') def get_net_port(self): """获取网络端口""" return self.config.get('Net_config', 'port') def set_port(self, port_str): """设置串口""" self.config.set('Serial_config', 'port', port_str) with open(self.ini_path, 'w', encoding='utf-8') as f: self.config.write(f) def set_baudrate(self, baudrate_str): """设置波特率""" self.config.set('Serial_config', 'baudrate', baudrate_str) with open(self.ini_path, 'w', encoding='utf-8') as f: self.config.write(f) def set_parity(self, parity_str): """设置奇偶校验""" self.config.set('Serial_config', 'parity', parity_str) with open(self.ini_path, 'w', encoding='utf-8') as f: self.config.write(f) def set_net_port(self, port_str): """设置网络端口""" self.config.set('Net_config', 'port', port_str) with open(self.ini_path, 'w', encoding='utf-8') as f: self.config.write(f) def get_hex_send(self): """获取十六进制发送""" return self.config.get('Sys_config', 'hex_send') def set_hex_send(self, hex_send_str): """设置十六进制发送""" self.config.set('Sys_config', 'hex_send', hex_send_str) with open(self.ini_path, 'w', encoding='utf-8') as f: self.config.write(f) def get_hex_receive(self): """获取十六进制接收""" return self.config.get('Sys_config', 'hex_receive') def set_hex_receive(self, hex_receive_str): """设置十六进制接收""" self.config.set('Sys_config', 'hex_receive', hex_receive_str) with open(self.ini_path, 'w', encoding='utf-8') as f: self.config.write(f) def get_add_date(self): """获取日期""" return self.config.get('Sys_config', 'add_date') def set_add_date(self, add_date_str): """设置日期""" self.config.set('Sys_config', 'add_date', add_date_str) with open(self.ini_path, 'w', encoding='utf-8') as f: self.config.write(f) def get_cr_lf(self): """获取回车换行""" return self.config.get('Sys_config', 'cr_lf') def set_cr_lf(self, cr_lf_str): """设置回车换行""" self.config.set('Sys_config', 'cr_lf', cr_lf_str) with open(self.ini_path, 'w', encoding='utf-8') as f: self.config.write(f) def get_auto_sav_log(self): return self.config.get('Sys_config', 'auto_sav_log') def set_auto_sav_log(self, auto_sav_log_str): self.config.set('Sys_config', 'auto_sav_log', auto_sav_log_str) with open(self.ini_path, 'w', encoding='utf-8') as f: self.config.write(f) def get_auto_sav_db(self): return self.config.get('Sys_config', 'auto_sav_db') def set_auto_sav_db(self, auto_sav_db_str): self.config.set('Sys_config', 'auto_sav_db', auto_sav_db_str) with open(self.ini_path, 'w', encoding='utf-8') as f: self.config.write(f) def get_add_crc_rtu(self): return self.config.get('Sys_config', 'add_crc_rtu') def set_add_crc_rtu(self, add_crc_rtu_str): self.config.set('Sys_config', 'add_crc_rtu', add_crc_rtu_str) with open(self.ini_path, 'w', encoding='utf-8') as f: self.config.write(f)