zhao006 vor 10 Monaten
Ursprung
Commit
a324aa218b

+ 2 - 2
app/HARDWARE/sources/log.c

@@ -23,9 +23,9 @@ int fputc(int ch, FILE *f)
 int fgetc(FILE *f)
 {
 		/* µÈ´ý´®¿ÚÊäÈëÊý¾Ý */
-		while (USART_GetFlagStatus(USART_485, USART_FLAG_RXNE) == RESET);
+		while (USART_GetFlagStatus(USART_232, USART_FLAG_RXNE) == RESET);
 
-		return (int)USART_ReceiveData(USART_485);
+		return (int)USART_ReceiveData(USART_232);
 }
 
 void LogPrint(logLevel_t logLevel, const char *func, const int line, char * fmt, ...)

+ 3 - 1
app/HARDWARE/sources/usart.c

@@ -1,5 +1,6 @@
 #include "usart.h"
 #include "mmodbus.h"
+#include "dlt645.h"
 
 uint8_t UART6_RX_BUF[BUFF_SIZE];
 uint8_t UART6_RX_STAT=0;
@@ -451,7 +452,8 @@ void Usart_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch)
 	while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);	
 }
 void USART3_IRQHandler(){
-			mmodbus_callback();
+			//mmodbus_callback();
+			dlt_callback();
 }
 
 /*********************************************END OF FILE**********************/

+ 13 - 57
app/MDKProject/lora_gateway.uvoptx

@@ -120,12 +120,12 @@
         <SetRegEntry>
           <Number>0</Number>
           <Key>DLGUARM</Key>
