From 4184e660563a45d75033fac454a93a8db42f5579 Mon Sep 17 00:00:00 2001 From: kyz Date: Fri, 4 Mar 2022 07:57:59 +0800 Subject: [PATCH] modify default cfg --- AlarmBoard.ioc | 2 + Core/Inc/user.h | 4 ++ Core/Src/main.c | 2 +- Core/Src/user_app.c | 25 ++++++-- Core/Src/user_config.c | 59 +++++++++-------- Core/Src/user_ftm.c | 62 +----------------- Core/Src/user_rs485.c | 2 +- modebus/port/porttimer.c | 3 +- py/{cfg_json.py => alarmboard_config.py} | 81 +++++++++++++----------- py/alarmboard_modbus.py | 2 - py/cfg_ui.py | 2 - 11 files changed, 103 insertions(+), 141 deletions(-) rename py/{cfg_json.py => alarmboard_config.py} (88%) diff --git a/AlarmBoard.ioc b/AlarmBoard.ioc index 72a2bea..c11f04c 100644 --- a/AlarmBoard.ioc +++ b/AlarmBoard.ioc @@ -6,6 +6,8 @@ CRC.GeneratingPolynomial=X12+X5+X0 CRC.IPParameters=DefaultPolynomialUse,DefaultInitValueUse,CRCLength,GeneratingPolynomial File.Version=6 GPIO.groupedBy=Group By Peripherals +IWDG.IPParameters=Prescaler +IWDG.Prescaler=IWDG_PRESCALER_16 KeepUserPlacement=false Mcu.Family=STM32G0 Mcu.IP0=CRC diff --git a/Core/Inc/user.h b/Core/Inc/user.h index 7927f8c..c32fb77 100644 --- a/Core/Inc/user.h +++ b/Core/Inc/user.h @@ -411,8 +411,12 @@ int32_t led_out_refresh( void ); int32_t device_input_info_get( DEV_IN_ST_Type * st ); int32_t tcp_modebus_handle( void ); +uint16_t crc16( uint8_t * data, uint16_t sz ); +void led_ch_flash( uint32_t i ); +void led_beep_refresh( void ); void remote_alarm_mute( void ); +void user_iwdg_refresh( void ); extern DEV_INFO_Type g_devinfo; extern DEV_Conf_Type g_dev_conf; diff --git a/Core/Src/main.c b/Core/Src/main.c index 204d6ef..a0d2228 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -240,7 +240,7 @@ static void MX_IWDG_Init(void) /* USER CODE END IWDG_Init 1 */ hiwdg.Instance = IWDG; - hiwdg.Init.Prescaler = IWDG_PRESCALER_4; + hiwdg.Init.Prescaler = IWDG_PRESCALER_16; hiwdg.Init.Window = 4095; hiwdg.Init.Reload = 4095; if (HAL_IWDG_Init(&hiwdg) != HAL_OK) diff --git a/Core/Src/user_app.c b/Core/Src/user_app.c index 0080624..8d4f958 100644 --- a/Core/Src/user_app.c +++ b/Core/Src/user_app.c @@ -355,6 +355,10 @@ int32_t alarm_check_group_input( uint32_t ch_type, uint32_t ch_mask ) int32_t alarm = INPUT_ST_NORMAL; int32_t i; int32_t ch_st = 0; + + if( ch_mask == 0 ){ + return INPUT_ST_NORMAL; + } for( i=0; i<32; i++ ){ if( ch_mask & (1< last + 10 ){ @@ -881,12 +897,7 @@ void user_app_start( void ) slave_device_ontimer(); } led_out_refresh(); - - if(HAL_IWDG_Refresh(&hiwdg) != HAL_OK) - { - /* Refresh Error */ - Error_Handler(); - } + user_iwdg_refresh(); } } } diff --git a/Core/Src/user_config.c b/Core/Src/user_config.c index 77e07d1..ad63323 100644 --- a/Core/Src/user_config.c +++ b/Core/Src/user_config.c @@ -7,13 +7,13 @@ const DEV_Conf_Type dev_config_default= { {//din - {ALARM_LED, ALARM_LEVEL_LOW, DELAY_50MS}, //1 - {ALARM_LED_BEEP, ALARM_LEVEL_LOW, DELAY_200MS}, //2 + {ALARM_LED_BEEP, ALARM_LEVEL_LOW, DELAY_1S}, //1 + {ALARM_LED_BEEP, ALARM_LEVEL_LOW, DELAY_1S}, //2 {ALARM_LED_BEEP, ALARM_LEVEL_LOW, DELAY_1S}, //3 - {ALARM_LED_BEEP, ALARM_LEVEL_LOW, DELAY_2S}, //4 - {ALARM_LED_BEEP, ALARM_LEVEL_LOW, DELAY_3S}, //5 - {ALARM_LED_BEEP, ALARM_LEVEL_LOW, DELAY_19S}, //6 - {ALARM_LED_BEEP, ALARM_LEVEL_LOW, DELAY_20S}, //7 + {ALARM_LED_BEEP, ALARM_LEVEL_LOW, DELAY_1S}, //4 + {ALARM_LED_BEEP, ALARM_LEVEL_LOW, DELAY_1S}, //5 + {ALARM_LED_BEEP, ALARM_LEVEL_LOW, DELAY_1S}, //6 + {ALARM_LED_BEEP, ALARM_LEVEL_LOW, DELAY_1S}, //7 {ALARM_LED_BEEP, ALARM_LEVEL_LOW, DELAY_1S}, //8 {ALARM_LED_BEEP, ALARM_LEVEL_LOW, DELAY_1S}, //9 {ALARM_LED_BEEP, ALARM_LEVEL_LOW, DELAY_1S}, //10 @@ -29,18 +29,18 @@ const DEV_Conf_Type dev_config_default= {ALARM_LED_BEEP, ALARM_LEVEL_LOW, DELAY_1S}, //20 {ALARM_LED_BEEP, ALARM_LEVEL_LOW, DELAY_1S}, //21 {ALARM_LED_BEEP, ALARM_LEVEL_LOW, DELAY_1S}, //22 - {ALARM_DISABLE, ALARM_LEVEL_LOW, DELAY_5S}, //23 - {ALARM_LED_BEEP, ALARM_LEVEL_LOW, DELAY_6S}, //24 + {ALARM_LED_BEEP, ALARM_LEVEL_LOW, DELAY_1S}, //23 + {ALARM_LED_BEEP, ALARM_LEVEL_LOW, DELAY_1S}, //24 }, {//ain - {ALARM_LED_BEEP, DELAY_1S, 0x0f, { 1000, 2000, 3000, 4000}}, //1 - {ALARM_LED_BEEP, DELAY_2S, 0x0f, { 1000, 2000, 3000, 4000}}, //1 - {ALARM_LED_BEEP, DELAY_3S, 0x0f, { 1000, 2000, 3000, 4000}}, //1 - {ALARM_LED_BEEP, DELAY_4S, 0x00, { 1000, 2000, 3000, 4000}}, //1 - {ALARM_LED_BEEP, DELAY_0, 0x00, { 1000, 2000, 3000, 4000}}, //1 - {ALARM_LED_BEEP, DELAY_2S, 0x00, { 1000, 2000, 3000, 4000}}, //1 - {ALARM_LED_BEEP, DELAY_3S, 0x00, { 1000, 2000, 3000, 4000}}, //1 - {ALARM_LED_BEEP, DELAY_1S, 0x0f, { 1000, 2000, 3000, 4000}}, //1 + {ALARM_DISABLE, DELAY_1S, 0x0f, { 1000, 2000, 3000, 4000}}, //1 + {ALARM_DISABLE, DELAY_1S, 0x0f, { 1000, 2000, 3000, 4000}}, //1 + {ALARM_DISABLE, DELAY_1S, 0x0f, { 1000, 2000, 3000, 4000}}, //1 + {ALARM_DISABLE, DELAY_1S, 0x0f, { 1000, 2000, 3000, 4000}}, //1 + {ALARM_DISABLE, DELAY_1S, 0x0f, { 1000, 2000, 3000, 4000}}, //1 + {ALARM_DISABLE, DELAY_1S, 0x0f, { 1000, 2000, 3000, 4000}}, //1 + {ALARM_DISABLE, DELAY_1S, 0x0f, { 1000, 2000, 3000, 4000}}, //1 + {ALARM_DISABLE, DELAY_1S, 0x0f, { 1000, 2000, 3000, 4000}}, //1 }, {//alarm { ALARM_TYPE_SINGLE, LED1, DIN_CH_1 }, //1 @@ -59,26 +59,25 @@ const DEV_Conf_Type dev_config_default= { ALARM_TYPE_SINGLE, LED14, DIN_CH_14 }, //14 { ALARM_TYPE_SINGLE, LED15, DIN_CH_15 }, //15 { ALARM_TYPE_SINGLE, LED16, DIN_CH_16 }, //16 - { ALARM_TYPE_GROUP_AND, LED17, (1<<16)|(1<<17)|(1<<18) }, //17 - { ALARM_TYPE_GROUP_OR, LED18, (1<<19)|(1<<20)|(1<<21) }, //17 - { ALARM_TYPE_SINGLE, LED19, AIN_CH_1 }, //19 - { ALARM_TYPE_SINGLE, LED20, AIN_CH_2 }, //20 - { ALARM_TYPE_SINGLE, LED21, AIN_CH_3 }, //21 - { ALARM_TYPE_SINGLE, LED22, AIN_CH_4 }, //22 - { ALARM_TYPE_SINGLE, LED23, AIN_CH_5 }, //23 - { ALARM_TYPE_DEVICE, LED24, 0 }, //24 + { ALARM_TYPE_SINGLE, LED16, DIN_CH_17 }, //16 + { ALARM_TYPE_SINGLE, LED16, DIN_CH_18 }, //16 + { ALARM_TYPE_SINGLE, LED19, DIN_CH_19 }, //19 + { ALARM_TYPE_SINGLE, LED20, DIN_CH_20 }, //20 + { ALARM_TYPE_SINGLE, LED21, DIN_CH_21 }, //21 + { ALARM_TYPE_SINGLE, LED22, DIN_CH_22 }, //22 + { ALARM_TYPE_SINGLE, LED23, DIN_CH_23 }, //23 + { ALARM_TYPE_SINGLE, LED24, DIN_CH_24 }, //24 }, {//repeater - 0x00f0ffff, //x8 - 0x87, //enable 1--8 + 0x00ffffff, //x8 + 0xff, //enable 1--8 }, { //VDR - {'x','X'}, //head + {'-','-'}, //head B4800, //baudrate - 7, //period 30s - 0x00F0FFF3, //ch mask + 5, //period 30s + 0x00ffffff, //ch mask }, - 0x12345678, //sinature }; diff --git a/Core/Src/user_ftm.c b/Core/Src/user_ftm.c index b4ab933..fec8e28 100644 --- a/Core/Src/user_ftm.c +++ b/Core/Src/user_ftm.c @@ -112,62 +112,6 @@ ftm_pkt_t * ftm_get_pkt( ftm_pkt_t * pkt ) return NULL; } -uint16_t read_device_cfg_reg( uint16_t regaddr ); -void ftm_handle_pkt( ftm_pkt_t *pkt ) -{ - uint16_t reg_start; - uint16_t reg_n; - uint16_t reg_v; - int i; - -uint8_t tx_buf[200]; - - dbg_printf( "get_pkt\r\n" ); - - switch( pkt->cmd ){ - case 0x0001: //read cfg - reg_start = (pkt->data[0]<<8)|pkt->data[1]; - reg_n = (pkt->data[2]<<8)|pkt->data[3]; - - break; - case 0x0002: //write cfg - reg_start = (pkt->data[0]<<8)|pkt->data[1]; - reg_n = (pkt->data[2]<<8)|pkt->data[3]; - for( i=0; idata[4+i*2] = (reg_v>>8)&0xff; - pkt->data[4+i*2+1] = (reg_v>>8)&0xff; - } - tx_buf[0] = 0xf0; - tx_buf[1] = 0xf0; - tx_buf[2] = reg_n *4 +12; - tx_buf[2] = reg_n *4 +12; - - - break; - } - - UART2_Recv_Restart(); //clear recv buf; - return; -} - -void ftm_ontimer( void ) -{ - ftm_pkt_t pkt; - static uint32_t n = 0; - - if( n++>=200 ){ - n = 0; - //UART2_Send( "test\r\n", 6 ); - } - - if( ftm_get_pkt( &pkt ) ){ - ftm_handle_pkt( &pkt ); - - } -} - - void ftm_start( void ) { uint32_t last, now; @@ -184,17 +128,17 @@ void ftm_start( void ) UART2_Recv_Start(); //HAL_IWDG_Refresh(&hiwdg); - eMBInit( MB_RTU, 1, 2, 9600, 0 ); + eMBInit( MB_RTU, 1, 2, 9600, MB_PAR_NONE ); eMBEnable(); while(1){ - + now = HAL_GetTick(); if( now > last + 10 ){ last += 10; - //ftm_ontimer(); led_beep_refresh(); led_out_refresh(); eMBPoll( ); + user_iwdg_refresh(); } { void virtual_timer_isr( void ); diff --git a/Core/Src/user_rs485.c b/Core/Src/user_rs485.c index 66f13c6..540606a 100644 --- a/Core/Src/user_rs485.c +++ b/Core/Src/user_rs485.c @@ -47,7 +47,7 @@ int32_t UART2_Send( uint8_t *data, uint16_t data_size ) Uart2Ready = RESET; RS485_RTS1_TX(); - HAL_Delay(10); + HAL_Delay(1); if (HAL_UART_Transmit_IT(&huart2, data, data_size) != HAL_OK) { Error_Handler(); diff --git a/modebus/port/porttimer.c b/modebus/port/porttimer.c index ea75e6d..c1d4d15 100644 --- a/modebus/port/porttimer.c +++ b/modebus/port/porttimer.c @@ -72,10 +72,11 @@ prvvMBPortTimerISR( ) } +extern void uart_recv_reset( void ); void virtual_timer_isr( void ) { if( timer_expired && timer_expired < HAL_GetTick() ){ xMBRTUTimerT35Expired(); uart_recv_reset(); } -} \ No newline at end of file +} diff --git a/py/cfg_json.py b/py/alarmboard_config.py similarity index 88% rename from py/cfg_json.py rename to py/alarmboard_config.py index 4bc3397..afe4cee 100644 --- a/py/cfg_json.py +++ b/py/alarmboard_config.py @@ -285,11 +285,14 @@ class cfg_json: messagebox.showwarning(title='', message='串口访问失败') except : messagebox.showwarning(title='', message='读取设备参数失败') - raise + #raise else: #v = dev.read_devinfo_regs( mbdev) #print( v ) pass + if not mbdev: + print( "mbtcp_master close ....") + mbdev.close() def on_btn_writetodev( self ): try: @@ -305,19 +308,19 @@ class cfg_json: #json_data = json.loads( json_str) #write din json_data = self.cfg_data - reg_values=[0]*24 + din_reg_values=[0]*24 for dx in json_data['din']: ch = dx["ch"]-1 mode = self.input_mode_cfg_value( dx['mode'] ) level = self.din_level_cfg_value(dx['level']) delay = self.delay_cfg_value( dx['delay']) cfg = dev.din_ch_cfg(idx=ch, mode=mode, level=level, delay=delay) - reg_values[ch]=cfg.to_modbus_reg() + din_reg_values[ch]=cfg.to_modbus_reg() mbdev = dev.connect() - dev.write_regs(8000+0, reg_values, mbdev) - #print( "DIN V:", reg_values) + dev.write_regs(8000+0, din_reg_values, mbdev) + #print( "DIN V:", din_reg_values) #write ain - reg_values=[0]*5*8 + ain_reg_values=[0]*5*8 for ax in json_data['ain']: ch = ax["ch"]-1 mode = self.input_mode_cfg_value( ax['mode'] ) @@ -326,16 +329,16 @@ class cfg_json: delay = self.delay_cfg_value( ax['delay']) cfg = dev.ain_ch_cfg(idx=ch, mode=mode, delay=delay, mask=mask, level=level) v = cfg.to_modbus_reg() - reg_values[ch*5+0] = v[0] - reg_values[ch*5+1] = v[1] - reg_values[ch*5+2] = v[2] - reg_values[ch*5+3] = v[3] - reg_values[ch*5+4] = v[4] + ain_reg_values[ch*5+0] = v[0] + ain_reg_values[ch*5+1] = v[1] + ain_reg_values[ch*5+2] = v[2] + ain_reg_values[ch*5+3] = v[3] + ain_reg_values[ch*5+4] = v[4] #dev.write_ain_cfg_regs(ch, [cfg]) - dev.write_regs(8000+50, reg_values, mbdev) - print( "AIN V:", reg_values) + dev.write_regs(8000+50, ain_reg_values, mbdev) + print( "AIN V:", ain_reg_values) #write alarm - reg_values=[0]*3*24 + alarm_reg_values=[0]*3*24 for ax in json_data['alarm']: ch = ax["ch"]-1 mode = self.alarm_mode_cfg_value( ax['mode'] ) @@ -344,46 +347,46 @@ class cfg_json: cfg = dev.alarm_ch_cfg(idx=ch, mode=mode, led=led, mask=mask) #dev.write_alarm_cfg_regs(ch, [cfg]) v = cfg.to_modbus_reg() - reg_values[ch*3+0] = v[0] - reg_values[ch*3+1] = v[1] - reg_values[ch*3+2] = v[2] - dev.write_regs(8000+100, reg_values, mbdev) - print( "ALAM V:", reg_values) + alarm_reg_values[ch*3+0] = v[0] + alarm_reg_values[ch*3+1] = v[1] + alarm_reg_values[ch*3+2] = v[2] + dev.write_regs(8000+100, alarm_reg_values, mbdev) + print( "ALAM V:", alarm_reg_values) #repeater - slave_mask = 0 + repeater_slave_mask = 0 for slave in json_data['repeater']["slave"]: - slave_mask |= (1<<(slave-1)) - ch_mask = 0 + repeater_slave_mask |= (1<<(slave-1)) + repeater_ch_mask = 0 for ch in json_data['repeater']['ch']: - ch_mask |= (1<<(ch-1)) - dev.write_repeater_cfg_regs(slave_mask, ch_mask, mbdev) + repeater_ch_mask |= (1<<(ch-1)) + dev.write_repeater_cfg_regs(repeater_slave_mask, repeater_ch_mask, mbdev) #vdr prefix = json_data['vdr']['prefix'] - prefix = int.from_bytes( prefix.encode('utf8'), 'big') - period = json_data['vdr']['period'] + vdr_prefix = int.from_bytes( prefix.encode('utf8'), 'big') + vdr_period = json_data['vdr']['period'] baud = json_data['vdr']['baud'] if baud == 4800: - baud = 0 + vdr_baud = 0 elif baud == 9600: - baud = 1 + vdr_baud = 1 elif baud == 14400: - baud = 2 + vdr_baud = 2 elif baud == 19200: - baud = 3 + vdr_baud = 3 elif baud == 38400: - baud = 4 + vdr_baud = 4 elif baud == 57600: - baud = 5 + vdr_baud = 5 elif baud == 115200: - baud = 6 + vdr_baud = 6 else: - baud = 0 + vdr_baud = 0 - ch_mask = 0 + vdr_ch_mask = 0 for ch in json_data['vdr']['ch']: - ch_mask |= (1<<(ch-1)) + vdr_ch_mask |= (1<<(ch-1)) - dev.write_vdr_cfg_regs( prefix=prefix, period=period, baud=baud, ch_mask=ch_mask, master=mbdev ) + dev.write_vdr_cfg_regs( prefix=vdr_prefix, period=vdr_period, baud=vdr_baud, ch_mask=vdr_ch_mask, master=mbdev ) dev.write_regs( 8300, [0x0000], mbdev ) #save cfg to flash #TODO debug ... except (TimeoutError,socket.timeout, modbus_tk.exceptions.ModbusInvalidResponseError): @@ -396,9 +399,11 @@ class cfg_json: messagebox.showwarning(title='', message='参数异常') except : messagebox.showwarning(title='', message='写入设备参数失败') - raise else: pass + if mbdev: + print( "mbtcp_master close ....") + mbdev.close() if __name__ == '__main__': root = Tk() diff --git a/py/alarmboard_modbus.py b/py/alarmboard_modbus.py index 43f58a9..a0a6a21 100644 --- a/py/alarmboard_modbus.py +++ b/py/alarmboard_modbus.py @@ -19,8 +19,6 @@ tcp_port=10000 uart_port='COM5' uart_baud='9600' -connect=0 - def connect(): #Connect to the slave if modbus_dev == 'TCP': diff --git a/py/cfg_ui.py b/py/cfg_ui.py index 7152f75..5dbac8a 100644 --- a/py/cfg_ui.py +++ b/py/cfg_ui.py @@ -1,8 +1,6 @@ from tkinter import * from tkinter import ttk -import cfg_test -import cfg_json # ALARM alarm_mode_table = [u"禁用报警", u"单路报警", u"组合报警与", u"组合报警或", u'故障报警'] -- 2.34.1