zhao006 7 maanden geleden
bovenliggende
commit
59516250cc

+ 0 - 0
app/HARDWARE/includes/TIM.h


+ 52 - 0
app/HARDWARE/sources/TIM.c

@@ -0,0 +1,52 @@
+#include "stm32f2xx.h"
+#include "TIM.h"
+
+
+void TIM2_Init(void)
+{
+    // 使能定时器2时钟
+    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
+    
+    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
+    
+    // 配置定时器参数
+    TIM_TimeBaseStructure.TIM_Prescaler = 8399;  // 预分频器,即84MHz/8400=10kHz的计数频率
+    TIM_TimeBaseStructure.TIM_Period = 10000;   // 设定自动重装载寄存器周期的值,计数10,000次为1s
+    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
+    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
+    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
+
+    // 启动定时器2
+    TIM_Cmd(TIM2, ENABLE);
+}
+
+void TIM2_Start(void)
+{
+    // 启动定时器2的中断
+    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
+    
+    // 启用定时器2中断
+    NVIC_InitTypeDef NVIC_InitStructure;
+    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
+    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
+    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
+    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
+    NVIC_Init(&NVIC_InitStructure);
+}
+
+void TIM2_Stop(void)
+{
+    // 关闭定时器2的中断
+    TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE);
+}
+
+void TIM2_IRQHandler(void)
+{
+    if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
+    {
+        // 处理定时器中断
+			
+        // 清除中断标志
+        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
+    }
+}

+ 20 - 0
app/HARDWARE/sources/log.c

@@ -15,6 +15,26 @@
 #include "stdlib.h"
 #include "malloc.h"
 
+///重定向c库函数printf到串口,重定向后可使用printf函数
+int fputc(int ch, FILE *f)
+{
+		/* 发送一个字节数据到串口 */
+		USART_SendData(USART_485, (uint8_t) ch);
+		
+		/* 等待发送完毕 */
+		while (USART_GetFlagStatus(USART_485, USART_FLAG_TXE) == RESET);	
+		return (ch);
+}
+
+///重定向c库函数scanf到串口,重写向后可使用scanf、getchar等函数
+int fgetc(FILE *f)
+{
+		/* 等待串口输入数据 */
+		while (USART_GetFlagStatus(USART_485, USART_FLAG_RXNE) == RESET);
+
+		return (int)USART_ReceiveData(USART_485);
+}
+
 static OS_EVENT  *logMutex;
 uint8_t perr;
 void LogPrint(logLevel_t logLevel,const char *file, const char *func, const int line, char * fmt, ...)

+ 9 - 9
app/HARDWARE/sources/udp_send.c

@@ -54,15 +54,15 @@ void udp_log_close()
 	sockfd=-1;
 }
 
-int fputc(int ch, FILE *f)
-{
-	if(sockfd>=0)
-	{
-		sendto(sockfd, &ch, 1, 0, (struct sockaddr *)&remote_addr,sizeof(remote_addr));
-    return ch;
-	}
-	return 1;
-}
+//int fputc(int ch, FILE *f)
+//{
+//	if(sockfd>=0)
+//	{
+//		sendto(sockfd, &ch, 1, 0, (struct sockaddr *)&remote_addr,sizeof(remote_addr));
+//    return ch;
+//	}
+//	return 1;
+//}
 
 int udp_send_printf(char *p)
 {

+ 10 - 2
app/HARDWARE/sources/usart.c

@@ -78,7 +78,7 @@ void DEBUG_USART_IRQHandler(void)
 		DMA_Cmd(DEBUG_USART_DMA_STREAM, ENABLE);
 		//˝ÓĘŐ×´ĚŹÖĂÎť
 		UART6_RX_STAT=1;
-		
+
 	}
 }
 
@@ -264,7 +264,7 @@ void USART_485_config()
 	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
 	GPIO_Init(GPIOC, &GPIO_InitStruct);
 
-	
+
   USART_InitStruct.USART_BaudRate = USART_485_BAUDRATE;
   USART_InitStruct.USART_WordLength = USART_WordLength_8b;
   USART_InitStruct.USART_StopBits = USART_StopBits_1;
@@ -273,6 +273,14 @@ void USART_485_config()
   USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
   USART_Init(USART_485, &USART_InitStruct);
 
+//  USART_InitStruct.USART_BaudRate = 2400;
+//  USART_InitStruct.USART_WordLength = USART_WordLength_9b;
+//  USART_InitStruct.USART_StopBits = USART_StopBits_1;
+//  USART_InitStruct.USART_Parity = USART_Parity_Even;
+//  USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
+//  USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
+//  USART_Init(USART_485, &USART_InitStruct);
+
   USART_Cmd(USART_485, ENABLE);
 	
 	USART_ClearFlag(USART_485, USART_FLAG_TC);

File diff suppressed because it is too large
+ 170 - 136
app/MDKProject/lora_gateway.uvoptx


+ 11 - 0
app/MDKProject/lora_gateway.uvprojx

@@ -10,6 +10,7 @@
       <TargetName>Target 1</TargetName>
       <ToolsetNumber>0x4</ToolsetNumber>
       <ToolsetName>ARM-ADS</ToolsetName>
+      <pArmCC>5060960::V5.06 update 7 (build 960)::.\ARMCC</pArmCC>
       <pCCUsed>5060960::V5.06 update 7 (build 960)::.\ARMCC</pCCUsed>
       <uAC6>0</uAC6>
       <TargetOption>
@@ -459,6 +460,16 @@
               <FileType>1</FileType>
               <FilePath>..\System\source\sys_sx1278.c</FilePath>
             </File>
+            <File>
+              <FileName>lte.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\System\source\lte.c</FilePath>
+            </File>
+            <File>
+              <FileName>LTE_MQTT.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\System\source\LTE_MQTT.c</FilePath>
+            </File>
           </Files>
         </Group>
         <Group>

BIN
app/OBJ/lora_gateway.axf


File diff suppressed because it is too large
+ 18801 - 18494
app/OBJ/lora_gateway.hex


+ 22 - 0
app/System/includes/LTE_MQTT.h