-          <Name></Name>
+          <Name>(	?d</Name>
         </SetRegEntry>
         <SetRegEntry>
           <Number>0</Number>
           <Key>JL2CM3</Key>
-          <Name>-U69407005 -O111 -S6 -ZTIFSpeedSel500 -A0 -C0 -JU1 -JI127.0.0.1 -JP0 -RST0 -N00("ARM CoreSight SW-DP") -D00(2BA01477) -L00(0) -TO18 -TC10000000 -TP21 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -TB1 -TFE0 -FO15 -FD20000000 -FC1000 -FN1 -FF0STM32F2xx_1024.FLM -FS08000000 -FL0100000 -FP0($$Device:STM32F207ZGTx$CMSIS\Flash\STM32F2xx_1024.FLM)</Name>
+          <Name>-U69407005 -O111 -S2 -ZTIFSpeedSel5000 -A0 -C0 -JU1 -JI127.0.0.1 -JP0 -RST0 -N00("ARM CoreSight SW-DP") -D00(2BA01477) -L00(0) -TO18 -TC10000000 -TP21 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -TB1 -TFE0 -FO15 -FD20000000 -FC1000 -FN1 -FF0STM32F2xx_1024.FLM -FS08000000 -FL0100000 -FP0($$Device:STM32F207ZGTx$CMSIS\Flash\STM32F2xx_1024.FLM)</Name>
         </SetRegEntry>
         <SetRegEntry>
           <Number>0</Number>
@@ -153,56 +153,7 @@
           <Name>UL2CM3(-S0 -C0 -P0 -FD20000000 -FC1000 -FN1 -FF0STM32F2xx_1024 -FS08000000 -FL0100000 -FP0($$Device:STM32F207ZGTx$CMSIS\Flash\STM32F2xx_1024.FLM))</Name>
         </SetRegEntry>
       </TargetDriverDllRegistry>
-      <Breakpoint>
-        <Bp>
-          <Number>0</Number>
-          <Type>0</Type>
-          <LineNumber>76</LineNumber>
-          <EnabledFlag>1</EnabledFlag>
-          <Address>134298458</Address>
-          <ByteObject>0</ByteObject>
-          <HtxType>0</HtxType>
-          <ManyObjects>0</ManyObjects>
-          <SizeOfObject>0</SizeOfObject>
-          <BreakByAccess>0</BreakByAccess>
-          <BreakIfRCount>1</BreakIfRCount>
-          <Filename>C:\Users\IBM\Desktop\app\app閭\System\source\sys_mqtt.c</Filename>
-          <ExecCommand></ExecCommand>
-          <Expression>\\lora_gateway\../System/source/sys_mqtt.c\76</Expression>
-        </Bp>
-        <Bp>
-          <Number>1</Number>
-          <Type>0</Type>
-          <LineNumber>75</LineNumber>
-          <EnabledFlag>1</EnabledFlag>
-          <Address>134298456</Address>
-          <ByteObject>0</ByteObject>
-          <HtxType>0</HtxType>
-          <ManyObjects>0</ManyObjects>
-          <SizeOfObject>0</SizeOfObject>
-          <BreakByAccess>0</BreakByAccess>
-          <BreakIfRCount>1</BreakIfRCount>
-          <Filename>C:\Users\IBM\Desktop\app\app閭\System\source\sys_mqtt.c</Filename>
-          <ExecCommand></ExecCommand>
-          <Expression>\\lora_gateway\../System/source/sys_mqtt.c\75</Expression>
-        </Bp>
-        <Bp>
-          <Number>2</Number>
-          <Type>0</Type>
-          <LineNumber>68</LineNumber>
-          <EnabledFlag>1</EnabledFlag>
-          <Address>134258906</Address>
-          <ByteObject>0</ByteObject>
-          <HtxType>0</HtxType>
-          <ManyObjects>0</ManyObjects>
-          <SizeOfObject>0</SizeOfObject>
-          <BreakByAccess>0</BreakByAccess>
-          <BreakIfRCount>1</BreakIfRCount>
-          <Filename>C:\Users\IBM\Desktop\app\app閭\System\source\task.c</Filename>
-          <ExecCommand></ExecCommand>
-          <Expression>\\lora_gateway\../System/source/task.c\68</Expression>
-        </Bp>
-      </Breakpoint>
+      <Breakpoint/>
       <WatchWindow1>
         <Ww>
           <count>0</count>
@@ -224,12 +175,17 @@
           <WinNumber>1</WinNumber>
           <ItemText>mqtt_connectFlag,0x0A</ItemText>
         </Ww>
+        <Ww>
+          <count>4</count>
+          <WinNumber>1</WinNumber>
+          <ItemText>dlt645</ItemText>
+        </Ww>
       </WatchWindow1>
       <MemoryWindow1>
         <Mm>
           <WinNumber>1</WinNumber>
           <SubType>8</SubType>
-          <ItemText>buf</ItemText>
+          <ItemText>pubJsonString</ItemText>
           <AccSizeX>0</AccSizeX>
         </Mm>
       </MemoryWindow1>
@@ -999,7 +955,7 @@
 
   <Group>
     <GroupName>FatFS</GroupName>
-    <tvExp>1</tvExp>
+    <tvExp>0</tvExp>
     <tvExpOptDlg>0</tvExpOptDlg>
     <cbSel>0</cbSel>
     <RteFlg>0</RteFlg>
@@ -1847,7 +1803,7 @@
 
   <Group>
     <GroupName>MQTT</GroupName>
-    <tvExp>1</tvExp>
+    <tvExp>0</tvExp>
     <tvExpOptDlg>0</tvExpOptDlg>
     <cbSel>0</cbSel>
     <RteFlg>0</RteFlg>
@@ -2055,7 +2011,7 @@
 
   <Group>
     <GroupName>DLT645</GroupName>
-    <tvExp>0</tvExp>
+    <tvExp>1</tvExp>
     <tvExpOptDlg>0</tvExpOptDlg>
     <cbSel>0</cbSel>
     <RteFlg>0</RteFlg>
@@ -2123,7 +2079,7 @@
 
   <Group>
     <GroupName>MODBUS</GroupName>
-    <tvExp>1</tvExp>
+    <tvExp>0</tvExp>
     <tvExpOptDlg>0</tvExpOptDlg>
     <cbSel>0</cbSel>
     <RteFlg>0</RteFlg>

BIN
app/OBJ/lora_gateway.axf


Datei-Diff unterdrückt, da er zu groß ist
+ 18433 - 18300
app/OBJ/lora_gateway.hex


+ 6 - 22
app/System/includes/gateway_message.h

@@ -2,10 +2,6 @@
 #define __GATEWAY_MESSAGE_H
 #include "stm32f2xx.h"
 
-
-
-
-
 // 网关所存储的modbus写命令
 typedef struct _GATEWAY_WRITE_MODBUS_COMMAND
 {
@@ -59,11 +55,9 @@ typedef struct _DEVICE_PARAMS
 {
 	uint8_t aliveflag; // 存活证明(由轮询来进行保证,当有轮询回应是确定其存活,当链路变化时立即上报设备已经死去,当轮询有相应的相应值时去上报已经存活)
 	uint8_t deviceID[20];
-	uint8_t protocol;  // 所属协议类型   1:modbus read 2:modbus write 3:dlt97  4:dlt07    一个设备只能存在一个协议
-	
+	uint8_t protocol;  // 所属协议类型   1:modbus read 2:modbus write 3:dlt97  4:dlt07    一个设备只能存在一个协议	
 	uint8_t MDBbigLittleFormat;
-	uint8_t MDBdataType;
-	
+	uint8_t MDBdataType;	
 	PARAMS_PROTOCOL_COMMAND	*params;
 	struct _DEVICE_PARAMS *nextDevice; // 指向下一个设备地址
 } DEVICE_PARAMS;
@@ -87,35 +81,25 @@ typedef struct _GATEWAY_PARAMS
 	uint8_t state;			 // 检测是否上位机配置过当上位机配置过该状态位,则以后均不走http获取相应配置 置位值为0xF1
 	uint8_t gatewayId[10];	 // 固化信息
 	uint8_t gatewayMode[10]; // 工作模式、后续版本迭代使用
-	
-	// 网关相关信息
-	uint8_t gateName[10];
+		uint8_t gateName[10];		// 网关相关信息
 
 	// 包含的设备结构体
 	uint8_t deviceCount;
 	DEVICE_PARAMS *device_params;
-	
-	// 外设的串口工作属性仅只有一个
+		// 外设的串口工作属性仅只有一个
 	uint32_t baudrate;	 // 波特率
 	uint8_t dataBits;	 // 数据位
 	uint8_t checkBit;	 // 校验位
 	uint8_t stopBit;	 // 停止位
 	uint8_t flowControl; // 流控制
-	uint8_t bandwidth;
 	uint8_t parity;				//校验位
-	
+	uint8_t bandwidth;	
 } GATEWAY_PARAMS;
 
 void addGatewayParams(char *gatewayString);
-void addDeviceParams(char *deviceString);
-
-
 int parseIntField(const char *data, const char *field);
 void parseStringField(const char *data, const char *field, char *value);
-
-GATEWAY_PARAMS *get_gateway_config_params();
-DEVICE_PARAMS *get_device_config_params();
-
+GATEWAY_PARAMS *get_gateway_config_params(void);
 int extract_substring(const char *input_string, const char *start_token, const char *end_token, char *result);
 
 //////////////////////////////////////////////////////////////////////////////////////////////////////////

+ 2 - 2
app/System/includes/json_queue.h

@@ -4,10 +4,10 @@
 #include "string.h"
 void creat_queue(void);
 typedef struct {
-    char *p;
     uint32_t stringLength;
+    char *p;
 } StringInfo;
 
 
 
-#endif
+#endif

+ 2 - 2
app/System/includes/myFile.h

@@ -9,6 +9,6 @@
 
 void write_file(const char* filename, const char* data, uint32_t length);
 void read_file(const char* filename, char* data);
-void ViewRootDir();
+void ViewRootDir(void);
 void DeleteDirFile(const char* filename);
-#endif
+#endif

+ 2 - 2
app/System/includes/node_data_acquisition.h

@@ -5,5 +5,5 @@
 #include "dlt645_port.h"
 void data_acquisition(void);
 extern uint8_t protocol_485;
-void rs_485_test();
-#endif
+void rs_485_test(void);
+#endif

+ 3 - 3
app/System/includes/node_message.h

@@ -60,9 +60,9 @@ typedef struct _NODE_DEVICE_PARAMS
     PARAMS params[20];              
 }NODE_DEVICE_PARAMS;
 
