Forráskód Böngészése

[protocol megatec] rewrite receive answer from UPS

balbekova 5 éve
szülő
commit
9a1847564b

+ 25 - 10
modules/MegaTec/megatec.c

@@ -141,25 +141,39 @@ void send_MegaTec_cmd(cmdMegaTecEnums_t command)
 bool ups_megatec_rx_pdu(void)
 {
 	int c = 0;
+	uint8_t cnt_answer = 0;
 
 	ups_pdu.len = 0;
 
-	while ((ups_pdu.len < UPS_PDU_MAX_LEN) && (c != 0x0d)) {
-		c = ups_getchar(100);//portMAX_DELAY200
-		if(c < 0)
-		{
+	while(c >= 0){
+		c = ups_getchar(200);//portMAX_DELAY200
+		if(c >= 0){
 			ups_pdu.len = 0;
-			break;
+			memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
+			ups_pdu.data[ups_pdu.len++] = c;
+			while ((ups_pdu.len < UPS_PDU_MAX_LEN) && (c != 0x0d)) {
+				c = ups_getchar(100);//portMAX_DELAY200
+				if(c < 0)
+				{
+					ups_pdu.len = 0;
+					return false;
+				}
+				ups_pdu.data[ups_pdu.len++] = c;
+			}
+		}
+		else{
+			if(ups_pdu.len == 0){
+				return false;
+			}
+		}
+		if(++cnt_answer >= 5){
+			return false;
 		}
-		ups_pdu.data[ups_pdu.len++] = c;
 	}
 
 	DBG printf("UPS raw data: %s\r\n", ups_pdu.data);
 	DBG printf("UPS raw data len: %d\r\n", ups_pdu.len);
 
-	if (ups_pdu.len == 0)
-		return false;
-
 	return true;
 }
 
@@ -671,7 +685,7 @@ void request_task(void* params)
 	uint8_t num_req = 0;
 	uint8_t *req;
 
-		ups_megatec_rx_pdu();
+		//ups_megatec_rx_pdu();
 		for(;;)
 		{
 			if(UPS.Present == true){
@@ -693,6 +707,7 @@ void request_task(void* params)
 
 						//memset(UPS.model, 0, 11);
 						memset(UPS.vertion, 0, 11);
+						init_ups_rbuf();
 					}
 				}
 			}

+ 2 - 0
peripheral_modules/inc/usart.h

@@ -31,6 +31,8 @@
 #define	UPS_USART_PARITY			USART_Parity_No
 #define UPS_USART_STOP_BIT			USART_StopBits_1
 
+
+void init_ups_rbuf(void);
 /**
   * @brief  
   */

+ 6 - 0
peripheral_modules/src/usart.c

@@ -104,6 +104,12 @@ void uart_hw_init(USART_TypeDef *uart, uint32_t baud, uint16_t wordlen, uint16_t
     }
 }
 
+void init_ups_rbuf(void)
+{
+	rbuf32_init(&ups_tx_rbuf, ups_tx_fifo, UPS_RBUF_SIZE);
+	rbuf32_init(&ups_rx_rbuf, ups_rx_fifo, UPS_RBUF_SIZE);
+}
+
 void InitUSART(void) {
 	xSem_rx_buf = xSemaphoreCreateCounting( UPS_RBUF_SIZE, 0 );
 	rbuf32_init(&ups_tx_rbuf, ups_tx_fifo, UPS_RBUF_SIZE);