Ver Fonte

add test request and shutdown

balbekova há 8 anos atrás
pai
commit
62260b7821
2 ficheiros alterados com 56 adições e 8 exclusões
  1. 51 8
      modules/MegaTec/megatec.c
  2. 5 0
      modules/MegaTec/megatec.h

+ 51 - 8
modules/MegaTec/megatec.c

@@ -20,6 +20,10 @@
 
 #define UPS_PDU_MAX_LEN 50
 
+uint16_t TimeParam = 0;
+uint16_t TimeParam2 = 0;
+bool megatec_send = true;
+
 enum {
 	NORMAL = 0x00,
 	VER_ERROR = 0x01,
@@ -71,13 +75,21 @@ void init_UPS_value(void)
 void send_MegaTec_cmd(cmdMegaTecEnums_t command)
 {
 	if(command == ups_test_time){
+		uint8_t req[10];
 
+		memset(req, 0, 10);
+		sprintf(req, "%s%d\r", MegaTecCMD[command], TimeParam);
+		ups_send_block(req, strlen(req));
 	}
 	else if(command == ups_shutdown){
 
 	}
 	else if(command == ups_shutdown_restore){
+		uint8_t req[10];
 
+		memset(req, 0, 10);
+		sprintf(req, "%s.%d%s%d\r", MegaTecCMD[command-1], TimeParam, MegaTecCMD[command], TimeParam2);
+		ups_send_block(req, strlen(req));
 	}
 	else{
 		ups_send_block(MegaTecCMD[command], strlen(MegaTecCMD[command]));
@@ -170,11 +182,12 @@ void ups_status_response(char *data)
 	endValue = strpbrk(data,"\r");
 	len = endValue - data;
 	strncpy(value, data, len);
+	uint8_t stat = 0;
 	for(i = 0; i < len; i ++)
 	{
-		UPS.Status |= (value[i] - 0x30) << (7-i);
+		stat |= (value[i] - 0x30) << (7-i);
 	}
-
+	UPS.Status = stat;
 }
 
 void ups_info_response(char *data)
@@ -224,17 +237,47 @@ void ups_megatec_process_pdu(cmdMegaTecEnums_t command)
 	}
 }
 
+int ups_metac_service_pdu(cmdMegaTecEnums_t command)
+{
+	while(!megatec_send)
+	{
+		vTaskDelay(50);
+	}
+	megatec_send = false;
+	send_MegaTec_cmd(command);
+	if (ups_megatec_rx_pdu())
+	{
+		megatec_send = true;
+		if(strncmp(ups_pdu.data, "ACK", 3) == 0)
+			return 1;
+		else if(strncmp(ups_pdu.data, "NCK", 3) == 0)
+			return 0;
+
+	}
+	return -1;
+}
+
 void request_task(void)
 {
 
 		for(;;)
 		{
-			send_MegaTec_cmd(ups_status_req);
-			if (ups_megatec_rx_pdu())
-				ups_megatec_process_pdu(ups_status_req);
-			send_MegaTec_cmd(ups_info);
-			if (ups_megatec_rx_pdu())
-				ups_megatec_process_pdu(ups_info);
+			if(megatec_send){
+				megatec_send= false;
+				send_MegaTec_cmd(ups_status_req);
+				if (ups_megatec_rx_pdu())
+					ups_megatec_process_pdu(ups_status_req);
+
+				megatec_send=true;
+			}
+			//vTaskDelay(100);
+			if(megatec_send){
+				megatec_send = false;
+				 send_MegaTec_cmd(ups_info);
+				if (ups_megatec_rx_pdu())
+					ups_megatec_process_pdu(ups_info);
+				megatec_send = true;
+			}
 
 	        vTaskDelay(1000);
 

+ 5 - 0
modules/MegaTec/megatec.h

@@ -44,6 +44,11 @@ typedef struct{
 
 UPS_value_t UPS;
 
+extern uint16_t TimeParam;
+extern uint16_t TimeParam2;
+
+int ups_metac_service_pdu(cmdMegaTecEnums_t command);
+
 void ups_megatec_init(void);