-NODE_DEVICE_PARAMS *get_node_receive_params();
-void free_all_node_params();
+NODE_DEVICE_PARAMS *get_node_receive_params(void);
+void free_all_node_params(void);
 extern uint8_t protocol_485;
 #endif
 
-#endif
+#endif

+ 1 - 1
app/System/includes/protocol.h

@@ -11,4 +11,4 @@ int masterSendNodeString(uint8_t nodeIndex, uint8_t *string, uint16_t *size);
 uint8_t SlaveProtocolAnalysis(uint8_t *buff, uint16_t len);
 void nodeSendReaddValue(uint8_t *string,uint16_t *size);
 int GatewayProtocolAnalysis(uint8_t *string, uint16_t len);
-#endif
+#endif

+ 0 - 1
app/System/includes/sys_http.h

@@ -1,7 +1,6 @@
 #ifndef SYS_HTTP_H
 #define SYS_HTTP_H
 
-
 void http_getDemo(void);
 
 void http_postDemo(void);

+ 2 - 1
app/System/includes/sys_mqtt.h

@@ -39,9 +39,10 @@
 
 extern int mqtt_connectFlag;
 extern OS_EVENT *mqtt_sendMseeageMbox;
+extern OS_EVENT *mqtt_recvMseeageMbox;
 extern OS_EVENT *JsonQ;
 void mqtt_threadCreate(void);
-char *get_mqtt_payload();
+
 
 #define jsonMaxSize 1000
 extern char pubJsonString[jsonMaxSize];

+ 6 - 16
app/System/includes/task.h

@@ -7,23 +7,13 @@
 //主要的数据处理线程
 #define DATA_TASK_PRIO 8
 #define DATA_STK_SIZE  2*1024
-
+#define   GetCurrentTime()  OSTimeGet()
 extern void data_task(void *pdata);
+extern tRadioDriver *Radio;
 
-// 自定义 JSONItem 结构体用来表示 JSON 数据项
-typedef struct
-{
-    char deviceId[20];
-    int power;
-    int temp;
-    int mode;
-    int fan;
-    int roomTemp;
-    int fault;
-} JSONItem;
+void write_modbus_data(char* buf);
+void send_mqtt(char*buf, int jsonCunt);
+void find_difference(char* data1, char* data2, char* string);
+int read_device_data(DEVICE_PARAMS *current_device,char* buf);
 
-void WRITE_MODBUS_DATA(char* buf);
-extern tRadioDriver *Radio;
-extern int commd;
-int* get_jsoncunt();
 #endif

+ 1 - 1
app/System/includes/time_count.h

@@ -4,4 +4,4 @@
 int time1;
 int time2;
 
-#endif
+#endif

+ 1 - 1
app/System/source/protocol.c

@@ -103,7 +103,7 @@ int masterSendNodeString(uint8_t nodeIndex, uint8_t *string, uint16_t *size)
 	// 最多进行20次循环组成下发数据
 	int i = 0;
 	int len;