@@ -0,0 +1,22 @@
+#ifndef __LTE_MQTT_H
+#define __LTE_MQTT_H
+
+#define client_idx  0 // MQTT 客户端标识符 0~5
+#define keepalive   60 // 保活时间   范围:0~3600;默认值:120;单位:秒
+#define MQTT_3  3   // version 3.1
+#define MQTT_4  4   // version 3.1.1
+			  
+#define clientid  "test"
+#define username1  "PC"
+#define password  "1234"
+
+
+#define NETSUCCESS 0
+#define NETERR -1				//打开网络失败
+#define PARAMERR 1			//参数错误
+#define IDFERR 2				//MQTT 标识符被占用
+#define PDPERR 3				//激活 PDP 失败
+#define DNSERR 4				//域名解析失败
+#define DISCONERR 5  		//网络断开导致错误
+
+#endif

+ 34 - 0
app/System/includes/lte.h

@@ -0,0 +1,34 @@
+#ifndef  __LTE_H
+#define  __LTE_H
+
+#include "usart.h"
+#include "string.h"
+
+#include "stdint.h"
+#include "string.h"
+#include "ucos_ii.h"
+#include "malloc.h"
+#include "myFile.h"
+#include "gateway_message.h"
+
+
+#define LTE_USART            USART6
+
+#define LTE_RxCounter  		UART6_RxCounter	//记录接收数据的个数
+#define LTE_RxBuff				UART6_RX_BUF		//指定接收数据缓冲区的首地址
+#define LTE_RXBUFF_SIZE		BUFF_SIZE	//指定接收缓冲区的大小
+
+#define SUCCESS  0
+#define ERROR  	 1
+#define TIMEOUT  2
+#define URCERROR 3
+#define EPSERROR 4
+
+
+int LTE_SendCmd(char *cmd, char* reply, int timeout);
+int LTE_init(void); // 设备初始化
+int LTE_http(void); // http初始化
+int LTE_send_URL(char* url); 
+int LTE_HTTP_get(); 
+
+#endif

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

@@ -23,7 +23,7 @@
 //#define  MQTT_SERVER_ADDR  "36.134.23.11"
 //#define  MQTT_SERVER_PORT  1883
 
-#define  CLIENT_ID     "stm32f207test"       //
+#define  CLIENT_ID     "stm32f207DTtest0002"       
 //#define  USER_NAME     "emqxxx"     
 //#define  PASSWORD      "123456" 
 //#define  USER_NAME     NULL  
@@ -36,7 +36,6 @@
 //#define  TOPICTEST     "/device/cxw0817111032"
 
 
-
 extern int mqtt_connectFlag;
 extern OS_EVENT *mqtt_sendMseeageMbox;
 extern OS_EVENT *mqtt_recvMseeageMbox;

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

@@ -15,6 +15,6 @@ void write_modbus_data(char* buf);
 void send_mqtt(char*buf);
 void find_difference(char* data1, char* data2, char* string);
 int read_device_data1(DEVICE_PARAMS *current_device,char* buf);
-int read_device_data2(DEVICE_PARAMS *current_device,char* buf);
-
+int read_device_data(DEVICE_PARAMS *current_device,char* buf);
+void mqtt_to_device(void);
 #endif

+ 235 - 0
app/System/source/LTE_MQTT.c

