From a44f752ca3d350c8fff1a86b22aa233c661e079a Mon Sep 17 00:00:00 2001 From: kyz Date: Wed, 11 Dec 2024 16:19:04 +0800 Subject: [PATCH] =?utf8?q?=E4=BF=AE=E6=94=B9RS485=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 修改RS485接口功能 移除MCU MQTT有关代码 修改IRIG B码数据处理 --- .../Modbus_EquipmentData/Get_EquipmentData.c | 6 +- STM32/code/app/App_RS485_1.c | 32 ++-- STM32/code/app/App_irig_b.c | 173 ++++++++++++++---- STM32/code/app/main_task.c | 14 +- STM32/code/flat/usart.c | 2 +- STM32/code/mcu_com/mcu_com.c | 4 +- STM32/code/mcu_com/mcu_com_hard.c | 4 +- 7 files changed, 162 insertions(+), 73 deletions(-) diff --git a/STM32/code/Modbus_EquipmentData/Get_EquipmentData.c b/STM32/code/Modbus_EquipmentData/Get_EquipmentData.c index 9f9853e0..9760a2e3 100644 --- a/STM32/code/Modbus_EquipmentData/Get_EquipmentData.c +++ b/STM32/code/Modbus_EquipmentData/Get_EquipmentData.c @@ -652,7 +652,7 @@ uint8_t Read_Register(uint16_t addr, uint16_t len, uint8_t *pData,uint8_t *pos) *pData = DataLen; return DataLen; } - +#if 0 /* mqtt͸´« */ uint8_t Mqtt_Through_Analysis(uint8_t *pData, uint16_t len, uint8_t type_t, uint8_t sub_type) { @@ -730,7 +730,7 @@ uint8_t Mqtt_Through_Analysis(uint8_t *pData, uint16_t len, uint8_t type_t, uint } } } - +#endif uint8_t Modbus_Protocol_Analysis(uint8_t *pData, uint16_t len) { @@ -808,6 +808,7 @@ uint8_t Modbus_Protocol_Analysis(uint8_t *pData, uint16_t len) return false; } +#if 0 //-------------------------------------------------------------------------------------------------------// //485Á½Â·ÏÂÐж˿Ú͸´« //ÉÏÐÐ485½Ó¿ÚÊÕµ½modbusÖ¸Áת·¢µ½485Á½Â·ÏÂÐнӿڣ¬ °´ÕÕµØÖ·ÇøÊ¶±ð @@ -971,3 +972,4 @@ void Task_RS485_2_3_passthrouh( void ) } } } +#endif \ No newline at end of file diff --git a/STM32/code/app/App_RS485_1.c b/STM32/code/app/App_RS485_1.c index 5ece7b64..d193d101 100644 --- a/STM32/code/app/App_RS485_1.c +++ b/STM32/code/app/App_RS485_1.c @@ -9,32 +9,32 @@ #include "protocol_modbus_rtu.h" #include "Rn8302_cal.h" -static BYTE rs4851_probuf[1024]; +static BYTE rs485_2_probuf[1024]; uint16_t pLocalCnt = 0; uint8_t pLocalTestData[100] = {0}; uint8_t pD645AckBuff[200] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25}; /* - RS485-1¶Ë¿Ú¼ÓÔØ¹æÔ¼ + RS485-2¶Ë¿Ú¼ÓÔØ¹æÔ¼ */ -void Task_RS485_1_sProtocol() +void Task_RS485_2_sProtocol() { BYTE *frame; WORD len; -#if 0 - /* RS485-1¼ÓÔØ698¹æÔ¼ */ - if (Port_Get_One_Rx_Frame(PORT_UART_IFACE_RS4851, &frame, &len)) // ²éѯ»ñÈ¡RS-4851¶Ë¿ÚFIFOÖеÄÊý¾Ý + + /* RS485-2¼ÓÔØ698¹æÔ¼ */ + if (Port_Get_One_Rx_Frame(PORT_UART_IFACE_RS4852, &frame, &len)) // ²éѯ»ñÈ¡RS-4851¶Ë¿ÚFIFOÖеÄÊý¾Ý { WORD txlen; - if (Dlt698_45_protocol(frame, len, rs4851_probuf, sizeof(rs4851_probuf), &txlen) == TRUE)//Éý¼¶Îļþ + if (Dlt698_45_protocol(frame, len, rs485_2_probuf, sizeof(rs485_2_probuf), &txlen) == TRUE)//Éý¼¶Îļþ { - Port_tran_One_Frame(PORT_UART_IFACE_RS4851, rs4851_probuf, txlen); // RS-4851¶Ë¿Ú·¢ËÍÍêÕûµÄÒ»Ö¡Êý¾Ý + Port_tran_One_Frame(PORT_UART_IFACE_RS4852, rs485_2_probuf, txlen); // RS-4852¶Ë¿Ú·¢ËÍÍêÕûµÄÒ»Ö¡Êý¾Ý } - else if (protocol_Modbus_RTU(frame, len, rs4851_probuf, &txlen) == TRUE) // MODBUS¹æÔ¼´¦Àí + else if (protocol_Modbus_RTU(frame, len, rs485_2_probuf, &txlen) == TRUE) // MODBUS¹æÔ¼´¦Àí { - Port_tran_One_Frame(PORT_UART_IFACE_RS4851, rs4851_probuf, txlen); + Port_tran_One_Frame(PORT_UART_IFACE_RS4852, rs485_2_probuf, txlen); } else if( (*frame == 0xFE) && (*(frame+1) == 0xFE) && @@ -47,12 +47,12 @@ void Task_RS485_1_sProtocol() memcpy(pD645AckBuff, frame+4, len-4); //send ack to host app - Port_tran_One_Frame(PORT_UART_IFACE_RS4851, pD645AckBuff, len-4); + Port_tran_One_Frame(PORT_UART_IFACE_RS4852, pD645AckBuff, len-4); } } - else if(Modbus_Protocol_Passthrouh_Analysis(frame,len))//͸´«µ½ Á½Â·ÏÂÐÐ485½Ó¿Ú - { - } +// else if(Modbus_Protocol_Passthrouh_Analysis(frame,len))//͸´«µ½ Á½Â·ÏÂÐÐ485½Ó¿Ú +// { +// } else if(Modbus_Protocol_Analysis(frame,len))//µÚÈý·½ºǫ́modbusЭÒé { } @@ -62,10 +62,6 @@ void Task_RS485_1_sProtocol() } } -#else - -#endif - } uint8_t send_bf=0; diff --git a/STM32/code/app/App_irig_b.c b/STM32/code/app/App_irig_b.c index 3250ee23..19135b2a 100644 --- a/STM32/code/app/App_irig_b.c +++ b/STM32/code/app/App_irig_b.c @@ -57,7 +57,7 @@ void IRIG_B_Time_Scan( void ) uint32_t curr_level; //USART2_RXD / GPD6 - curr_level = HAL_GPIO_ReadPin( GPIOD, 1<<6 )==GPIO_PIN_SET ? 1: 0; + curr_level = HAL_GPIO_ReadPin( GPIOD, GPIO_PIN_6 )==GPIO_PIN_SET ? 1: 0; if( rx_start == 0 ){ if( last_level == 0 && curr_level == 1 ){ @@ -90,12 +90,17 @@ void IRIG_B_Time_Scan( void ) #endif } +#define TIME_BIT_P 2 +#define TIME_BIT_0 0 +#define TIME_BIT_1 1 +#define TIME_BIT_E -1 + //¶Áȡһ¸öbit£¬ ·µ»Ø ´íÎó£º-1 £¬ 0£ºÊý¾Ý0 2/8 1£ºÊý¾Ý1 5/5£¬ 2£ºP 8/2 int32_t IRIG_B_Time_get_bit( void ) { int h_time = 0; int l_time = 0; - int ret = -1; + int ret = TIME_BIT_E; //¼ÆËã¸ßµçƽʱ¼ä while( rd_n < wr_n && irig_b_rx_buf[rd_n&TIME_BUF_SIZE_MASK] == 1 ){ @@ -106,78 +111,170 @@ int32_t IRIG_B_Time_get_bit( void ) while( rd_n < wr_n && irig_b_rx_buf[rd_n&TIME_BUF_SIZE_MASK] == 0 ){ rd_n++; l_time++; + if( h_time+l_time > 10 ) + break; } if( h_time>0 && h_time + l_time >=9 && h_time + l_time <= 11 ){ - if( h_time <3 ) - ret = 0; // 2ms + 8ms - 0 - else if( h_time <6 ) - ret = 1; //5ms + 5ms - 1 + if( h_time <4 ) + ret = TIME_BIT_0; // 2ms + 8ms - 0 + else if( h_time <7 ) + ret = TIME_BIT_1; //5ms + 5ms - 1 else if( h_time <9 ) - ret = 2; //8ms + 2ms - P + ret = TIME_BIT_P; //8ms + 2ms - P } return ret; } + //¶Áȡһ¸öByte£¬ Õý³££º·µ»Ø BCDÂ룬 ´íÎ󣺷µ»Ø-1 -int32_t IRIG_B_Time_get_byte( void ) +int32_t IRIG_B_Time_get_byte( uint8_t first ) { uint8_t rx_byte = 0; int32_t b; - //P - if( (b = IRIG_B_Time_get_bit()) != 2) - return -1; - //Bit0 - if( (b = IRIG_B_Time_get_bit()) < 0 || b == 2) - return -1; - rx_byte |= b?1:0; + if( (b = IRIG_B_Time_get_bit()) == TIME_BIT_E || b == TIME_BIT_P) + return TIME_BIT_E; + + if(b==TIME_BIT_1) + rx_byte |= 1; //Bit1 - if( (b = IRIG_B_Time_get_bit()) < 0 || b == 2) - return -1; - rx_byte |= b?2:0; + if( (b = IRIG_B_Time_get_bit()) == TIME_BIT_E || b == TIME_BIT_P) + return TIME_BIT_E; + + if(b==TIME_BIT_1) + rx_byte |= 2; //Bit2 - if( (b = IRIG_B_Time_get_bit()) < 0 || b == 2) - return -1; - rx_byte |= b?4:0; + if( (b = IRIG_B_Time_get_bit()) == TIME_BIT_E || b == TIME_BIT_P) + return TIME_BIT_E; + + if(b==TIME_BIT_1) + rx_byte |= 4; //Bit3 - if( (b = IRIG_B_Time_get_bit()) < 0 || b == 2) - return -1; - rx_byte |= b?8:0; + if( (b = IRIG_B_Time_get_bit()) == TIME_BIT_E || b == TIME_BIT_P) + return TIME_BIT_E; + + if(b==TIME_BIT_1) + rx_byte |= 8; //0 if( (b = IRIG_B_Time_get_bit()) < 0 || b != 0) return -1; - rx_byte |= b?1:0; + //Bit4 + if( (b = IRIG_B_Time_get_bit()) == TIME_BIT_E || b == TIME_BIT_P) + return TIME_BIT_E; + + if(b==TIME_BIT_1) + rx_byte |= 0x10; + + //Bit5 + if( (b = IRIG_B_Time_get_bit()) == TIME_BIT_E || b == TIME_BIT_P) + return TIME_BIT_E; + + if(b==TIME_BIT_1) + rx_byte |= 0x20; + + //Bit6 + if( (b = IRIG_B_Time_get_bit()) == TIME_BIT_E || b == TIME_BIT_P) + return TIME_BIT_E; + + if(b==TIME_BIT_1) + rx_byte |= 0x40; + + if( first == 0 ){ + //Bit7 + if( (b = IRIG_B_Time_get_bit()) == TIME_BIT_E || b == TIME_BIT_P) + return TIME_BIT_E; + + if(b==TIME_BIT_1) + rx_byte |= 0x80; + } + return rx_byte; } + void IRIG_B_Time_Init( void ) { - //HAL_GPIO_WritePin( GPIOD, PIN(4), GPIO_PIN_RESET ); + GPIO_InitTypeDef GPIO_InitStruct = {0}; + //GPIO PD6 + GPIO_InitStruct.Pin = GPIO_PIN_6; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); } void IRIG_B_Time_Task( void ) { #if 0 - uint8_t data[10]; - if( wr_n - rd_n >= 1000 ){ - //½âÎöÊý¾Ý + + uint32_t sec; + uint32_t min; + uint32_t hour; + uint32_t day; + uint32_t year; + + uint8_t data[10]; + int32_t rx_byte[10]; + while( wr_n - rd_n >= 1000 ){ - if( get_bit() == TIME_P ){ //P0 - for( int i=0; i<10; i++ ){ - get_bit() == TEIM_P - data[i] = get_byte(); - } - } - }else if( rx_start == 0 ){ - rd_n = wr_n; - } + if( IRIG_B_Time_get_bit() == TIME_BIT_P ){ //P0 + if( IRIG_B_Time_get_bit() == TIME_BIT_P ){ //PR + rx_byte[0] = IRIG_B_Time_get_byte(1); + if( rx_byte[0] == TIME_BIT_E ) + break; + if( IRIG_B_Time_get_bit() != TIME_BIT_P ) //P1 + break; + if( (rx_byte[1] = IRIG_B_Time_get_byte(0)) == TIME_BIT_E ) + break; + if( IRIG_B_Time_get_bit() != TIME_BIT_P ) //P2 + break; + if( (rx_byte[2] = IRIG_B_Time_get_byte(0)) == TIME_BIT_E ) + break; + if( IRIG_B_Time_get_bit() != TIME_BIT_P ) //P3 + break; + if( (rx_byte[3] = IRIG_B_Time_get_byte(0)) == TIME_BIT_E ) + break; + if( IRIG_B_Time_get_bit() != TIME_BIT_P ) //P4 + break; + if( (rx_byte[4] = IRIG_B_Time_get_byte(0)) == TIME_BIT_E ) + break; + if( IRIG_B_Time_get_bit() != TIME_BIT_P ) //P5 + break; + if( (rx_byte[5] = IRIG_B_Time_get_byte(0)) == TIME_BIT_E ) + break; + if( IRIG_B_Time_get_bit() != TIME_BIT_P ) //P6 + break; + if( (rx_byte[6] = IRIG_B_Time_get_byte(0)) == TIME_BIT_E ) + break; + if( IRIG_B_Time_get_bit() != TIME_BIT_P ) //P7 + break; + if( (rx_byte[7] = IRIG_B_Time_get_byte(0)) == TIME_BIT_E ) + break; + if( IRIG_B_Time_get_bit() != TIME_BIT_P ) //P8 + break; + if( (rx_byte[8] = IRIG_B_Time_get_byte(0)) == TIME_BIT_E ) + break; + if( IRIG_B_Time_get_bit() != TIME_BIT_P ) //P9 + break; + if( (rx_byte[9] = IRIG_B_Time_get_byte(0)) == TIME_BIT_E ) + break; + //»ñµÃһ֡ʱ¼äÊý¾Ý + sec = ((rx_byte[0]>>4)&0x0f)*10 + (rx_byte[0]&0x0f); + min = ((rx_byte[1]>>4)&0x0f)*10 + (rx_byte[1]&0x0f); + hour = ((rx_byte[2]>>4)&0x0f)*10 + (rx_byte[2]&0x0f); + day = ((rx_byte[3]>>4)&0x0f)*10 + (rx_byte[3]&0x0f) + (rx_byte[4]&0x0f)*100 ; + year = ((rx_byte[5]>>4)&0x0f)*10 + (rx_byte[5]&0x0f); + printf( "get time %d, %d, %d, %d\n", sec, hour, day, year ); + } + } + } #endif } diff --git a/STM32/code/app/main_task.c b/STM32/code/app/main_task.c index 7d76bc4c..ef4633d2 100644 --- a/STM32/code/app/main_task.c +++ b/STM32/code/app/main_task.c @@ -147,6 +147,7 @@ extern void IRIG_B_Time_Task( void ); extern float get_ambient_temperature( void ); extern float get_ambient_humidity( void ); extern void Task_RS485_1_tem_sProtocol( void ); +extern void Task_RS485_2_sProtocol( void ); void Task_main() @@ -171,21 +172,12 @@ void Task_main() Task_Adjust_Daemon(); -/// Run_RN8302B_Task(); +// Run_RN8302B_Task(); Run_RN2026_Task(); - //Task_RS485_1_sProtocol(); /* RS485-1´Ó¹æÔ¼(GDW698/MODBUS) */ + Task_RS485_2_sProtocol(); /* RS485-2´Ó¹æÔ¼(GDW698/MODBUS) */ Task_RS485_1_tem_sProtocol(); //»·¾³ÎÂʪ¶È¼ì²â -#if 0 - Task_RS485_2_3_mProtocol(); /* RS485-2/3Ö÷¶¯¹æÔ¼(MODBUS) */ -#else - { - extern void Task_RS485_2_3_passthrouh( void ); - Task_RS485_2_3_passthrouh(); - } -#endif - Task_LAN_Com(); Task_System_Weihu(); diff --git a/STM32/code/flat/usart.c b/STM32/code/flat/usart.c index ecd53dd7..32be03bc 100644 --- a/STM32/code/flat/usart.c +++ b/STM32/code/flat/usart.c @@ -431,7 +431,7 @@ void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle) PD5 ------> USART2_TX PD6 ------> USART2_RX */ - GPIO_InitStruct.Pin = GPIO_PIN_5 /* | GPIO_PIN_6*/; + GPIO_InitStruct.Pin = GPIO_PIN_5 | GPIO_PIN_6; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; diff --git a/STM32/code/mcu_com/mcu_com.c b/STM32/code/mcu_com/mcu_com.c index 73ead236..00f190b1 100644 --- a/STM32/code/mcu_com/mcu_com.c +++ b/STM32/code/mcu_com/mcu_com.c @@ -1228,6 +1228,7 @@ void Task_Buzzer(void) } } +#if 0 //发送modbus寄存器信息 void Mqtt_send_modbus(uint8_t type, uint8_t sub_type, uint8_t *data, uint8_t DataLen) { @@ -1731,9 +1732,8 @@ void Mqtt_Proactive_Escalation(void) rpt_item++; } #endif - } - +#endif //发送mcu软件版本信息 void send_mcuSoftWare_Version(void) { diff --git a/STM32/code/mcu_com/mcu_com_hard.c b/STM32/code/mcu_com/mcu_com_hard.c index 343fa883..6536552d 100644 --- a/STM32/code/mcu_com/mcu_com_hard.c +++ b/STM32/code/mcu_com/mcu_com_hard.c @@ -358,7 +358,7 @@ void ComHard_Uart2_Analy(void) p += 2; if(*p) { - Mqtt_Proactive_Escalation_battery(); +// Mqtt_Proactive_Escalation_battery(); HAL_Delay(3000); Battery_SwitchEnable(0); @@ -422,6 +422,7 @@ void ComHard_Uart2_Analy(void) break; } break; +#if 0 case MQTT_PASSTHROUGH_SET: p += 2; if(sub_type == Passthrough_485_2 || sub_type == Passthrough_485_3) @@ -434,6 +435,7 @@ void ComHard_Uart2_Analy(void) } break; +#endif case SET_ELECTRICAL_RATE: p += 2; -- 2.34.1