-	len = strlen(string);
+	len = strlen((char*)string);
 	do
 	{
 		switch (currentDevice->protocol)

+ 7 - 30
app/System/source/sys_mqtt.c

@@ -72,23 +72,9 @@ void mqtt_outputMsg(MQTTString *name, uint8_t *msgbuf, int msglen, uint16_t id,
 	int lenght=msglen;
 	MQTT_PRINTF("receive a msg: id=%d qos=%d topic=%s msg=%s \r\n", id, qos, name->lenstring.data, msgbuf);
 	StringInfo message;
-	message.stringLength=msglen;
-	message.p=mymalloc(SRAMIN ,msglen+1);
+	message.p=mymalloc(SRAMEX ,msglen);
 	memcpy(message.p,msgbuf,msglen);
-	uint8_t err;
-	OSTimeDly(1500);
-	err=OSQPost(JsonQ,(void *)&message);
-	commd = 0;
-	switch(err)
-	{
-		case OS_ERR_NONE: 
-			break;
-		case OS_ERR_Q_FULL: 
-			MQTT_PRINTF("receive a msg queue is full \r\n");
-			break;
-		default:
-			break;
-	}
+	OSMboxPost(mqtt_recvMseeageMbox,(void*)&message);
 }	
 
 /************************************************************
@@ -204,6 +190,7 @@ int mqtt_userSendMessage(int sock, int boxMsg)
 
 
 OS_EVENT *mqtt_sendMseeageMbox;   //发送消息邮箱
+OS_EVENT *mqtt_recvMseeageMbox; 	//接收消息邮箱
 int mqtt_connectFlag;             //成功连接服务器标志   
 static int mysock;                //连接mqtt服务器的sock编号
 
@@ -263,7 +250,7 @@ __MQTT_START:
 			rc = mqtt_pingReq(mysock);
 			if(rc == -1) goto __MQTT_START;
 		}
-		OSTimeDly(1);
+		OSTimeDly(100);
 	}
 }
 
@@ -306,7 +293,7 @@ void mqtt_userReceiveThread(void *arg)
 			packetType = MQTTPacket_read(mqtt_recvbuffer, len, transport_getdata);
 			mqtt_userReceiveMessage(mysock, packetType, mqtt_recvbuffer, len);
 		}
-		OSTimeDly(1);
+		OSTimeDly(100);
 	}
 }
 
@@ -327,18 +314,8 @@ void mqtt_threadCreate(void)
 {
 	mqtt_connectFlag = 0;
 	mqtt_sendMseeageMbox = OSMboxCreate(NULL);
+	mqtt_recvMseeageMbox = OSMboxCreate(NULL);
 	JsonQ = OSQCreate(json_message[0], QUEUE_SIZE);  //创建json队列
 	OSTaskCreate(mqtt_userManThread, NULL, &mqttmainTaskStack[APP_TASK_MQTTMAIN_STK_SIZE-1], APP_TASK_MQTTMAIN_PRIO);	
 	OSTaskCreate(mqtt_userReceiveThread, NULL, &mqttreceiveTaskStack[APP_TASK_MQTTRECEIVE_STK_SIZE-1], APP_TASK_MQTTRECEIVE_PRIO);		
-}
-
-
-
-
-
-
-
-
-
-
-
+}

+ 1 - 1
app/System/source/sys_sx1278.c

@@ -15,7 +15,7 @@ void sx1278_send_task(void *pdata)
 			message=(StringInfo *)OSQPend(JsonQ,1000,&err);
 			if(message != NULL)
 			{
-				WRITE_MODBUS_DATA(message->p);
+				write_modbus_data(message->p);
 			}
 			printf("sx1278_task_recv : %s",message->p);
 			myfree(SRAMEX ,message->p);

+ 270 - 203
app/System/source/task.c

@@ -5,8 +5,8 @@
 #include "log.h"
 #include "malloc.h"
 #include "sx1276.h"
+#include "dlt645.h"
 
-#include "protocol.h"
 #include "usart.h"
 #include "node_data_acquisition.h"
 #include "sys_mqtt.h"
@@ -19,19 +19,9 @@
 #include "MQTTClient.h"
 #include "cJSON.h"
 #include "time_count.h"
+#include "dlt645_1997_private.h"
 
-
-
-int READ_DATA(DEVICE_PARAMS *current_device,char* buf);
-void WRITE_MODBUS_DATA(char* cJSONstring);
-///////////////////////////////////////////////////////////////////////////////////////////
 char string[512];
-void  findDifference(char* data1, char* data2, char* string) ;
-int jsoncunt = 1;
-int commd = 1;
-
-////////////////////////////////////////////////////////////////////////////////////////////
-
 /*
 *********************************************************************************************************
 *	�� �� ��: void data_task(void *pdata)
@@ -40,165 +30,201 @@ int commd = 1;
 *	�� �� ֵ: ��
 *********************************************************************************************************
 */
-
 void data_task(void *pdata)
 {
-	OS_CPU_SR cpu_sr;
-	pdata = pdata;
-	mmodbus_init(1);
-	char buf[1000];	// ����modbus��ȡ������	
-	
-	char *device_config_json = mymalloc(SRAMEX, 9 * 1024);
-	read_file("device.txt", device_config_json);
-	addGatewayParams(device_config_json);
-	myfree(SRAMEX, device_config_json);
-	
-	GATEWAY_PARAMS *get;
-	get= get_gateway_config_params();
-	int deviceIndex=0;
-	DEVICE_PARAMS *current_device=get->device_params;
-	
-//	Config_485_Port(get->baudrate, get->dataBits, get->stopBit, get->parity, get->flowControl);
-	 
-	while (current_device!=NULL)
-	{
-		uint8_t err;
-		StringInfo *message=NULL; //���ն�����Ϣ�ṹ��
-		message=(StringInfo *)OSQPend(JsonQ,1000,&err);
-		if(commd)
-		{			
-			READ_DATA(current_device, buf);		
-			if(current_device->nextDevice!=NULL) 
-			{
-				current_device=current_device->nextDevice;		
-			}
-			else
-			{		 
-				sprintf(buf + strlen(buf) - 1, "}");
-					time1 = OSTimeGet();
-					if( jsoncunt || time2  <= time1 - 20000)
-					{
-						memset(pubJsonStringCopy,0, strlen(pubJsonStringCopy));
-						memcpy(pubJsonStringCopy + strlen(pubJsonStringCopy), buf, strlen(buf));//������һ�ε�����
-						sprintf(pubJsonString,"{\"DEVICEID\":\"%s\",\"data\":[%s]",get->deviceId, buf);									
-						int msg = MBOX_USER_PUBLISHQOS2;	
-						if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
-						current_device=get->device_params;
-						jsoncunt = 0;
-						time2 = OSTimeGet();
-					}//20s����һ��
-					
-					else
-					{
-						if(strcmp(buf,pubJsonStringCopy))
-						{
-							memset(string, 0 , strlen(string));
-							findDifference(buf, pubJsonStringCopy, string);	
-							memset(pubJsonString,0, strlen(pubJsonString));
-							sprintf(pubJsonString,"{\"deviceId\":\"%s\",\"data\":[{%s}]}",get->deviceId, string);
-							memset(pubJsonStringCopy,0, strlen(pubJsonStringCopy));
-							sprintf(pubJsonStringCopy, buf, strlen(buf));
-						
-							int msg = MBOX_USER_PUBLISHQOS2;	
-							if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);	
-						}
-				}	
+		OS_CPU_SR cpu_sr;
+		pdata = pdata;
+		dlt645_init(100);
+		//mmodbus_init(1);
+		
+		int jsonCunt = 1;	
+		char *device_config_json = mymalloc(SRAMEX, 9 * 1024);
+		read_file("device.txt", device_config_json);
+		addGatewayParams(device_config_json);
+		myfree(SRAMEX, device_config_json);
+		
+		GATEWAY_PARAMS *get;
+		get= get_gateway_config_params();
+		DEVICE_PARAMS *current_device=get->device_params;
+//		Config_485_Port(get->baudrate, get->dataBits, get->stopBit, get->parity, get->flowControl);
+		char *buf = mymalloc(SRAMEX, 9 * 1024);	// ���ն�ȡ������
+		memset(buf, 0, 9 * 1024);	
+		while (current_device!=NULL)
+		{						
+				read_device_data(current_device, buf);	//��ȡ����	
+				send_mqtt(buf, jsonCunt);								//��������
+				jsonCunt = 0;		
 				memset(buf,0,strlen(buf));	
-				current_device=get->device_params;
-			}			
+				current_device=get->device_params;		
+				OSTimeDly(1);
 		}
-		else
-		{			
-			if(message != NULL)
-			{
-				WRITE_MODBUS_DATA(message->p);
-			}
-			printf("sx1278_task_recv : %s",message->p);
-			myfree(SRAMIN ,message->p);
-			
-			commd = 1;	 
+		myfree(SRAMEX, buf);
+}	
+/*
+*********************************************************************************************************
+*	�� �� ��: void mqtt_to_device()
+*	����˵��: �����յ������ݷ������豸
+*	��    ��:
+*	�� �� ֵ:  
+*********************************************************************************************************
+*/
+void mqtt_to_device(){
+		uint8_t err;
+		StringInfo *message;
+		message	= (StringInfo*)OSMboxPend(mqtt_recvMseeageMbox, 1000, &err);	
+		
+		if(message != NULL)	//������Ϣ
+		{
+				write_modbus_data(message->p);	//�����
+				myfree(SRAMEX ,message->p);//�ͷ��ڲ�����
+				//OSTimeDly(1000);
 		}	
-				OSTimeDly(1000); 
-	}
 }
 
-	#if 0   //���Դ��벻����ת��ֱ�ӽ�����Ӧ�Ľ���
-			SlaveProtocolAnalysis(string,bufferLength);
-			data_acquisition();
-			uint8_t node_string[256];
-			uint16_t node_string_Length;
-			nodeSendReaddValue(node_string,&node_string_Length);
-			GatewayProtocolAnalysis(node_string,node_string_Length);
-#endif			
- 
 /*
 *********************************************************************************************************
 *	�� �� ��: int READ_MODBUS_DATA(DEVICE_PARAMS *device)
 *	����˵��: ��ȡ��ǰ�ڵ��ϵ�modbus����
-*	��    �Σ�DEVICE_PARAMS *device  ��ǰ�豸
-*	�� �� ֵ:  1 ���ɹ�  0��ʧ��
+*	��    ��: DEVICE_PARAMS *device  ��ǰ�豸
+*	�� �� ֵ: 1: �ɹ� 0:ʧ��
 *********************************************************************************************************
 */
-
-int READ_DATA(DEVICE_PARAMS *device, char* buf)
-	{
-		DEVICE_PARAMS *current_device=device;
-		GATEWAY_PARAMS* get;
-		get = get_gateway_config_params();
+int read_device_data(DEVICE_PARAMS *device, char* buf)
+{
+		DEVICE_PARAMS *current_device=device;	
 		GATEWAY_READ_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_read_modbus_command;
-		GATEWAY_READ_DLT645_COMMAND *currentDLT645Params = current_device->params->gateway_read_dlt645_command;
+		GATEWAY_READ_DLT645_COMMAND *currentDLT645Params = current_device->params->gateway_read_dlt645_command;	
+
 		while(current_device->params != NULL)
-		{
-			if (current_device->protocol == MODBUS_READ)
-			{
-				protocol_485=1;
-				uint16_t data[currentModbusParams->registerByteNum /2]; // modbus�Ĵ�������
-				mmodbus_set16bitOrder(current_device->MDBbigLittleFormat);
-				if (currentModbusParams->functionCode == 0x03 | currentModbusParams->functionCode == 0x01)
+		{				
+				if (current_device->protocol == MODBUS_READ)
 				{
-					bool success = mmodbus_readHoldingRegisters16i(currentModbusParams->slaveAddress,
-															 currentModbusParams->registerAddress,
-															 currentModbusParams->registerByteNum /2,
-															 data);
-					if (success)
-					{
-						uint32_t value;
-						if (currentModbusParams->registerByteNum == 4)
+						protocol_485=1;
+						uint16_t data[currentModbusParams->registerByteNum /2]; // modbus�Ĵ�������
+						mmodbus_set16bitOrder(current_device->MDBbigLittleFormat);
+						if (currentModbusParams->functionCode == 0x03 | currentModbusParams->functionCode == 0x01)
 						{
-							value = (uint32_t)data[0] | data[1];
-						}
-						else if (currentModbusParams->registerByteNum == 2)
+								bool success = mmodbus_readHoldingRegisters16i(currentModbusParams->slaveAddress,
+																		 currentModbusParams->registerAddress,
+																		 currentModbusParams->registerByteNum /2,
+																		 data);
+								if (success)
+								{
+										uint32_t value;
+										if (currentModbusParams->registerByteNum == 4)
+										{
+												value = (uint32_t)data[0] | data[1];
+										}
+										else if (currentModbusParams->registerByteNum == 2)
+										{
+												value = data[0];
+										}		
+										if (currentModbusParams->decimalPoint == 0)
+										{
+												currentModbusParams->value[0] = value;
+												currentModbusParams->value[1] = value << 8;
+												currentModbusParams->value[2] = value << 16;
+												currentModbusParams->value[3] = value << 24;
+										}
+										else
+										{
+												float convertedValue = (float)value / pow(10, currentModbusParams->decimalPoint);
+												memcpy(currentModbusParams->value, &convertedValue, 4);
+										}
+										sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":%d},", 
+																							current_device->deviceID, currentModbusParams->keyword, value);									
+								}
+								else
+								{
+										printf("read modbus register fail\n");
+										return 0;
+								}
+								
+								/* ÿ����һ���Ĵ���������message�ж� */
+								mqtt_to_device();
+								
+								currentModbusParams = currentModbusParams->nextParams;
+								if (currentModbusParams == NULL)	
+								{
+										current_device = current_device->nextDevice;
+										currentModbusParams = current_device->params->gateway_read_modbus_command;
+										if(current_device == NULL)
+										{
+												sprintf(buf + strlen(buf) - 1, "}"); 
+												return 1;
+										}
+								}												
+						}					
+				}
+				 else if (current_device->protocol == DLT645_2007 || current_device->protocol == DLT645_97)
+				{
+						protocol_485=2;
+						uint8_t read_buf[10];
+						
+						currentDLT645Params->rxLen = 0;
+						memset(read_buf, 0, 10);
+						memset(currentDLT645Params->data, 0, 10);
+
+						dlt645_set_addr(&dlt645, currentDLT645Params->deviceID645);
+						int8_t rs;
+						if (current_device->protocol == DLT645_2007)
 						{
-							value = data[0];
-						}		
-						if (currentModbusParams->decimalPoint == 0)
+								rs = dlt645_read_data(&dlt645, currentDLT645Params->Identification, read_buf, DLT645_2007);
+						}
+						else if (current_device->protocol == DLT645_1997)
 						{
-							currentModbusParams->value[0] = value;
-							currentModbusParams->value[1] = value << 8;
-							currentModbusParams->value[2] = value << 16;
-							currentModbusParams->value[3] = value << 24;
+								rs = dlt645_read_data(&dlt645, currentDLT645Params->Identification, read_buf, DLT645_1997);
 						}
-						else
+						if (rs != -1)
 						{
-							float convertedValue = (float)value / pow(10, currentModbusParams->decimalPoint);
-							memcpy(currentModbusParams->value, &convertedValue, 4);
+								if (rs <= 4)
+								{
+										memcpy(currentDLT645Params->data, read_buf, 4);
+										currentDLT645Params->rxLen = 4;
+								}
+								else if (rs == 5)
+								{
+										memcpy(currentDLT645Params->data, read_buf, 5);
+										currentDLT645Params->rxLen = 5;
+								}
+							else if (rs > 5)
+								{
+										memcpy(currentDLT645Params->data, read_buf, 9);
+										currentDLT645Params->rxLen = 9;
+								}
+								sprintf(buf + strlen(buf), "{\"identifier\":\"%s\",\"deviceID645\":\"%02x%02x%02x%02x%02x%02x\",\"identifier645\":%d}",
+																				currentDLT645Params->keyword, currentDLT645Params->deviceID645[0],
+																				currentDLT645Params->deviceID645[1],currentDLT645Params->deviceID645[2],
+																				currentDLT645Params->deviceID645[3],currentDLT645Params->deviceID645[4],
+																				currentDLT645Params->deviceID645[5],currentDLT645Params->Identification);
+								
+			
 						}
-					 sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":%d},", 
-																									current_device->deviceID, currentModbusParams->keyword, value);
-					currentModbusParams = currentModbusParams->nextParams;
-					if (currentModbusParams == NULL)	
-					{
-						sprintf(buf + strlen(buf) - 1, ""); 
-						return 0;
-					}
+//						else
+//						{
+//								currentDLT645Params->rxLen = 0;
+//								printf("read DLT current data fail\n");
+//						}
+								/* ÿ����һ���Ĵ���������message�ж� */
+								mqtt_to_device();
 					
-				}
-			}
+								currentDLT645Params = currentDLT645Params->nextParams;		
+								if (currentDLT645Params == NULL)	
+								{
+										current_device = current_device->nextDevice;
+										currentDLT645Params = current_device->params->gateway_read_dlt645_command;
+										if(current_device == NULL)
+										{
+												sprintf(buf + strlen(buf) - 1, "}"); 
+												return 1;
+										}
+								}										
+				}			
+				
 		}		
-	}
 		return 1;
 }