@@ -0,0 +1,235 @@
+#include "LTE_MQTT.h"
+#include "lte.h"
+
+
+// 配置mqtt参数
+int MQTT_config()
+{
+		u16 cat1_timeout = 0;
+		char configMessage[64];
+    snprintf(configMessage, sizeof(configMessage), "AT+QMTCFG=\"version\",%d,%d \r\n",client_idx, MQTT_4);
+    while(LTE_SendCmd(configMessage,"OK", 1000)){
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+		
+		cat1_timeout = 0;
+		memset(configMessage, 0, sizeof(configMessage));
+		snprintf(configMessage, sizeof(configMessage), "AT+QMTCFG=\"keepalive\",%d,%d \r\n",client_idx, MQTT_4);
+    while(LTE_SendCmd(configMessage,"OK", 1000)){
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+		// 配置需求参数
+		
+		
+	}
+
+// 连接MQTT- 接收模式
+int MQTT_recv_connect()
+{
+		u16 cat1_timeout = 0;
+		// 选择模式
+    while(LTE_SendCmd("AT+QMTCFG=\"recv/mode\",1,0,1 \r\n","OK", 1000)){
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+	
+		cat1_timeout = 0;
+    while(LTE_SendCmd("AT+QMTOPEN=? \r\n","OK", 1000)){
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+		// 打开mqtt
+		cat1_timeout = 0;
+		char message[64];
+    snprintf(message, sizeof(message), "AT+QMTOPEN=%d,\"183.162.218.20 \",1883 \r\n",client_idx);
+    while(LTE_SendCmd(message,"OK", 1000)){
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+		
+		cat1_timeout = 0;
+    while(LTE_SendCmd("AT+QMTOPEN=? \r\n","OK", 1000)){
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;
+        }
+    }
+		// 连接服务器
+		cat1_timeout = 0;
+		memset(message, 0, sizeof(message));
+    snprintf(message, sizeof(message), "AT+QMTCONN=%d,%s,%s,%s \r\n",client_idx,
+																							(char*)&clientid,(char*)&username1,(char*)&password);
+    while(LTE_SendCmd(message,"OK", 1000)){
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+		int result,index;
+		sscanf((char*)&LTE_RxBuff, "+QMTOPEN:  %d,%d, ", &index, &result);
+
+#if 0
+		// 拿返回的result做判断
+		switch(result){
+			case NETSUCCESS:
+				break;
+			case NETERR:
+				return -1;
+			case 1:
+				return -1;
+			
+		}		
+#endif
+		
+		// 订阅主题
+		cat1_timeout = 0;
+		memset(message, 0, sizeof(message));
+    snprintf(message, sizeof(message), "AT+QMTSUB=%d,msgid,topic,qos \r\n",client_idx);
+    while(LTE_SendCmd(message,"OK", 1000)){
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+		//发布消息
+		cat1_timeout = 0;
+		memset(message, 0, sizeof(message));
+    snprintf(message, sizeof(message), "AT+QMTPUBEX=%d,msgid,qos,retain,topic,length \r\n",client_idx);
+    while(LTE_SendCmd(message,"OK", 1000)){
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+		// 从缓存中读取消息
+		cat1_timeout = 0;
+		memset(message, 0, sizeof(message));
+    snprintf(message, sizeof(message), "AT+QMTRECV=%d,recv_id \r\n",client_idx);
+    while(LTE_SendCmd(message,"OK", 1000)){
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+		int client_idx1,msgid,payload_len;
+		char topic[80],payload[512];
+		sscanf((char*)&LTE_RxBuff, "+QMTRECV:%d,%d,%s,%d],%s ", 
+					&client_idx1,	&msgid,	(char*)&topic,	&payload_len,	(char*)&payload);
+		
+		
+}
+
+// 连接MQTT- 发送模式
+int MQTT_send_connect()
+{
+		u16 cat1_timeout = 0;
+		// 选择模式
+    while(LTE_SendCmd("AT+QMTCFG=\"send/mode\",1,0,1 \r\n","OK", 1000)){
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+	
+		cat1_timeout = 0;
+    while(LTE_SendCmd("AT+QMTOPEN=? \r\n","OK", 1000)){
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+		// 打开mqtt
+		cat1_timeout = 0;
+		char message[64];
+    snprintf(message, sizeof(message), "AT+QMTOPEN=%d,\"183.162.218.20 \",1883 \r\n",client_idx);
+    while(LTE_SendCmd(message,"OK", 1000)){
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+		
+		cat1_timeout = 0;
+    while(LTE_SendCmd("AT+QMTOPEN=? \r\n","OK", 1000)){
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+		// 连接服务器
+		cat1_timeout = 0;
+		memset(message, 0, sizeof(message));
+    snprintf(message, sizeof(message), "AT+QMTCONN=%d,clientid,username,password \r\n",client_idx);
+    while(LTE_SendCmd(message,"OK", 1000)){
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+		// 订阅主题
+		cat1_timeout = 0;
+		memset(message, 0, sizeof(message));
+    snprintf(message, sizeof(message), "AT+QMTSUB=%d,msgid,topic,qos \r\n",client_idx);
+    while(LTE_SendCmd(message,"OK", 1000)){
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+		//发布消息
+		cat1_timeout = 0;
+		memset(message, 0, sizeof(message));
+    snprintf(message, sizeof(message), "AT+QMTPUBEX=%d,msgid,qos,retain,topic,length \r\n",client_idx);
+    while(LTE_SendCmd(message,"OK", 1000)){
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+		// 从缓存中读取消息
+		cat1_timeout = 0;
+		memset(message, 0, sizeof(message));
+    snprintf(message, sizeof(message), "AT+QMTRECV=%d,recv_id \r\n",client_idx);
+    while(LTE_SendCmd(message,"OK", 1000)){
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+		int client_idx1,msgid,payload_len;
+		char topic[80],payload[5000];
+		sscanf((char*)&LTE_RxBuff, "+QMTRECV:%d,%d,%s,%d],%s ", 
+					&client_idx1,	&msgid,	(char*)&topic,	&payload_len,	(char*)&payload);
+		
+		
+}

+ 0 - 80
app/System/source/gateway_message.c

@@ -311,84 +311,4 @@ int extract_substring(const char *input_string, const char *start_token, const c
     return 1;
 }
 
-//void addparams(DEVICE_PARAMS *device, char* pbuf){
-//	GATEWAY_PARAMS* get;
-//	get = get_gateway_config_params();
-//	switch (device->protocol)
-//	{
-////			case DLT645_97:
-////			case DLT645_07:
-////			{
-////					DLT_READ_DATA *read_dlt645_data= mymalloc(SRAMEX, sizeof(DLT_READ_DATA));
-////					read_dlt645_data->Identification = parseIntField(pbuf, "\"identifier645\":");
-////					parseStringField(pbuf, "\"identifier\":\"", (char *)&read_dlt645_data->keyword);
-////					char *string = mymalloc(SRAMIN, 13);
-////					parseStringField(pbuf, "\"deviceID645\":\"", string);
-////					for (int j = 0; j < 6; j++)
-////					{
-////							uint8_t byte;
-////							sscanf((const char *)&string[j * 2], "%2hhx", &byte);
-////							read_dlt645_data->deviceID645[j]=byte;
-////					}
-////					myfree(SRAMIN, string);
-////					if (readData->read_dlt645_data == NULL)
-////					{
-////							readData->read_dlt645_data = read_dlt645_data;
-////					}
-////					else
-////					{
-////							DLT_READ_DATA *current = readData->read_dlt645_data;
-////							while (current->nextParams != NULL)
-////							{
-////									current = current->nextParams;
-////							}
-////							current->nextParams = read_dlt645_data;
-////					}
-////				}
-////						
-////					break;
-//			case MODBUS:
-//			{
-//						parseStringField(pbuf,"\"deviceId\":\"",(char *)&get->device_params->deviceID);
-//						get->device_params->params->gateway_read_modbus_command->value = parseIntField(pbuf, "\"power\":");
-//				
-//						if (readData->read_modbus_data == NULL)
-//						{
-//								readData->read_modbus_data = read_modbus_command;
-//						}
-//						else
-//						{
-//								MODBUS_READ_DATA *current = readData->read_modbus_data;
-//								while (current->nextParams != NULL)
-//								{
-//										current = current->nextParams;
-//								}
-//								current->nextParams = read_modbus_command;
-//						}
-//					}
-//					break;
-//			default:
-//					break;
-//	}
-//}
 
-
-
-//void analysis_read_data(DEVICE_PARAMS *device, char* buf){	
-////		READ_DATA* readData = mymalloc(SRAMEX, sizeof(READ_DATA));
-//		char* pbuf = mymalloc(SRAMEX, strlen(buf));
-//		memcpy(pbuf,buf,strlen(buf));
-//		while(1)
-//		{				
-////			addparams(device, readData ,pbuf);
-//			addparams(device, pbuf);
-//			pbuf = strstr(pbuf, "}");
-//			pbuf[0]='A';
-//			pbuf++;
-//			if (pbuf[0] == ']')
-//			{
-//				myfree(SRAMEX, pbuf);
-//				break;														
-//			}
-//		}	
-//}

+ 324 - 0
app/System/source/lte.c

@@ -0,0 +1,324 @@
+#include "lte.h"
+
+uint8_t UART3_RxCounter = 0;
+
+//定义通过串口6发送AT指令命令
+// timeout *= 100ms
+int LTE_SendCmd(char *cmd, char* reply, int timeout) 
+{
+	UART3_RxCounter = 0; 
+	memset(LTE_RxBuff, 0, LTE_RXBUFF_SIZE); 
+	Usart_SendString(USART6, cmd);
+	while(timeout--) {
+		OSTimeDly(100);
+		if(strstr((char*)&LTE_RxBuff, reply))
+			break;
+		printf("time = %d ", timeout);
+	}
+	printf("\r\n");
+	if(timeout <= 0)
+		return -1; 
+	return 0; 
+}
+
+int LTE_init()
+{
+		char* rss_str;
+		int rssi,res;
+		u16 cat1_timeout = 0;
+		
+		while(LTE_SendCmd("AT+IPR=115200\r\n","OK", 3)){
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            printf(" IPR false\r\n");
+            return TIMEOUT;   
+        }
+    }
+    cat1_timeout = 0;
+   
+		while(LTE_SendCmd("AT&W\r\n","OK", 3)){
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            printf(" IPR false\r\n");
+            return TIMEOUT;   
+        }
+    }
+    cat1_timeout = 0;
+		
+		while(LTE_SendCmd("AT\r\n","OK", 2)){
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            printf(" uart false\r\n");
+            return TIMEOUT;   
+        }
+    }
+    cat1_timeout = 0;
+    printf("uart ok\r\n");
+    while(LTE_SendCmd("AT+CPIN?\r\n","READY", 50)){// 最大延时5s
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }	
+    printf("simcard ok\r\n");
+		
+		cat1_timeout = 0;
+    while(LTE_SendCmd("AT+CSQ\r\n","+CSQ", 3)){// 最大延时300ms
+				rss_str = strstr((char*)&LTE_RxBuff,"+CSQ:");
+				if (!rss_str) {
+						return 1;
+					}
+				sscanf(rss_str, "+CSQ:%d,%d", &rssi, &res);
+				if(rssi != 99)
+						printf("RSSI is %d\r\n",rssi);
+						memset(LTE_RxBuff, 0, LTE_RXBUFF_SIZE);
+						return 0;
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+   
+		cat1_timeout = 0;
+    while(LTE_SendCmd("AT+CEREG?\r\n","0,1", 2)){// 最大延时200ms
+				if (strstr((char*)LTE_RxBuff, "0,1")){  
+						printf("\r\n%s\r\n", LTE_RxBuff);
+						memset(LTE_RxBuff, 0, LTE_RXBUFF_SIZE);
+						return 0;
+				}
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+    printf("网络注册 ok\r\n");
+		
+    cat1_timeout = 0;
+    while(LTE_SendCmd("AT+CGATT\r\n","+CGATT: 1", 100)){ // 最大延时140s
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+    printf(" 网络附着 ok\r\n");
+	
+	  cat1_timeout = 0;
+    while(LTE_SendCmd("AT+QSCLK=0 \r\n","OK", 3)){ // 最大延时300ms
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+    printf("close sleep mode\r\n");
+}
+
+int LTE_TCP_init()
+{
+	u16 cat1_timeout = 0;
+	//配置PDP上下文ID为1
+	while(LTE_SendCmd("AT+QICFG=\"transpktsize\"50\r\n","OK", 3)){// 最大延时300ms
+		OSTimeDly(1);
+		cat1_timeout ++;
+		if(cat1_timeout >= 2000){
+				return TIMEOUT;   
+		}
+  }
+	return 0;
+}
+
+int LTE_http()
+{
+    u16 cat1_timeout = 0;
+		//配置PDP上下文ID为1
+    while(LTE_SendCmd("AT+QHTTPCFG=\"contextid\",1\r\n","OK", 3)){// 最大延时300ms
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+		cat1_timeout = 0;
+		//启用输出 HTTP 响应头信息
+		while(LTE_SendCmd("AT+QHTTPCFG=\"responseheader\",1\r\n","OK", 3)){// 最大延时300ms
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+    cat1_timeout = 0;
+    printf("QHTTPCFG ok\r\n");
+		//查询 PDP 上下文状态
+    while(LTE_SendCmd("AT+QIACT?\r\n","OK", 100)){//最大延时150S
+				OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+    cat1_timeout = 0;
+    printf("PDP_CHECK one ok\r\n");
+		
+   while(LTE_SendCmd("AT+QHTTPCFG=\"contenttype\",1\r\n","OK", 3)){// 最大延时300ms
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT; 
+        }
+    }
+  
+    cat1_timeout = 0;
+    printf("CFG ok\r\n");
+
+    /*
+    "AT+QICSGP=1,1,\"CMNET\",\"\",\"\",1\r\n"
+    APN 联通:UNINET   移动:CMNET   电信:CTNET
+    */
+    while(LTE_SendCmd("AT+QICSGP=1,1,\"CMNET\",\"\",\"\",1\r\n","OK", 3000)){// 最大响应时间  无
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+    cat1_timeout = 0;
+    printf("PDP_CONFIG ok\r\n");
+		// 使配置生效
+    while(LTE_SendCmd("AT+CFUN=1 \r\n","OK", 100)){// 最大响应时间15S
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+    cat1_timeout = 0;
+    printf("PDP_CONFIG ok\r\n");
+		//查询 PDP 上下文状态
+    while(LTE_SendCmd("AT+QIACT?\r\n","+QIACT", 100)){//最大延时150S
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+    cat1_timeout = 0;
+    printf("PDP_CHECK two ok\r\n");
+		
+		 //激活 PDP 上下文 1
+     while(LTE_SendCmd("AT+QIACT=1\r\n","OK", 500)){
+         OSTimeDly(1);
+         cat1_timeout ++;
+         if(cat1_timeout >= 2000){
+             return TIMEOUT;   
+         }
+		}
+     cat1_timeout = 0;
+     printf("PDP_激活 ok\r\n");
+		return SUCCESS;
+}
+/*
+*HTTP(S)服务器的 URL 必须以 http://或 https://开头,表示访问 HTTP 或 HTTPS 服务器
+*/
+int LTE_send_URL(char* url)
+{
+    u16 cat1_timeout = 0;
+    char message[32];// 根据网址具体长度决定
+    snprintf(message, sizeof(message), "AT+QHTTPURL=%d,%d\r\n", strlen(url), 80);
+    while(LTE_SendCmd(message,"CONNECT", 1000)){// 返回connect,切换到透传模式
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+    cat1_timeout = 0;
+    printf("ready to send url\r\n");
+    while(LTE_SendCmd(url,"OK", 5000)){
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+    cat1_timeout = 0;
+    printf("url send OK\r\n");	
+	return SUCCESS; //成功
+}
+
+int LTE_HTTP_get()
+{
+	u16 cat1_timeout = 0;
+	int err,httprspcode,content_length,fd;
+__start:
+	 //发送 HTTP GET 请求,最大响应时间为 80 秒
+   while(LTE_SendCmd("AT+QHTTPGET=80\r\n","OK", 80)){
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+	 sscanf((char*)&LTE_RxBuff, "+QHTTPGET: %d,%d,%d ", &err, &httprspcode, &content_length);
+	 if(200 == httprspcode){
+		 printf("connect OK\r\n");	
+	 }
+	 else{
+		 goto __start;
+	 }
+	 
+	 // 读取 HTTP 响应信息并将其储存到 UFS 文件中
+		cat1_timeout = 0;
+		while(LTE_SendCmd("AT+QHTTPREADFILE=\"UFS:test.txt\",80 \r\n","OK", 100)){
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+				
+		cat1_timeout = 0;
+		// 打开文件
+		while(LTE_SendCmd("AT+QFOPEN=\"UFS:test.txt\",2\r\n","OK", 100)){
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+		sscanf((char*)&LTE_RxBuff, "+QFOPEN:%d ", &fd);// 获取文件指针
+		
+		// 读取文件
+		char readMessage[32];
+		memset(LTE_RxBuff, 0, LTE_RXBUFF_SIZE); 
+    sprintf(readMessage,"AT+QFREAD=%d,%d\r\n", fd,sizeof(LTE_RxBuff));
+		while(LTE_SendCmd(readMessage,"OK", 500)){// 最大响应时间5S
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+		
+		
+		// 解析数据 
+		addGatewayParams((char*)&LTE_RxBuff);
+		
+		cat1_timeout = 0;
+		// 关闭文件
+		char closeCMD[32];
+		sprintf(closeCMD,"AT+QFCLOSE=%d\r\n", fd);
+		while(LTE_SendCmd(closeCMD,"OK", 100)){
+        OSTimeDly(1);
+        cat1_timeout ++;
+        if(cat1_timeout >= 2000){
+            return TIMEOUT;   
+        }
+    }
+}

+ 12 - 8
app/System/source/sys_mqtt.c

@@ -24,7 +24,7 @@ int mqtt_userConnect(void)
 {
 	GATEWAY_PARAMS* get;
 	get = get_gateway_config_params();
-	char* MQTT_SERVER_ADDR = (char*)get->host;
+	char* MQTT_SERVER_ADDR = "36.134.23.11";//(char*)get->host;
 	int MQTT_SERVER_PORT = get->port;
 	int sock = -1;
 	
@@ -46,7 +46,7 @@ int mqtt_userConnect(void)
 		return -1;
 	}
   MQTT_PRINTF("connect mqtt server success \r\n");
-	LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "connect mqtt server success");
+//	LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "connect mqtt server success");
 	return sock;
 }
 
@@ -60,6 +60,7 @@ int mqtt_userSubscribeTopic(int sock)
 	GATEWAY_PARAMS* get;
 	get = get_gateway_config_params();
 	char* TOPICPC = (char*)get->commandTopic;
+//	char* TOPICPC = "/device/DTtest0003/command";
 	return mqtt_subscribeTopic(sock, TOPICPC, 2);
 }
 
@@ -162,12 +163,13 @@ int mqtt_userReceiveMessage(int sock, int type, uint8_t *pbuf, int len)
 *        -2: 组建发送数据时发送错误
 *************************************************************/
 char pubJsonString[jsonMaxSize];
-//char pubJsonStringCopy[jsonMaxSize];
+char pubJsonStringCopy[jsonMaxSize];
 int mqtt_userSendMessage(int sock, int boxMsg)
 {
 	GATEWAY_PARAMS* get;
 	get = get_gateway_config_params();
 	char* TOPIC = (char*)get->messageTopic;
+//	char* TOPIC = "/device/DTtest0003";
 	int rc = 1;	
 	//用户发送的数据
 	if((boxMsg & 0xf0000000) == 0x20000000)
@@ -240,15 +242,16 @@ __MQTT_START:
 		MQTT_PRINTF("subscribe success \r\n");
 //		LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "subscribe success");
 	}
-	
+
 	//3.循环发送数据
 	while(1)
-	{	
-		mboxMsg = OSMboxPend(mqtt_sendMseeageMbox, 60000, &err);
+	{
+		mboxMsg = OSMboxPend(mqtt_sendMseeageMbox, 2000, &err);
 		if(OS_ERR_NONE == err)
 		{
 			//接收到了 网络异常消息
-			if((*(unsigned int *)mboxMsg) == MBOX_NETWORK_ERROR) goto __MQTT_START;
+			if((*(unsigned int *)mboxMsg) == MBOX_NETWORK_ERROR) 
+				goto __MQTT_START;
 			else               
 			{
 				if(mqtt_userSendMessage(mysock, *(unsigned int *)mboxMsg) == -1)
@@ -259,7 +262,8 @@ __MQTT_START:
 		else //超时没有发送数据包,发送心跳包
 		{
 			rc = mqtt_pingReq(mysock);
-			if(rc == -1) goto __MQTT_START;
+			if(rc == -1) 
+				goto __MQTT_START;
 		}
 		OSTimeDly(100);
 	}

+ 127 - 157
app/System/source/task.c

@@ -25,6 +25,7 @@ char string[512];
 uint8_t read_cnt = 0;
 uint8_t count = 0;
 uint8_t jsonCunt = 1;	
+int ID = 0;
 /*
 *********************************************************************************************************
 *	函 数 名: void data_task(void *pdata)
@@ -34,15 +35,13 @@ uint8_t jsonCunt = 1;
 *********************************************************************************************************
 */
 void data_task(void *pdata)
-{
-		
+{		
 		OS_CPU_SR cpu_sr;
 		pdata = pdata;
 
 		dlt645_init(100);
 		mmodbus_init(1);
 		
-		
 		char *device_config_json = mymalloc(SRAMEX, 9 * 1024);
 		read_file("device.txt", device_config_json);
 		addGatewayParams(device_config_json);
@@ -51,7 +50,7 @@ void data_task(void *pdata)
 		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);
+//		Config_485_Port(get->baudrate, get->dataBits, get->stopBit, get->checkBit, get->flowControl);
 		char *buf = mymalloc(SRAMEX, 9 * 1024);	// 接收读取的数据
 		memset(buf, 0, 9 * 1024);		
 		while (current_device!=NULL)
@@ -59,28 +58,31 @@ void data_task(void *pdata)
 			time1 = GetCurrentTime();
 			if(mqtt_connectFlag)
 			{
-					if(jsonCunt || time2  <= time1 - (10 * 1000))// 10s进行一次全数据发送
+					if(jsonCunt || time2  <= time1 - ( 60 * 1000))// 60s进行一次全数据发送
 					{
-							read_device_data1(current_device, buf);
-							send_mqtt(buf);
-							jsonCunt = 0;
-							memset(buf,0,strlen(buf));	
+							read_device_data(current_device, buf);
+							send_mqtt(buf);	
 							current_device=get->device_params;	
 							time2 = GetCurrentTime();
-//							LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "data for all");
+							jsonCunt = 0;
+							count = 0;
 					}
 					else
 					{
-						read_device_data2(current_device, buf);
-						if(count > 0)// count检测buf内是否含有数据
-						{
-							send_mqtt(buf);
-							memset(buf,0,strlen(buf));	
+						read_device_data(current_device, buf);
+						if(count > 0)// count检测是否含有数据
+						{			
+							send_mqtt(string);
+							memset(string,0,strlen(string));
 							current_device=get->device_params;
 							count = 0;
-//							LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "different data");
+						}else
+						{
+//							LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "no data");
 						}
 					}	
+					mqtt_to_device();
+					memset(buf,0,strlen(buf));
 			}		
 		OSTimeDly(100);
 		}
@@ -119,61 +121,41 @@ void find_diff(char* buf, char* string) {
 *	返 回 值: 1: 成功 0:失败
 *********************************************************************************************************
 */
-int read_device_data1(DEVICE_PARAMS *device, char* buf)
+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;	
-
+	
 		while(current_device->params != NULL)
 		{				
 				if (current_device->protocol == MODBUS_READ)
 				{
 						protocol_485=1;
+						uint8_t state;
 						uint16_t data[currentModbusParams->registerByteNum /2]; // modbus寄存器长度
+						uint8_t data1[currentModbusParams->registerByteNum /2];
 						mmodbus_set16bitOrder(current_device->MDBbigLittleFormat);
-						if (currentModbusParams->functionCode == 0x03 | currentModbusParams->functionCode == 0x01)
+						// 读水阀状态
+						if(currentModbusParams->functionCode == 0x01)
 						{
-								bool success = mmodbus_readHoldingRegisters16i(currentModbusParams->slaveAddress,
-																		 currentModbusParams->registerAddress,
+							bool success = mmodbus_readCoil(currentModbusParams->slaveAddress,
 																		 currentModbusParams->registerByteNum /2,
-																		 data);
-
-								if (success)
+																		 data1);
+							if(success)
+							{
+								uint8_t value;
+								value = data1[0];
+								if(value == 0)
 								{
-										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 = value;
-
-										}
-										else
-										{
-												float convertedValue = (float)value / pow(10, currentModbusParams->decimalPoint);
-												currentModbusParams->value=convertedValue;
-										}
-										sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":%d},", 
-																							current_device->deviceID, currentModbusParams->keyword, value);									
+								sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":close},", 
+																									current_device->deviceID, currentModbusParams->keyword);
+								}else{
+									sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":open},", 
+																									current_device->deviceID, currentModbusParams->keyword);
 								}
-//								else
-//								{
-//										printf("read modbus register fail\n");
-//										return 0;
-//								}
-								
-								/* 每读完一个寄存器,进行message判断 */
-								mqtt_to_device();
-								
-								currentModbusParams = currentModbusParams->nextParams;
+							}
+							currentModbusParams = currentModbusParams->nextParams;
 								if (currentModbusParams == NULL)	
 								{
 										current_device = current_device->nextDevice;
@@ -183,100 +165,16 @@ int read_device_data1(DEVICE_PARAMS *device, char* buf)
 												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];
-						uint32_t dltValue;
-						
-						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)
-						{
-								rs = dlt645_read_data(&dlt645, currentDLT645Params->Identification, read_buf, DLT645_2007);
+								}		
 						}
-						else if (current_device->protocol == DLT645_1997)
-						{
-								rs = dlt645_read_data(&dlt645, currentDLT645Params->Identification, read_buf, DLT645_1997);
-						}
-						if (rs != -1)
-						{
-								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;
-								}
-															
-								dltValue = currentDLT645Params->data[0] << 24 | currentDLT645Params->data[1] << 16|
-																		currentDLT645Params->data[2] << 8  | currentDLT645Params->data[3];
-					
-								sprintf(buf + strlen(buf), "{\"identifier\":\"%s\",\"deviceID645\":\"%02x%02x%02x%02x%02x%02x\",\"identifier645\":%d,\"value\":%X}",
-																				currentDLT645Params->keyword, currentDLT645Params->deviceID645[0],
-																				currentDLT645Params->deviceID645[1],currentDLT645Params->deviceID645[2],
-																				currentDLT645Params->deviceID645[3],currentDLT645Params->deviceID645[4],
-																				currentDLT645Params->deviceID645[5],currentDLT645Params->Identification,dltValue);
-								
-			
-						}
-//						else
-//						{
-//								currentDLT645Params->rxLen = 0;
-//								printf("read DLT current data fail\n");
-//						}				
-								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;
-}
-
-
-int read_device_data2(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;	
-
-		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 (currentModbusParams->functionCode == 0x03)
 						{
+//								bool success = mmodbus_readHoldingRegisters16i(0x17,0x00,0x02,data);
 								bool success = mmodbus_readHoldingRegisters16i(currentModbusParams->slaveAddress,
-																		 currentModbusParams->registerAddress,
-																		 currentModbusParams->registerByteNum /2,
-																		 data);
+																															 currentModbusParams->registerAddress,
+																															 currentModbusParams->registerByteNum /2,
+																															 data);
 
 								if (success)
 								{
@@ -291,11 +189,16 @@ int read_device_data2(DEVICE_PARAMS *device, char* buf)
 										}
 										if((value - currentModbusParams->value) != 0)
 										{
+												count++;
+												sprintf(string + strlen(string), "{\"deviceId\":\"%s\",\"%s\":%d},", 
+																									current_device->deviceID, currentModbusParams->keyword, value);
+										}
+										else
+										{									
 												sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":%d},", 
 																									current_device->deviceID, currentModbusParams->keyword, value);
-												count++;
 										}
-											if (currentModbusParams->decimalPoint == 0)
+										if (currentModbusParams->decimalPoint == 0)
 										{
 												currentModbusParams->value = value;
 
@@ -318,7 +221,61 @@ int read_device_data2(DEVICE_PARAMS *device, char* buf)
 												return 1;
 										}
 								}												
-						}					
+						}
+						// 开关水阀
+						if(currentModbusParams->functionCode == 0x05)
+						{
+							bool success = mmodbus_writeCoil(currentModbusParams->slaveAddress,
+																							 currentModbusParams->registerByteNum /2,
+																							 state);
+							if(success)
+							{
+								
+									sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\": success},", 
+																									current_device->deviceID, currentModbusParams->keyword);
+							}
+							else{
+									sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\": fail},", 
+																									current_device->deviceID, currentModbusParams->keyword);
+							}
+							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;
+									}
+							}
+						}
+						// 写单个寄存器
+						if(currentModbusParams->functionCode == 0x06)
+						{
+							bool success = mmodbus_writeHoldingRegisters16i(currentModbusParams->slaveAddress,
+																		 currentModbusParams->registerAddress,
+																		 currentModbusParams->registerByteNum /2,
+																		 data);
+							if(success)
+							{
+								sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":write success},", 
+																									current_device->deviceID, currentModbusParams->keyword);
+							}
+							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)
 				{
@@ -360,13 +317,13 @@ int read_device_data2(DEVICE_PARAMS *device, char* buf)
 															
 								dltValue = currentDLT645Params->data[0] << 24 | currentDLT645Params->data[1] << 16|
 																		currentDLT645Params->data[2] << 8  | currentDLT645Params->data[3];
-					
+								
 								sprintf(buf + strlen(buf), "{\"identifier\":\"%s\",\"deviceID645\":\"%02x%02x%02x%02x%02x%02x\",\"identifier645\":%d,\"value\":%X}",
 																				currentDLT645Params->keyword, currentDLT645Params->deviceID645[0],
 																				currentDLT645Params->deviceID645[1],currentDLT645Params->deviceID645[2],
 																				currentDLT645Params->deviceID645[3],currentDLT645Params->deviceID645[4],
 																				currentDLT645Params->deviceID645[5],currentDLT645Params->Identification,dltValue);
-								
+								count++;
 			
 						}
 		
@@ -402,12 +359,16 @@ void write_modbus_data(char* cJSONstring)
 		DEVICE_PARAMS* current_device = get->device_params;
 		/* 利用cJSOn_Parse解析数据,获取各类型数据 */
 		cJSON *root = cJSON_Parse(cJSONstring);
-		const char *deviceId = cJSON_GetStringValue(cJSON_GetObjectItem(root, "deviceId"));	
+		const char *deviceId = cJSON_GetStringValue(cJSON_GetObjectItem(root, "deviceId"));
+		const cJSON *func = cJSON_GetObjectItemCaseSensitive(root, "function");
 		const cJSON *power = cJSON_GetObjectItemCaseSensitive(root, "power");
 		const cJSON *temp = cJSON_GetObjectItemCaseSensitive(root, "temp");
 		const cJSON *mode = cJSON_GetObjectItemCaseSensitive(root, "mode");
 		const cJSON *fan = cJSON_GetObjectItemCaseSensitive(root, "fan");
-		
+	
+		const cJSON *slaveAddress = cJSON_GetObjectItemCaseSensitive(root, "slaveAddress");
+		const cJSON *registerAddress = cJSON_GetObjectItemCaseSensitive(root, "registerAddress");
+		const cJSON *cmd = cJSON_GetObjectItemCaseSensitive(root, "cmd");
 		while(current_device)
 		{
 				char* device_ID = (char*)current_device->deviceID;
@@ -415,6 +376,13 @@ void write_modbus_data(char* cJSONstring)
 				if(!strcmp(device_ID,deviceId)) //匹配ID
 				{
 						OSTimeDly(100);
+					if(func->valueint == 5)
+					// 开关阀门
+					{
+						bool success = mmodbus_writeCoil(slaveAddress->valueint,registerAddress->valueint,cmd->valueint);
+					}
+					if(func->valueint == 6)
+					{
 					/* 写入寄存器操作 */
 						if(power)
 						{
@@ -446,6 +414,8 @@ void write_modbus_data(char* cJSONstring)
 																									currentModbusParams->registerAddress, 
 																									fan->valueint);					
 						}
+					}
+					
 				}
 				current_device = current_device->nextDevice;
 		}
@@ -473,7 +443,7 @@ void find_difference(char* buf, char* pubJsonStringCopy, char* string)
      // 利用strtok_r函数分割字符串,并逐一比较
     char* token1 = strtok_r((char*)data1, delimiter, &saveptr1);
     char* token2 = strtok_r((char*)data2, delimiter, &saveptr2);
-
+		memset(string,0,strlen(string));
     while (token1 != NULL && token2 != NULL) 
 		{
         if (strcmp(token1, token2) != 0) 
@@ -511,8 +481,8 @@ void find_difference(char* buf, char* pubJsonStringCopy, char* string)
 void send_mqtt(char*buf){
 		GATEWAY_PARAMS *get;
 		get= get_gateway_config_params();
-
-		sprintf(pubJsonString,"{\"DEVICEID\":\"%s\",\"data\":[%s]}",get->deviceId, buf);	// 组成要发送的json语句							
+		
+	sprintf(pubJsonString,"ID: %d, {\"DEVICEID\":\"%s\",\"data\":[%s]}",ID++,get->deviceId, buf);	// 组成要发送的json语句							
 	
 		int msg = MBOX_USER_PUBLISHQOS0;	
 		if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);

+ 7 - 6
app/USER/main.c

@@ -19,7 +19,7 @@
 #include "node_data_acquisition.h"
 #include "log.h"
 #include "tcp_server.h"
-
+#include "lte.h"
 
 #define UNIQUE_ID 0x1fff7a10
 
@@ -74,7 +74,7 @@ int main(void)
 {
 	int status;
 	load_unique();
-	sprintf(gatewayId,"DTtest0001");//DT8pd3ac6h  DTbma5ac6h  DTtest0001
+	sprintf(gatewayId,"DTtest0001");//DT8pd3ac6h  DTbma5ac6h  DTtest0001 DTrzts0001
 	NVIC_Configuration();
 	my_mem_init(SRAMEX);
 	my_mem_init(SRAMIN);
@@ -83,8 +83,12 @@ int main(void)
 	//nandflash并不用初始化,调用fafts时会初始化
 	NET_STATUS_LED_Config();
 	USART_485_config();	
-  USART_485_DE_TX();
+//  USART_485_DE_TX();
   USART_232_config();	
+	
+	DEBUG_USART_Config();
+	USART_DMA_Config();
+	
 	status = NAND_Init();
 	while(status){
 		NAND_Format();
@@ -117,8 +121,6 @@ void period_taskFuntcion(void *arg)
   OSStatInit();
 #endif
 
-	int time1,time2;
-	time1 = OSTimeGet();
 	lwIP_Init();
 
 	// 输出日志
@@ -126,7 +128,6 @@ void period_taskFuntcion(void *arg)
 //	tcp_server_init();
 	
 //	LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "system start");
-	time2 = OSTimeGet() - time1;
 	http_getDemo();
 	http_postDemo();
 

+ 8 - 0
app/dlt/port/dlt645_port.c

@@ -60,6 +60,14 @@ void dlt_callback()
 		temp=DLT645_USART->DR;
 		dlt645_port.done = 1;
 	}
+	if( RESET != USART_GetFlagStatus(DLT645_USART, USART_FLAG_IDLE))
+	{
+		uint8_t temp;                            
+		temp=DLT645_USART->SR;             //先读sr再读DR才能清除idle中断
+		temp=DLT645_USART->DR;
+		
+	}
+	
 	OSIntExit();
 }
 /**

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

@@ -212,7 +212,7 @@ int dlt645_2007_read_data(dlt645_t *ctx,
         DLT645_LOG("send data error!\n");
         return -1;
     }
-
+		 OSTimeDly(500);
     if (dlt645_receive_msg(ctx, read_buf, DL645_RESP_LEN, code, DLT645_2007) < 0)
     {
         DLT645_LOG("receive msg error!\n");

+ 5 - 5
app/modbus/mmodbus.c

@@ -118,7 +118,7 @@ bool mmodbus_sendRaw(uint8_t *data, uint16_t size, uint32_t timeout)
 	mmodbus.txBusy = 1;
 	memset(mmodbus.rxBuf, 0, _MMODBUS_RXSIZE);
 	mmodbus.rxIndex = 0;
-//	OSIntEnter();
+	OSIntEnter();
 	uint32_t startTime = gettick();
 	GPIO_WriteBit(_MMODBUS_CTRL_GPIO, _MMODBUS_CTRL_PIN,1);
 	mmodbus_delay(1);
@@ -128,7 +128,7 @@ bool mmodbus_sendRaw(uint8_t *data, uint16_t size, uint32_t timeout)
 		while (USART_GetFlagStatus(_MMODBUS_USART, USART_FLAG_TXE) == RESET);
 	} 
 	while (RESET == USART_GetFlagStatus(_MMODBUS_USART, USART_FLAG_TC));
-//	OSIntExit();
+	OSIntExit();
 	GPIO_WriteBit(_MMODBUS_CTRL_GPIO, _MMODBUS_CTRL_PIN,0);
 	mmodbus.done=0;
 	mmodbus.txBusy = 0;
@@ -502,11 +502,11 @@ bool mmodbus_writeCoil(uint8_t slaveAddress, uint16_t number, uint8_t data)
   txData[1] = MModbusCMD_WriteSingleCoil;
   txData[2] = (number & 0xFF00) >> 8;
   txData[3] = (number & 0x00FF);
+	txData[4] = 0;
   if (data == 0)
-    txData[4] = 0;
+    txData[5] = 0;
   else
-    txData[4] = 0xFF;
-  txData[5] = 0;
+    txData[5] = 0xFF;
   static uint16_t crc;
   crc = mmodbus_crc16(txData, 6);
   txData[6] = (crc & 0x00FF);

+ 2 - 2
app/mqtt/MQTTClient.c

@@ -54,8 +54,8 @@ int mqtt_connectToMqttServer(int sock)
 	
 	//配置连接参数
 	data.clientID.cstring = CLIENT_ID;
-	data.username.cstring = (char*)get->username;
-	data.password.cstring = (char*)get->passwd;
+	data.username.cstring = (char*)get->username;//;NULL
+	data.password.cstring = (char*)get->passwd;//;NULL
 	data.keepAliveInterval = KEEPLIVE_TIME;
 	data.MQTTVersion = MQTT_VERSION;     
 	data.cleansession = 1;