-	/*
+/*
 *********************************************************************************************************
 *	�� �� ��:void WRITE_MODBUS_DATA(char* cJSONstring)
 *	����˵��: ����mqtt���ݲ�д��modbus�Ĵ���
@@ -206,15 +232,16 @@ int READ_DATA(DEVICE_PARAMS *device, char* buf)
 *	�� �� ֵ: ��
 *********************************************************************************************************
 */
-void WRITE_MODBUS_DATA(char* cJSONstring)
+void write_modbus_data(char* cJSONstring)
 {
-	uint16_t data;
-	uint16_t number;
-	uint16_t slaveAddress;
-	GATEWAY_PARAMS* get;
-	get = get_gateway_config_params();
-	DEVICE_PARAMS* current_device = get->device_params;
-	if(cJSONstring[0] != '\0'){
+		uint16_t data;
+		uint16_t number;
+		uint16_t slaveAddress;
+		
+		GATEWAY_PARAMS* get;
+		get = get_gateway_config_params();
+		DEVICE_PARAMS* current_device = get->device_params;
+		
 		cJSON *root = cJSON_Parse(cJSONstring);
 		const char *deviceId = cJSON_GetStringValue(cJSON_GetObjectItem(root, "deviceId"));	
 		const cJSON *power = cJSON_GetObjectItemCaseSensitive(root, "power");
@@ -224,79 +251,80 @@ void WRITE_MODBUS_DATA(char* cJSONstring)
 		
 		while(current_device)
 		{
-			char* device_ID = (char*)current_device->deviceID;
-			GATEWAY_WRITE_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_write_modbus_command;
-			if(!strcmp(device_ID,deviceId))
-			{
-				OSTimeDlyHMSM(0, 0, 0, 100);
-				//OSIntEnter();
-				if(power)
+				char* device_ID = (char*)current_device->deviceID;
+				GATEWAY_WRITE_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_write_modbus_command;
+				if(!strcmp(device_ID,deviceId))
 				{
-					slaveAddress = currentModbusParams->slaveAddress;
-					number =currentModbusParams->registerAddress;
-					data = power->valueint;
-					mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
-				}
-				OSTimeDlyHMSM(0, 0, 0, 100);
-				if(temp)
-				{
-					currentModbusParams = currentModbusParams->nextParams;
-					slaveAddress = currentModbusParams->slaveAddress;
-					number =currentModbusParams->registerAddress;
-					data = temp->valueint;
-					mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
-				}
-				OSTimeDlyHMSM(0, 0, 0, 100);
-				if(mode)
-				{
-					currentModbusParams = currentModbusParams->nextParams;
-					slaveAddress = currentModbusParams->slaveAddress;
-					number =currentModbusParams->registerAddress;
-					data = mode->valueint;
-					mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
-				}
-				OSTimeDlyHMSM(0, 0, 0, 100);
-				if(fan)
-				{
-					currentModbusParams = currentModbusParams->nextParams;
-					slaveAddress = currentModbusParams->slaveAddress;
-					number =currentModbusParams->registerAddress;
-					data = fan->valueint;
-					mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
+						OSTimeDly(100);
+						if(power)
+						{
+								mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress, 
+																								currentModbusParams->registerAddress, 
+																								power->valueint);
+						}
+						OSTimeDly(100);
+						if(temp)
+						{
+								currentModbusParams = currentModbusParams->nextParams;
+								mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress, 
+																								currentModbusParams->registerAddress, 
+																								temp->valueint);
+						}
+						OSTimeDly(100);
+						if(mode)
+						{
+								currentModbusParams = currentModbusParams->nextParams;
+								mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress, 
+																								currentModbusParams->registerAddress, 
+																								mode->valueint);
+						}
+						OSTimeDly(100);
+						if(fan)
+						{
+								currentModbusParams = currentModbusParams->nextParams;
+								mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress, 
+																									currentModbusParams->registerAddress, 
+																									fan->valueint);					
+						}
 				}
-				//OSIntExit();
-			}
-			current_device = current_device->nextDevice;
+				current_device = current_device->nextDevice;
 		}
 		cJSON_Delete(root);
-
-	}
 }
 
-
-// �Ƚ����� JSONItem ���飬�ҵ��仯�IJ���
-void  findDifference(char* buf, char* pubJsonStringCopy, char* string) 
+/*
+*********************************************************************************************************
+*	�� �� ��: void find_difference(char* buf, char* pubJsonStringCopy, char* string)
+*	����˵��: �Ƚϳ�����1�Ͳ���2�IJ�ͬ��
+*	��    ��: ����1:������ ����2:������ ����3���������
+*	�� �� ֵ: ��
+*********************************************************************************************************
+*/
+void find_difference(char* buf, char* pubJsonStringCopy, char* string) 
 {
     const char* delimiter = "{}";
     char* saveptr1;
     char* saveptr2;
 		char* data1 = malloc(strlen(buf) + 1);	
 		char* data2 = malloc(strlen(pubJsonStringCopy) + 1);	;
+		
 		memcpy(data1, buf, strlen(buf));
 		memcpy(data2, pubJsonStringCopy, strlen(pubJsonStringCopy));
-    // ����strtok_r�����ָ��ַ���������һ�Ƚ�
+     // ����strtok_r�����ָ��ַ���������һ�Ƚ�
     char* token1 = strtok_r((char*)data1, delimiter, &saveptr1);
     char* token2 = strtok_r((char*)data2, delimiter, &saveptr2);
 
-    while (token1 != NULL && token2 != NULL) {
-        if (strcmp(token1, token2) != 0) {
+    while (token1 != NULL && token2 != NULL) 
+		{
+        if (strcmp(token1, token2) != 0) 
+				{
             memcpy(string + strlen(string), token1, strlen(token1));
         }
         token1 = strtok_r(NULL, delimiter, &saveptr1);
         token2 = strtok_r(NULL, delimiter, &saveptr2);
     }
 
-//    // �����ʣ���ַ���δ�Ƚϣ����ӡʣ���ַ���
+//     // �����ʣ���ַ���δ�Ƚϣ����ӡʣ���ַ���
 //    while (token1 != NULL) {
 //        sprintf(string + strlen(string),"%s,", token1);
 //        token1 = strtok_r(NULL, delimiter, &saveptr1);
@@ -310,3 +338,42 @@ void  findDifference(char* buf, char* pubJsonStringCopy, char* string)
 		free(data2);
 }
 
+/*
+*********************************************************************************************************
+*	�� �� ��: void send_mqtt(char*buf, int jsonCunt)
+*	����˵��: �����ݷ��͵�mqtt
+*	��    ��: ����1:��ȡ���� ����2:��һ�η��ͱ�־
+*	�� �� ֵ: ��
+*********************************************************************************************************
+*/
+void send_mqtt(char*buf, int jsonCunt){
+		GATEWAY_PARAMS *get;
+		get= get_gateway_config_params();
+		time1 = GetCurrentTime();
+		
+		if(jsonCunt || time2  <= time1 - (3 * 1000)) // 20s����һ��
+		{
+				memset(pubJsonStringCopy,0, strlen(pubJsonStringCopy));
+				memcpy(pubJsonStringCopy + strlen(pubJsonStringCopy), buf, strlen(buf));//������һ�ε�����
+				sprintf(pubJsonString,"{\"DEVICEID\":\"%s\",\"data\":[%s]",get->deviceId, buf);									
+				
+				int msg = MBOX_USER_PUBLISHQOS0;	
+				if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
+				time2 = GetCurrentTime();			
+		}
+		else
+		{
+				if(strcmp(buf,pubJsonStringCopy))
+				{
+						memset(string, 0 , strlen(string));
+						find_difference(buf, pubJsonStringCopy, string);	
+						memset(pubJsonString,0, strlen(pubJsonString));
+						sprintf(pubJsonString,"{\"deviceId\":\"%s\",\"data\":[{%s}]}",get->deviceId, string);
+						memset(pubJsonStringCopy,0, strlen(pubJsonStringCopy));
+						sprintf(pubJsonStringCopy, buf, strlen(buf));
+					
+						int msg = MBOX_USER_PUBLISHQOS0;	
+						if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);	
+				}
+		}	
+}

+ 5 - 5
app/USER/main.c

@@ -32,7 +32,7 @@
 *  5: 用于 ota升级
 *  6: 用于 MQTT数据发送线程  APP_TASK_MQTTMAIN_PRIO
 *  7: 用于 MQTT数据接收线程  APP_TASK_MQTTRECEIVE_PRIO
-*  8: 用于 sx1278发送/接收处理数据
+*  8: 用于 sx1278发送/接收处理数据	DATA_TASK_PRIO
 *  9: 用于 本机状态led显示      
 * 10:用于网络状态led显示
 * 11:用于轮询读出本机保存信息发送到sx1278发送接收线程
@@ -72,7 +72,7 @@ int main(void)
 {
 	int status;
 	load_unique();
-	sprintf(gatewayId,"DTtest0001");//DT8pd3ac6h  DTbma5ac6h  DTtest0001
+	sprintf(gatewayId,"DT8pd3ac6h");//DT8pd3ac6h  DTbma5ac6h  DTtest0001
 	NVIC_Configuration();
 	my_mem_init(SRAMEX);
 	my_mem_init(SRAMIN);
@@ -93,10 +93,10 @@ int main(void)
 	//OSTaskCreate(start_task,(void*)0,(OS_STK*)&START_TASK_STK[START_STK_SIZE-1],START_TASK_PRIO);
 	OSTaskCreate(period_taskFuntcion,(void*)0,(OS_STK*)&PERIOD_TASK_STK[PERIOD_STK_SIZE-1],PERIOD_TASK_PRIO);
 	OSTaskCreate(led_taskFuntcion,(void*)0,(OS_STK*)&LED_TASK_STK[LED_STK_SIZE-1],LED_TASK_PRIO);
+	OSTaskCreate(sx1278_send_task,(void *)0,(OS_STK*)&SX1278_TASK_STK[SX1278_STK_SIZE-1],SX1278_TASK_PRIO);
 	#endif
 	OSTaskCreate(period_taskFuntcion,(void*)0,(OS_STK*)&PERIOD_TASK_STK[PERIOD_STK_SIZE-1],PERIOD_TASK_PRIO);
 	OSTaskCreate(data_task,(void*)0,(OS_STK*)&DATA_TASK_STK[DATA_STK_SIZE-1],DATA_TASK_PRIO);
-	//OSTaskCreate(sx1278_send_task,(void *)0,(OS_STK*)&SX1278_TASK_STK[SX1278_STK_SIZE-1],SX1278_TASK_PRIO);
 	printf("system start \r\n");
 	OSStart(); //ucos启动
   /* Infinite loop */
@@ -178,8 +178,8 @@ void assert_failed(uint8_t* file, uint32_t line)
      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 
   /* Infinite loop */
-  //while (1)
-  //{}
+ // while (1)
+ //{}
 }
 #endif
 /*

+ 1 - 1
app/dlt/src/dlt645.c

@@ -29,7 +29,7 @@
 int dlt645_receive_msg(dlt645_t *ctx, uint8_t *msg, uint16_t len, uint32_t code, dlt645_protocal protocal)
 {
     int msg_len = ctx->read(ctx, msg, len);
-
+	
     if (protocal == DLT645_1997)
     {
         return dlt645_1997_recv_check(msg, msg_len, ctx->addr, code);

+ 8 - 12
app/modbus/mmodbus.c

@@ -73,18 +73,14 @@ void mmodbus_callback(void)
       USART_ReceiveData(_MMODBUS_USART);
   }
 
-		if ((mmodbus.rxIndex > 0) && RESET != USART_GetFlagStatus(_MMODBUS_USART, USART_FLAG_IDLE))
-		{	
-			uint8_t i;
-			//USART_ReceiveData(_MMODBUS_USART);		
-			mmodbus.done = 1;
-			
-			i = _MMODBUS_USART->SR;
-			i = _MMODBUS_USART->DR;
-//			USART_ClearFlag(_MMODBUS_USART,USART_FLAG_RXNE);
-			
-			return;
-		}
+	if ((mmodbus.rxIndex > 0) && RESET != USART_GetFlagStatus(_MMODBUS_USART, USART_FLAG_IDLE))
+	{	
+		uint8_t i;	
+		mmodbus.done = 1;			
+		i = _MMODBUS_USART->SR;
+		i = _MMODBUS_USART->DR;		
+		return;
+	}
 		else
 		{
 			USART_GetITStatus(_MMODBUS_USART, USART_FLAG_RXNE);

+ 1 - 1
app/mqtt/MQTTClient.h

@@ -3,7 +3,7 @@
 
 #include "stdint.h"
 
-#define  MQTT_SENDBUF_LENGTH   1024
+#define  MQTT_SENDBUF_LENGTH   10 * 1024
 #define  MQTT_RECVBUF_LENTH   512
 
 extern uint8_t mqtt_sendBuf[MQTT_SENDBUF_LENGTH];