zhao006 1 year ago
parent
commit
7208afaabc

+ 3 - 0
app/HARDWARE/includes/usart.h

@@ -88,6 +88,9 @@ void DEBUG_USART_Config(void);
 void USART_232_config(void);
 void Usart_SendHex( USART_TypeDef * pUSARTx, uint8_t *str,uint16_t hexLength);
 void USART_485_config(void);
+void USART_485_DE_TX(void);
+void USART_485_DE_RX(void);
+void Config_485_Port(uint32_t baudrate, uint8_t databits, uint8_t stopbits, uint8_t parity, uint8_t flowcontrol);
 
 extern uint8_t UART6_RX_BUF[BUFF_SIZE];
 extern uint8_t UART6_RX_STAT;

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

@@ -12,10 +12,10 @@
 int fputc(int ch, FILE *f)
 {
 		/* 发送一个字节数据到串口 */
-		USART_SendData(USART_232, (uint8_t) ch);
+		USART_SendData(USART_485, (uint8_t) ch);
 		
 		/* 等待发送完毕 */
-		while (USART_GetFlagStatus(USART_232, USART_FLAG_TXE) == RESET);	
+		while (USART_GetFlagStatus(USART_485, USART_FLAG_TXE) == RESET);	
 		return (ch);
 }
 
@@ -23,9 +23,9 @@ int fputc(int ch, FILE *f)
 int fgetc(FILE *f)
 {
 		/* 等待串口输入数据 */
-		while (USART_GetFlagStatus(USART_232, USART_FLAG_RXNE) == RESET);
+		while (USART_GetFlagStatus(USART_485, USART_FLAG_RXNE) == RESET);
 
-		return (int)USART_ReceiveData(USART_232);
+		return (int)USART_ReceiveData(USART_485);
 }
 
 void LogPrint(logLevel_t logLevel, const char *func, const int line, char * fmt, ...)

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

@@ -1,4 +1,5 @@
 #include "usart.h"
+#include "mmodbus.h"
 
 uint8_t UART6_RX_BUF[BUFF_SIZE];
 uint8_t UART6_RX_STAT=0;
@@ -281,6 +282,7 @@ void USART_485_config()
 	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
 	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
 	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
+
 	NVIC_Init(&NVIC_InitStruct);
 }
 /*
@@ -448,6 +450,8 @@ void Usart_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch)
 	USART_SendData(pUSARTx,temp_l);	
 	while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);	
 }
-
+void USART3_IRQHandler(){
+			mmodbus_callback();
+}
 
 /*********************************************END OF FILE**********************/

+ 23 - 36
app/MDKProject/lora_gateway.uvoptx

@@ -117,6 +117,11 @@
         <pMon>Segger\JL2CM3.dll</pMon>
       </DebugOpt>
       <TargetDriverDllRegistry>
+        <SetRegEntry>
+          <Number>0</Number>
+          <Key>DLGUARM</Key>
+          <Name></Name>
+        </SetRegEntry>
         <SetRegEntry>
           <Number>0</Number>
           <Key>JL2CM3</Key>
@@ -137,11 +142,6 @@
           <Key>ARMDBGFLAGS</Key>
           <Name></Name>
         </SetRegEntry>
-        <SetRegEntry>
-          <Number>0</Number>
-          <Key>DLGUARM</Key>
-          <Name></Name>
-        </SetRegEntry>
         <SetRegEntry>
           <Number>0</Number>
           <Key>CMSIS_AGDI</Key>
@@ -153,24 +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>24</LineNumber>
-          <EnabledFlag>1</EnabledFlag>
-          <Address>0</Address>
-          <ByteObject>0</ByteObject>
-          <HtxType>0</HtxType>
-          <ManyObjects>0</ManyObjects>
-          <SizeOfObject>0</SizeOfObject>
-          <BreakByAccess>0</BreakByAccess>
-          <BreakIfRCount>0</BreakIfRCount>
-          <Filename>..\System\source\sys_http.c</Filename>
-          <ExecCommand></ExecCommand>
-          <Expression></Expression>
-        </Bp>
-      </Breakpoint>
+      <Breakpoint/>
       <WatchWindow1>
         <Ww>
           <count>0</count>
@@ -185,14 +168,24 @@
         <Ww>
           <count>2</count>
           <WinNumber>1</WinNumber>
-          <ItemText>"0:"</ItemText>
+          <ItemText>"0:",0x0A</ItemText>
+        </Ww>
+        <Ww>
+          <count>3</count>
+          <WinNumber>1</WinNumber>
+          <ItemText>mqtt_connectFlag</ItemText>
+        </Ww>
+        <Ww>
+          <count>4</count>
+          <WinNumber>1</WinNumber>
+          <ItemText>mqtt_recvbuffer</ItemText>
         </Ww>
       </WatchWindow1>
       <MemoryWindow1>
         <Mm>
           <WinNumber>1</WinNumber>
           <SubType>8</SubType>
-          <ItemText>http_data</ItemText>
+          <ItemText>payload</ItemText>
           <AccSizeX>0</AccSizeX>
         </Mm>
       </MemoryWindow1>
@@ -202,7 +195,7 @@
       <DebugFlag>
         <trace>0</trace>
         <periodic>1</periodic>
-        <aLwin>1</aLwin>
+        <aLwin>0</aLwin>
         <aCover>0</aCover>
         <aSer1>0</aSer1>
         <aSer2>0</aSer2>
@@ -220,7 +213,7 @@
         <aPa1>0</aPa1>
         <AscS4>0</AscS4>
         <aSer4>0</aSer4>
-        <StkLoc>0</StkLoc>
+        <StkLoc>1</StkLoc>
         <TrcWin>0</TrcWin>
         <newCpu>0</newCpu>
         <uProt>0</uProt>
@@ -238,12 +231,6 @@
       <pszMrulep></pszMrulep>
       <pSingCmdsp></pSingCmdsp>
       <pMultCmdsp></pMultCmdsp>
-      <SystemViewers>
-        <Entry>
-          <Name>System Viewer\FSMC</Name>
-          <WinId>35905</WinId>
-        </Entry>
-      </SystemViewers>
       <DebugDescription>
         <Enable>1</Enable>
         <EnableFlashSeq>0</EnableFlashSeq>
@@ -256,7 +243,7 @@
 
   <Group>
     <GroupName>USER</GroupName>
-    <tvExp>0</tvExp>
+    <tvExp>1</tvExp>
     <tvExpOptDlg>0</tvExpOptDlg>
     <cbSel>0</cbSel>
     <RteFlg>0</RteFlg>
@@ -1012,7 +999,7 @@
 
   <Group>
     <GroupName>Sx1278</GroupName>
-    <tvExp>1</tvExp>
+    <tvExp>0</tvExp>
     <tvExpOptDlg>0</tvExpOptDlg>
     <cbSel>0</cbSel>
     <RteFlg>0</RteFlg>
@@ -2092,7 +2079,7 @@
 
   <Group>
     <GroupName>MODBUS</GroupName>
-    <tvExp>0</tvExp>
+    <tvExp>1</tvExp>
     <tvExpOptDlg>0</tvExpOptDlg>
     <cbSel>0</cbSel>
     <RteFlg>0</RteFlg>

+ 6 - 4
app/MDKProject/lora_gateway.uvprojx

@@ -10,14 +10,14 @@
       <TargetName>Target 1</TargetName>
       <ToolsetNumber>0x4</ToolsetNumber>
       <ToolsetName>ARM-ADS</ToolsetName>
-      <pCCUsed>5060750::V5.06 update 6 (build 750)::ARMCC</pCCUsed>
+      <pCCUsed>5060960::V5.06 update 7 (build 960)::.\ARMCC</pCCUsed>
       <uAC6>0</uAC6>
       <TargetOption>
         <TargetCommonOption>
           <Device>STM32F207ZGTx</Device>
           <Vendor>STMicroelectronics</Vendor>
-          <PackID>Keil.STM32F2xx_DFP.2.9.0</PackID>
-          <PackURL>http://www.keil.com/pack</PackURL>
+          <PackID>Keil.STM32F2xx_DFP.2.10.0</PackID>
+          <PackURL>http://www.keil.com/pack/</PackURL>
           <Cpu>IRAM(0x20000000,0x00020000) IROM(0x08000000,0x00100000) CPUTYPE("Cortex-M3") CLOCK(12000000) ELITTLE</Cpu>
           <FlashUtilSpec></FlashUtilSpec>
           <StartupFile></StartupFile>
@@ -185,6 +185,8 @@
             <uocXRam>0</uocXRam>
             <RvdsVP>0</RvdsVP>
             <RvdsMve>0</RvdsMve>
+            <RvdsCdeCp>0</RvdsCdeCp>
+            <nBranchProt>0</nBranchProt>
             <hadIRAM2>0</hadIRAM2>
             <hadIROM2>0</hadIROM2>
             <StupSel>8</StupSel>
@@ -351,7 +353,7 @@
             <NoWarn>0</NoWarn>
             <uSurpInc>0</uSurpInc>
             <useXO>0</useXO>
-            <uClangAs>0</uClangAs>
+            <ClangAsOpt>4</ClangAsOpt>
             <VariousControls>
               <MiscControls></MiscControls>
               <Define></Define>

BIN
app/OBJ/lora_gateway.axf


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


+ 46 - 40
app/System/includes/gateway_message.h

@@ -6,21 +6,22 @@
 
 
 
-// 节点所存储的modbus写命令
-typedef struct _NODE_WRITE_MODBUS_COMMAND
+// 网关所存储的modbus写命令
+typedef struct _GATEWAY_WRITE_MODBUS_COMMAND
 {
+	uint16_t registerByteNum;
 	uint8_t slaveAddress;
 	uint8_t functionCode;
 	uint8_t dataType;          //数据类型
-	uint8_t slaveAdress;
 	uint8_t keyword[20]; // 所写属性的名称
 	uint8_t bigLittleFormat;
 	uint16_t registerAddress;	// 寄存器地址
 	uint8_t write_command_flag; // 是否成功写入的标志
-	struct _NODE_WRITE_MODBUS_COMMAND *nextParams;
-} NODE_WRITE_MODBUS_COMMAND;
-// 节点所存储的modbus读命令
-typedef struct _NODE_READ_MODBUS_COMMAND
+	struct _GATEWAY_WRITE_MODBUS_COMMAND *nextParams;
+} GATEWAY_WRITE_MODBUS_COMMAND;
+
+// 网关所存储的modbus读命令
+typedef struct _GATEWAY_READ_MODBUS_COMMAND
 {
 	uint8_t slaveAddress;
 	uint8_t functionCode;
@@ -30,83 +31,88 @@ typedef struct _NODE_READ_MODBUS_COMMAND
 	uint8_t value[4];          //读取到的数据值
 	uint16_t registerByteNum;
 	uint8_t decimalPoint;
-	struct _NODE_READ_MODBUS_COMMAND *nextParams;
-} NODE_READ_MODBUS_COMMAND;
+	struct _GATEWAY_READ_MODBUS_COMMAND *nextParams;
+} GATEWAY_READ_MODBUS_COMMAND;
+
+
 // 节点所存储都DLT645读指令
-typedef struct _NODE_READ_DLT645_COMMAND
+typedef struct _GATEWAY_READ_DLT645_COMMAND
 {
 	uint8_t deviceID645[6];
 	uint8_t keyword[20];   // 所读属性的名称
 	uint32_t Identification; // 645数据标识
 	uint8_t data[9];	   // 读到的数据
 	uint8_t rxLen;		   // 从机应答值的长度
-	struct _NODE_READ_DLT645_COMMAND *nextParams;
-} NODE_READ_DLT645_COMMAND;
+	struct _GATEWAY_READ_DLT645_COMMAND *nextParams;
+} GATEWAY_READ_DLT645_COMMAND;
 
 typedef struct _PARAMS_PROTOCOL_COMMAND
 {
-	NODE_READ_MODBUS_COMMAND   *node_read_modbus_command;
-	NODE_WRITE_MODBUS_COMMAND  *node_write_modbus_command;
-	NODE_READ_DLT645_COMMAND   *node_read_dlt645_command;
+	GATEWAY_READ_MODBUS_COMMAND   *gateway_read_modbus_command;
+	GATEWAY_WRITE_MODBUS_COMMAND  *gateway_write_modbus_command;
+	GATEWAY_READ_DLT645_COMMAND   *gateway_read_dlt645_command;
 }PARAMS_PROTOCOL_COMMAND;
 
+
 // 节点包含的设备信息
 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;
 	
-	
 	PARAMS_PROTOCOL_COMMAND	*params;
 	struct _DEVICE_PARAMS *nextDevice; // 指向下一个设备地址
 } DEVICE_PARAMS;
 
-typedef struct _NODE_PARAMS
-{                    
-	uint8_t node_address[3];
-	// 外设的串口工作属性仅只有一个
-	uint32_t baudrate;	 // 波特率
-	uint8_t dataBits;	 // 数据位
-	uint8_t checkBit;	 // 校验位
-	uint8_t stopBit;	 // 停止位
-	uint8_t flowControl; // 流控制
-	// lora通信的属性设置
-	uint8_t RFFrequency;        //载波频率
-	uint8_t SignalBw;           //信号带宽
-	uint8_t SpreadingFactor;    //扩频因子
-	uint8_t ErrorCoding;        //编码率
-	// 包含的设备结构体
-	uint8_t deviceCount;
-	DEVICE_PARAMS *device_params;
-	struct _NODE_PARAMS *nextNode; // 下一个设备所存地址如果没有下一个设备指向NULL
-} NODE_PARAMS;
 
+// 网关包含的设备信息
 typedef struct _GATEWAY_PARAMS
 {
 	uint8_t data_valid_flag;  // 数据有效标志  0xF1:有效  其它:无效
-	uint8_t host[20];		  // MQTT服务器地址
+	char host[20];		  // MQTT服务器地址
 	uint16_t port;			  // MQTT服务器端口号
 	uint8_t messageTopic[50]; // MQTT消息主题
 	uint8_t commandTopic[50]; // MQTT指令主题
+	char username[20];			//MQTT用户名
+	char passwd[20];				// MQTT用户密码
 	uint8_t deviceId[25];	  // 设备ID
 	uint8_t dataSource;		  // 协议类型                 0 保留 1 表示645协议 2表示modbus协议
-	uint8_t dataType645;	  // 645协议数据类型          0保留  1 表示07版本  2表示97版本
+	uint8_t dataType645;	  // 645协议数据类型          0 保留 1 表示07版本  2表示97版本
 	uint32_t pollTime;		  // 轮询时间
 	uint32_t inboundTime;
 	uint8_t state;			 // 检测是否上位机配置过当上位机配置过该状态位,则以后均不走http获取相应配置 置位值为0xF1
 	uint8_t gatewayId[10];	 // 固化信息
 	uint8_t gatewayMode[10]; // 工作模式、后续版本迭代使用
+	
 	// 网关相关信息
 	uint8_t gateName[10];
 
-	// 附属设备相关信息
-	NODE_PARAMS *node_params;
+	// 包含的设备结构体
+	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;				//校验位
+	
 } 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();
 #endif

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

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

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

@@ -5,12 +5,6 @@
 void http_getDemo(void);
 
 void http_postDemo(void);
-extern uint8_t load_http_config;  //httpÓÐÎÞ»ñÈ¡ÐÂÊý¾ÝµÄÒÀ¾Ý
-typedef Http_config
-{
-	
-}
-
 #endif 
 
 

+ 9 - 6
app/System/includes/sys_mqtt.h

@@ -3,6 +3,8 @@
 
 #include "ucos_ii.h"
 #include "json_queue.h"
+#include "gateway_message.h"
+
 #define  MBOX_NETWORK_ERROR     0xf0000000
 
 #define  MBOX_MQTT_QOS1PUBACK   0x10000001
@@ -18,19 +20,19 @@
 
 //#define  MQTT_SERVER_ADDR  "192.168.0.116"
 //#define  MQTT_SERVER_ADDR  "192.168.0.183"
-#define  MQTT_SERVER_ADDR  "36.134.23.11"
-#define  MQTT_SERVER_PORT  1883
+//#define  MQTT_SERVER_ADDR  "36.134.23.11"
+//#define  MQTT_SERVER_PORT  1883
 
 #define  CLIENT_ID     "stm32f207test"       //
 //#define  USER_NAME     "emqxxx"     
 //#define  PASSWORD      "123456" 
-#define  USER_NAME     NULL  
-#define  PASSWORD      NULL
+//#define  USER_NAME     NULL  
+//#define  PASSWORD      NULL
 #define  KEEPLIVE_TIME   120
 #define  MQTT_VERSION    4
 
-#define  TOPICSTM32    "test/stm32"
-#define  TOPICPC       "test/pc"
+//#define  TOPICSTM32    "test/stm32"
+//#define  TOPICPC       "test/pc"
 //#define  TOPICTEST     "/device/cxw0817111032"
 
 
@@ -39,6 +41,7 @@ extern int mqtt_connectFlag;
 extern OS_EVENT *mqtt_sendMseeageMbox;
 extern OS_EVENT *JsonQ;
 void mqtt_threadCreate(void);
+char *get_mqtt_payload();
 
 #define jsonMaxSize 1000
 extern char pubJsonString[jsonMaxSize];

+ 10 - 0
app/System/includes/task.h

@@ -3,12 +3,22 @@
 #include "includes.h"
 #include "stm32f2xx.h"
 #include "radio.h"
+#include "gateway_message.h"
 //Ö÷ÒªµÄÊý¾Ý´¦ÀíÏß³Ì
 #define DATA_TASK_PRIO 8
 #define DATA_STK_SIZE  2*1024
 
 extern void data_task(void *pdata);
 
+typedef struct _value{
+		char PowerValue;
+		char TempValue;
+		char ModeValue;
+		char FanValue;
+		char RoomTempValue;
+	}value;
+
+void WRITE_MODBUS_DATA(char* buf);
 extern tRadioDriver *Radio;
 
 #endif

+ 55 - 96
app/System/source/gateway_message.c

@@ -3,8 +3,10 @@
 #include <stdlib.h>
 #include "protocol.h"
 #include "malloc.h"
+#include <stdio.h>   
 #include "gateway_message.h"
 
+
 GATEWAY_PARAMS gateway_config_params = {0};
 
 GATEWAY_PARAMS *get_gateway_config_params()
@@ -14,120 +16,74 @@ GATEWAY_PARAMS *get_gateway_config_params()
 
 void parseStringField(const char *data, const char *field, char *value);
 int parseIntField(const char *data, const char *field);
-void addNode(char *nodestring);
-void addDevice(char *string, NODE_PARAMS *node);
+//void addNode(char *nodestring);
+void addDevice(char *string);
 int extract_substring(const char *input_string, const char *start_token, const char *end_token, char *result);
 void addSensorListParams(char *paramString, DEVICE_PARAMS *device);
 void addCommandListParams(char *paramString, DEVICE_PARAMS *device);
 /**
  * @brief  解析输入字符串网关结构体信息,将数据保存
- * @param  char *nodestring 输入字符串的数据
+ * @param  char *gatewayString 输入字符串的数据
  * @retval 无
  */
-void addGatewayParams(char *gatewayString)
+	void addGatewayParams(char *gatewayString)
 {
-    parseStringField(gatewayString, "\"commandTopic\":\"", (char *)&gateway_config_params.commandTopic);
-    parseStringField(gatewayString, "\"messageTopic\":\"", (char *)&gateway_config_params.messageTopic);
-    parseStringField(gatewayString, "\"deviceId\":\"", (char *)&gateway_config_params.deviceId);
-    parseStringField(gatewayString, "\"host\":\"", (char *)&gateway_config_params.host);
-    gateway_config_params.port = parseIntField(gatewayString, "\"port\":");
-    gateway_config_params.node_params = NULL;
-    gateway_config_params.pollTime = parseIntField(gatewayString, "\"pollTime\":");
-	  char *nodeString = strstr(gatewayString,"nodeList");  //移位置到节点数据开始处    
+		//gateway_config_params.device_params = NULL;
+		gateway_config_params.port = parseIntField(gatewayString, "\"port\":");
+		gateway_config_params.stopBit = parseIntField(gatewayString, "\"stopBit\":");
+		gateway_config_params.dataBits = parseIntField(gatewayString, "\"dataBit\":");
+		gateway_config_params.baudrate = parseIntField(gatewayString, "\"baudrate\":");
+		gateway_config_params.checkBit = parseIntField(gatewayString, "\"checkBit\":");
+		gateway_config_params.inboundTime= parseIntField(gatewayString, "\"inboundTime\":");
+		
+		parseStringField(gatewayString, "\"host\":\"", (char *)&gateway_config_params.host);
+		parseStringField(gatewayString, "\"deviceId\":\"", (char *)&gateway_config_params.deviceId);
+		parseStringField(gatewayString, "\"commandTopic\":\"", (char *)&gateway_config_params.commandTopic);
+		parseStringField(gatewayString, "\"messageTopic\":\"", (char *)&gateway_config_params.messageTopic);
+		parseStringField(gatewayString, "\"username\":\"", (char *)&gateway_config_params.username);
+		parseStringField(gatewayString, "\"password\":\"", (char *)&gateway_config_params.passwd);
+	
+		char *deviceString = strstr(gatewayString,"deviceList");  //移位置到节点数据开始处
 		
-    while (1)
-    {
-      addNode(nodeString);
-			nodeString=nodeString;
-			nodeString=strstr(nodeString,"}");
-			nodeString[0]='A';
-			nodeString++;
-			if(nodeString[0]==']')//数组结束标志、
-			{
-				break;
-			}
-    }
-
-}
-
-/**
- * @brief  解析输入的字符串节点信息,并将此节点信息挂载在网关结构体的节点下面
- * @param  char *nodestring
- * @retval 无
-        注:node内包含的设备名称device_1在别的节点下也可能包含,所以在解析节点时要先将数据截取出来让匹配不会越界到别的节点
- */
-
-void addNode(char *nodestring)
-{
-    // 新增一个节点的设备并解析最外层的节点属性
-    char *deviceString = (char *)nodestring; // 用于移位解析device设备信息
-    NODE_PARAMS *newNode = malloc(sizeof(NODE_PARAMS));
-    newNode->device_params = NULL; // 先将其设备挂载为NULL
-    newNode->nextNode = NULL;      // 下一个设备的挂载为NULL
-	  char nodeIdString[5];
-    parseStringField(nodestring, "\"nodeId\":\"", (char *)&nodeIdString);
-    for (int j = 0; j < 2; j++)
-    {
-      uint8_t byte;
-      sscanf((const char *)&nodeIdString[j * 2], "%2hhx", &byte);
-      newNode->node_address[j]=byte;
-    }
-    newNode->RFFrequency = parseIntField(nodestring, "\"RFFrequency\":");
-    newNode->SignalBw = parseIntField(nodestring, "\"SignalBw\":");
-    newNode->SpreadingFactor = parseIntField(nodestring, "\"SpreadingFactor\":");
-    newNode->ErrorCoding = parseIntField(nodestring, "\"ErrorCoding\":");
 		
-		deviceString=strstr(nodestring,"deviceList");//移位置到设备数组起始处
-    while (1)
+		while (1)
     {
         if (deviceString)
         {
-           addDevice(deviceString, newNode); // 往此节点下挂载设备信息
+           addDevice(deviceString); // 往此地址下挂载设备信息
            deviceString=deviceString;//重新获取设备字符串的位置
 					 deviceString = strstr(deviceString, "}"); 
 					 deviceString[0]='A';
 					 deviceString++;
 					 if(deviceString[0]==']')
 					 {
-						 break;
+					 break;
 					 }
         }
         else
         {
             return;
         }
-        /* code */
-    }
- 
-
-    // 添加节点到网关链表中
-    if (gateway_config_params.node_params == NULL)
-    {
-        gateway_config_params.node_params = newNode;
-    }
-    else
-    {
-        NODE_PARAMS *current = gateway_config_params.node_params;
-        while (current->nextNode != NULL)
-        {
-            current = current->nextNode;
-        }
-        current->nextNode = newNode;
     }
 }
+
 /**
  * @brief  解析输入字段的字符串,解析出属于该节点下的设备信息
  * @param  uint8_t *string输入的字符串数据,NODE_PARAMS *node节点信息
  * @retval 无
  */
-void addDevice(char *deviceString, NODE_PARAMS *node)
+
+void addDevice(char *deviceString)
 {
     char *paramString = (char *)deviceString; // 属性指针
-    // 创建新设备页
-    DEVICE_PARAMS *newDevicePage = (DEVICE_PARAMS *)malloc(sizeof(DEVICE_PARAMS));
+		DEVICE_PARAMS *newDevicePage = (DEVICE_PARAMS *)malloc(sizeof(DEVICE_PARAMS));    // 创建新设备页
     newDevicePage->nextDevice = NULL;
-    parseStringField(deviceString, "\"deviceId\":\"", (char *)&newDevicePage->deviceID);
     newDevicePage->protocol = parseIntField(deviceString, "\"protocol\":");
+		newDevicePage->MDBdataType = parseIntField(deviceString, "\"dataType\":");
+		newDevicePage->MDBbigLittleFormat = parseIntField(deviceString, "\"bigLittleFormat\":");
+		
+		parseStringField(deviceString, "\"deviceId\":\"", (char *)&newDevicePage->deviceID);
+	
     newDevicePage->params = (PARAMS_PROTOCOL_COMMAND *)malloc(sizeof(PARAMS_PROTOCOL_COMMAND));
 
     paramString = strstr(paramString, "\"sensorData\":["); // 找到该节点的轮询上发属性
@@ -141,7 +97,7 @@ void addDevice(char *deviceString, NODE_PARAMS *node)
         if (paramString[0] == ']')
         {
             paramString = (char *)deviceString; // 复原指针位置
-            break;                              // 找到了结束符,跳出循环
+						break;                              // 找到了结束符,跳出循环
         }
     }
 		if(newDevicePage->protocol==MODBUS)  //如果为modbus的话解析写指令
@@ -160,8 +116,8 @@ void addDevice(char *deviceString, NODE_PARAMS *node)
 					paramString++;
 					if (paramString[0] == ']')
 					{
-						paramString = (char *)deviceString;
-						break;
+						paramString = (char *)deviceString;// 复原指针位置
+						break;														 // 找到了结束符,跳出循环
 					}
 				}
 			}
@@ -169,13 +125,13 @@ void addDevice(char *deviceString, NODE_PARAMS *node)
     // 解析下发的mqtt存储信息下发控制指令
 		
     // 添加设备页到链表末尾
-    if (node->device_params == NULL)
+    if (gateway_config_params.device_params == NULL)
     {
-        node->device_params = newDevicePage;
+        gateway_config_params.device_params = newDevicePage;
     }
     else
     {
-        DEVICE_PARAMS *current = node->device_params;
+        DEVICE_PARAMS *current = gateway_config_params.device_params;
         while (current->nextDevice != NULL)
         {
             current = current->nextDevice;
@@ -197,7 +153,7 @@ void addSensorListParams(char *paramString, DEVICE_PARAMS *device)
     case DLT645_97:
     case DLT645_07:
 		{
-        NODE_READ_DLT645_COMMAND *read_dlt645_command = malloc(sizeof(NODE_READ_DLT645_COMMAND));
+        GATEWAY_READ_DLT645_COMMAND *read_dlt645_command = malloc(sizeof(GATEWAY_READ_DLT645_COMMAND));
         read_dlt645_command->Identification = parseIntField(paramString, "\"identifier645\":");
         parseStringField(paramString, "\"identifier\":\"", (char *)&read_dlt645_command->keyword);
         char *string = mymalloc(SRAMIN, 13);
@@ -209,13 +165,13 @@ void addSensorListParams(char *paramString, DEVICE_PARAMS *device)
           read_dlt645_command->deviceID645[j]=byte;
         }
         myfree(SRAMIN, string);
-        if (device->params->node_read_dlt645_command == NULL)
+        if (device->params->gateway_read_dlt645_command == NULL)
         {
-            device->params->node_read_dlt645_command = read_dlt645_command;
+            device->params->gateway_read_dlt645_command = read_dlt645_command;
         }
         else
         {
-            NODE_READ_DLT645_COMMAND *current = device->params->node_read_dlt645_command;
+            GATEWAY_READ_DLT645_COMMAND *current = device->params->gateway_read_dlt645_command;
             while (current->nextParams != NULL)
             {
                 current = current->nextParams;
@@ -223,23 +179,25 @@ void addSensorListParams(char *paramString, DEVICE_PARAMS *device)
             current->nextParams = read_dlt645_command;
         }
 			}
+				
         break;
     case MODBUS:
 		{
-        NODE_READ_MODBUS_COMMAND *read_modbus_command = malloc(sizeof(NODE_READ_MODBUS_COMMAND));
+        GATEWAY_READ_MODBUS_COMMAND *read_modbus_command = malloc(sizeof(GATEWAY_READ_MODBUS_COMMAND));
 				parseStringField(paramString,"\"identifier\":\"",(char *)&read_modbus_command->keyword);
         read_modbus_command->decimalPoint = parseIntField(paramString, "\"precise\":");
         read_modbus_command->functionCode = parseIntField(paramString, "\"rFunctionCode\":");
         read_modbus_command->slaveAddress = parseIntField(paramString, "\"slaveAddress\":");
         read_modbus_command->registerAddress = parseIntField(paramString, "\"registerAddress\":");
         read_modbus_command->registerByteNum = parseIntField(paramString, "\"registerByteNum\":");
-        if (device->params->node_read_modbus_command == NULL)
+        
+			if (device->params->gateway_read_modbus_command== NULL)
         {
-            device->params->node_read_modbus_command = read_modbus_command;
+            device->params->gateway_read_modbus_command = read_modbus_command;
         }
         else
         {
-            NODE_READ_MODBUS_COMMAND *current = device->params->node_read_modbus_command;
+            GATEWAY_READ_MODBUS_COMMAND *current = device->params->gateway_read_modbus_command;
             while (current->nextParams != NULL)
             {
                 current = current->nextParams;
@@ -260,19 +218,20 @@ void addSensorListParams(char *paramString, DEVICE_PARAMS *device)
 void addCommandListParams(char *paramString, DEVICE_PARAMS *device)
 {
 	
- 	NODE_WRITE_MODBUS_COMMAND *write_modbus_command=malloc(sizeof(NODE_WRITE_MODBUS_COMMAND));
+ 	GATEWAY_WRITE_MODBUS_COMMAND *write_modbus_command=malloc(sizeof(GATEWAY_WRITE_MODBUS_COMMAND));
 	parseStringField(paramString,"\"identifier\":\"",(char *)&write_modbus_command->keyword);
 
 	write_modbus_command->functionCode=parseIntField(paramString, "\"wFunctionCode\":");
 	write_modbus_command->slaveAddress=parseIntField(paramString, "\"slaveAddress\":");
 	write_modbus_command->registerAddress = parseIntField(paramString, "\"registerAddress\":");
-	if(device->params->node_write_modbus_command == NULL)
+	write_modbus_command->registerByteNum = parseIntField(paramString, "\"registerByteNum\":");
+	if(device->params->gateway_write_modbus_command == NULL)
 	{
-		device->params->node_write_modbus_command=write_modbus_command;
+		device->params->gateway_write_modbus_command=write_modbus_command;
 	}
 	else
 	{
-		NODE_WRITE_MODBUS_COMMAND *current=device->params->node_write_modbus_command;
+		GATEWAY_WRITE_MODBUS_COMMAND *current=device->params->gateway_write_modbus_command;
     while (current->nextParams != NULL)
     {
       current = current->nextParams;

+ 11 - 1
app/System/source/node_data_acquisition.c

@@ -1,5 +1,6 @@
 #include "node_data_acquisition.h"
 #include "node_message.h"
+#include "malloc.h"
 
 #include <math.h>
 uint8_t protocol_485;                      //485回调函数会调用那个callback dlt645还是modbus dlt645为2 modbus为1
@@ -97,4 +98,13 @@ void data_acquisition(void)
 		}
 		i++;
 	}
-}
+}
+void rs_485_test()
+{
+	uint16_t *data=mymalloc(SRAMEX,50);
+	bool success = mmodbus_readHoldingRegisters16i(0x01,0x00,0x0C,data);
+	if(success)
+	{
+		int i=0;
+	}
+}

+ 18 - 39
app/System/source/protocol.c

@@ -66,19 +66,9 @@ int masterSendNodeString(uint8_t nodeIndex, uint8_t *string, uint16_t *size)
 	GATEWAY_PARAMS *gateway;
 	gateway = get_gateway_config_params();
 
-	// 将当前的节点指针指到需要读取的节点位置
-	NODE_PARAMS *currentNode = gateway->node_params;
-	while (nodeIndex)
-	{
-		currentNode = currentNode->nextNode;
-		nodeIndex--;
-	}
-	memset(string, 0, 256); // 清除上一次数据
-	// 将首字符用nodeAdress
-	string[0] = currentNode->node_address[0];
-	string[1] = currentNode->node_address[1];
+
 	// 找到当前的设备结束位置
-	DEVICE_PARAMS *currentDevice = currentNode->device_params;
+	DEVICE_PARAMS *currentDevice = gateway->device_params;
 	while (nowDeviceNum)
 	{
 		currentDevice = currentDevice->nextDevice;
@@ -86,8 +76,8 @@ int masterSendNodeString(uint8_t nodeIndex, uint8_t *string, uint16_t *size)
 	}
 
 	// 移动位置到结束位置属性的下一位
-	NODE_READ_MODBUS_COMMAND *currentModbusParams = currentDevice->params->node_read_modbus_command;
-	NODE_READ_DLT645_COMMAND *currentDlt645Params = currentDevice->params->node_read_dlt645_command;
+	GATEWAY_READ_MODBUS_COMMAND *currentModbusParams = currentDevice->params->gateway_read_modbus_command;
+	GATEWAY_READ_DLT645_COMMAND *currentDlt645Params = currentDevice->params->gateway_read_dlt645_command;
 	switch (currentDevice->protocol)
 	{
 	case MODBUS:
@@ -148,12 +138,13 @@ int masterSendNodeString(uint8_t nodeIndex, uint8_t *string, uint16_t *size)
 					*size = len;
 					return 1;
 				}
-				currentModbusParams = currentDevice->params->node_read_modbus_command;
+				currentModbusParams = currentDevice->params->gateway_read_modbus_command;
 				nowDeviceNum++;
 				nowparamsNum = 0;
 			}
 			break;
 		}
+
 		case DLT645_07:
 		{
 			uint8_t protocolHexData = protocol_dlt645_07;
@@ -176,7 +167,7 @@ int masterSendNodeString(uint8_t nodeIndex, uint8_t *string, uint16_t *size)
 					*size = len;
 					return 1;
 				}
-				currentDlt645Params = currentDevice->params->node_read_dlt645_command;
+				currentDlt645Params = currentDevice->params->gateway_read_dlt645_command;
 				nowDeviceNum++;
 				nowparamsNum = 0;
 			}
@@ -189,14 +180,16 @@ int masterSendNodeString(uint8_t nodeIndex, uint8_t *string, uint16_t *size)
 			len++;
 			insertHexData(string, (uint8_t *)&currentDlt645Params->deviceID645, len, 6); // 插入dlt645_97地址域
 			len += 6;
-			insertHexData(string, (uint8_t *)&currentDlt645Params->Identification, len, 2); // 插入dlt645_97数据标识
-			len += 2;
+			insertHexData(string, (uint8_t *)&currentDlt645Params->Identification, len, 4); // 插入dlt645_97数据标识
+			len += 4;
+
 			// 属性读取完毕,移位到下一个属性
 			i++;
 			currentDlt645Params = currentDlt645Params->nextParams;
 			if (currentDlt645Params == NULL)
 			{
 				currentDevice = currentDevice->nextDevice;
+				currentDlt645Params = currentDevice->params->gateway_read_dlt645_command;
 				if (currentDlt645Params == NULL)
 				{
 					deviceNum = 0; // 将结束位置全部归0
@@ -204,12 +197,13 @@ int masterSendNodeString(uint8_t nodeIndex, uint8_t *string, uint16_t *size)
 					*size = len;
 					return 1;
 				}
-				currentDlt645Params = currentDevice->params->node_read_dlt645_command; // 还有属性则更新位置
+				currentDlt645Params = currentDevice->params->gateway_read_dlt645_command; // 还有属性则更新位置
 				nowDeviceNum++;
 				nowparamsNum = 0;
 			}
 			break;
 		}
+		
 		default:
 			goto end_while;
 		}
@@ -408,31 +402,16 @@ int GatewayProtocolAnalysis(uint8_t *string, uint16_t len)
 	int nowparamsNum = startParamsNum;
 	GATEWAY_PARAMS *gateway;
 	gateway = get_gateway_config_params();
-	uint16_t nodeId = string[0] << 8 | string[1];
-	// 将当前的节点指针指到需要读取的节点位置
-	NODE_PARAMS *currentNode = gateway->node_params;
-	uint16_t currentNodeId = currentNode->node_address[0] << 8 | currentNode->node_address[1];
-	while (currentNodeId != nodeId)
-	{
-		currentNode = currentNode->nextNode;
-		if (currentNode == NULL)
-		{
-			// 没有找到对应该节点信息不是给本机
-			
-			return 0;
-		}
-		currentNodeId = currentNode->node_address[0] << 8 | currentNode->node_address[1];
-	}
 	// 找到该节点进行轮询
-	DEVICE_PARAMS *currentDevice = currentNode->device_params;
+	DEVICE_PARAMS *currentDevice = gateway->device_params;
 	while (nowDeviceNum)
 	{
 		currentDevice = currentDevice->nextDevice;
 		nowDeviceNum--;
 	}
 	// 移动位置到开始属性
-	NODE_READ_MODBUS_COMMAND *currentModbusParams = currentDevice->params->node_read_modbus_command;
-	NODE_READ_DLT645_COMMAND *currentDlt645Params = currentDevice->params->node_read_dlt645_command;
+	GATEWAY_READ_MODBUS_COMMAND *currentModbusParams = currentDevice->params->gateway_read_modbus_command;
+	GATEWAY_READ_DLT645_COMMAND *currentDlt645Params = currentDevice->params->gateway_read_dlt645_command;
 	switch (currentDevice->protocol)
 	{
 	case MODBUS:
@@ -550,7 +529,7 @@ int GatewayProtocolAnalysis(uint8_t *string, uint16_t len)
 						if (currentDlt645Params == NULL)
 						{
 							currentDevice = currentDevice->nextDevice;
-							currentDlt645Params=currentDevice->params->node_read_dlt645_command;
+							currentDlt645Params=currentDevice->params->gateway_read_dlt645_command;
 						}
 					break;
 				case MODBUS:
@@ -558,7 +537,7 @@ int GatewayProtocolAnalysis(uint8_t *string, uint16_t len)
 					if(currentModbusParams==NULL)
 					{
 						currentDevice=currentDevice->nextDevice;
-						currentDlt645Params=currentDevice->params->node_read_dlt645_command;
+						currentDlt645Params=currentDevice->params->gateway_read_dlt645_command;
 					}
 					break;
 			}

+ 2 - 2
app/System/source/sys_http.c

@@ -21,8 +21,8 @@ void http_getDemo(void)
 	ret = http_clientGet("gpu.ringzle.com", http, 8082, 0, http_data, &datalen);
 	if(ret==200) //获取成功
 	{
-		DeleteDirFile("lora_json.txt");
-		write_file("lora_json.txt",http_data,strlen(http_data));
+		DeleteDirFile("device.txt");
+		write_file("device.txt",http_data,strlen(http_data));
 		load_http_config=1;
 	}
 	HTTP_PRINTF("%s", (char *)http_data);

+ 25 - 3
app/System/source/sys_mqtt.c

@@ -10,12 +10,21 @@
 #include "led.h"
 #include "MQTTClient.h"
 #include "malloc.h"
+#include "mmodbus.h"
+#include "myFile.h"
+#include "gateway_message.h"
+#include "task.h"
+
 /********************************************************
 * @brief  连接到服务器
 * @param  sock: sock编号
 *********************************************************/
 int mqtt_userConnect(void)
 {
+	GATEWAY_PARAMS* get;
+	get = get_gateway_config_params();
+	char* MQTT_SERVER_ADDR = get->host;
+	int MQTT_SERVER_PORT = get->port;
 	int sock = -1;
 	
 	//连接到tcp服务器
@@ -44,6 +53,9 @@ int mqtt_userConnect(void)
 *************************************************************/
 int mqtt_userSubscribeTopic(int sock)
 {
+	GATEWAY_PARAMS* get;
+	get = get_gateway_config_params();
+	char* TOPICPC = (char*)get->commandTopic;
 	return mqtt_subscribeTopic(sock, TOPICPC, 2);
 }
 
@@ -156,15 +168,18 @@ int mqtt_userReceiveMessage(int sock, int type, uint8_t *pbuf, int len)
 char pubJsonString[jsonMaxSize];
 int mqtt_userSendMessage(int sock, int boxMsg)
 {
+	GATEWAY_PARAMS* get;
+	get = get_gateway_config_params();
+	char* TOPIC = (char*)get->messageTopic;
 	int rc = 1;	
 	//用户发送的数据
 	if((boxMsg & 0xf0000000) == 0x20000000)
 	{
 		switch(boxMsg)
 		{
-			case MBOX_USER_PUBLISHQOS0: rc = mqtt_publishMessage_qos0(sock, TOPICSTM32, (uint8_t *)pubJsonString, strlen(pubJsonString)); break;
-			case MBOX_USER_PUBLISHQOS1: rc = mqtt_publishMessage_qos1(sock, TOPICSTM32, (uint8_t *)pubJsonString, strlen(pubJsonString)); break;
-			case MBOX_USER_PUBLISHQOS2: rc = mqtt_publishMessage_qos2(sock, TOPICSTM32, (uint8_t *)pubJsonString, strlen(pubJsonString)); break;
+			case MBOX_USER_PUBLISHQOS0: rc = mqtt_publishMessage_qos0(sock, TOPIC, (uint8_t *)pubJsonString, strlen(pubJsonString)); break;
+			case MBOX_USER_PUBLISHQOS1: rc = mqtt_publishMessage_qos1(sock, TOPIC, (uint8_t *)pubJsonString, strlen(pubJsonString)); break;
+			case MBOX_USER_PUBLISHQOS2: rc = mqtt_publishMessage_qos2(sock, TOPIC, (uint8_t *)pubJsonString, strlen(pubJsonString)); break;
 		}
 	}
 	//协议站使用的数据
@@ -191,6 +206,8 @@ static int mysock;                //连接mqtt服务器的sock编号
 * @param  arg: 未使用
 * @retval 
 *************************************************************/
+//extern uint16_t ModbusValueData_16;
+uint16_t ModbusValueData_16;
 void mqtt_userManThread(void *arg)
 {
 	int rc;
@@ -224,6 +241,7 @@ __MQTT_START:
 	//3.循环发送数据
 	while(1)
 	{	
+		
 		mboxMsg = OSMboxPend(mqtt_sendMseeageMbox, 60000, &err);
 		if(OS_ERR_NONE == err)
 		{
@@ -254,6 +272,7 @@ void mqtt_userReceiveThread(void *arg)
 	int len;	
 	int packetType;
 	int msg;
+	char* buf = malloc(512);
 	
 	MQTT_PRINTF("mqtt receivethread start \r\n");
 	
@@ -283,9 +302,12 @@ void mqtt_userReceiveThread(void *arg)
 			memset(mqtt_recvbuffer, 0, MQTT_RECVBUF_LENTH);
 			packetType = MQTTPacket_read(mqtt_recvbuffer, len, transport_getdata);
 			mqtt_userReceiveMessage(mysock, packetType, mqtt_recvbuffer, len);
+			buf = (char*)&mqtt_recvbuffer;
+			WRITE_MODBUS_DATA(buf);
 		}
 		OSTimeDly(1);
 	}
+	free(buf);
 }
 
 #define  APP_TASK_MQTTMAIN_PRIO            6

+ 194 - 139
app/System/source/task.c

@@ -11,10 +11,16 @@
 #include "node_data_acquisition.h"
 #include "sys_mqtt.h"
 #include "sys_http.h"
+#include "node_message.h"
+#include "usart.h"
+#include "mmodbus.h"
+#include "sys_mqtt.h"
+#include "gateway_message.h"
+#include "MQTTClient.h"
 
 void master_task(uint8_t *string,uint16_t stringlength);
 void slave_task();
-
+int READ_MODBUS_DATA(DEVICE_PARAMS *current_device);
 
 
 uint16_t BufferSize;
@@ -33,165 +39,214 @@ tRadioDriver *Radio=NULL;
 *	返 回 值: 无
 *********************************************************************************************************
 */
+
 void data_task(void *pdata)
 {
 	OS_CPU_SR cpu_sr;
 	pdata = pdata;
-	
-	Radio = RadioDriverInit();
-	Radio->Init();
-#ifdef MASTER
-	__load_file:
-	char *lora_config_json = mymalloc(SRAMEX, 9 * 1024);
-	read_file("lora_json.txt", lora_config_json);
-	addGatewayParams(lora_config_json);
-	myfree(SRAMEX, lora_config_json);
-	GATEWAY_PARAMS *get;
-	get= get_gateway_config_params();
-	int nodeIndex=0;
-	NODE_PARAMS *current_node=get->node_params;
-	uint8_t string[256];
-	uint16_t bufferLength;
-	OS_Q_DATA Qnum;
-	StringInfo message;
-	char *mqttRecv;
-	uint8_t err;
- 	while (current_node!=NULL)
+//	Radio = RadioDriverInit();
+//	Radio->Init();
+//	mmodbus_init(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();
+//	int deviceIndex=0;
+//	DEVICE_PARAMS *current_device=get->device_params;
+//	
+//	uint32_t baudrate = get->baudrate;
+//	uint8_t databits = get->dataBits;
+//	uint8_t stopbits = get->stopBit;
+//	uint8_t parity = get->parity;
+//	uint8_t flowcontrol = get->flowControl;
+//	Config_485_Port(baudrate, databits, stopbits, parity, flowcontrol);
+//	 
+//	uint8_t string[256];
+//	uint16_t bufferLength;
+//	OS_Q_DATA Qnum;
+//	StringInfo message;
+//	char *mqttRecv;
+//	uint8_t err;
+// 	while (current_device!=NULL)
+//	{
+//		READ_MODBUS_DATA(current_device);
+//		if(current_device->nextDevice!=NULL) 
+//		{
+//			current_device=current_device->nextDevice;	
+//		}
+//		else
+//		{		
+//			int msg = MBOX_USER_PUBLISHQOS2;	
+//			if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
+//			memset(pubJsonString,0,strlen(pubJsonString));
+//			current_device=get->device_params;			
+//		}			
+//	}
+}
+
+	#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)
 	{
-		if(load_http_config!=0)
-		{
-			goto __load_file;
-		}
-		while(!masterSendNodeString(nodeIndex,string,&bufferLength))       //轮询读出
-		{
-			master_task(string,bufferLength);
-		}
-		OSQQuery(JsonQ,&Qnum);
-		//如果队列为空
-		if(Qnum.OSNMsgs!=0)
+		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)
 		{
-			mqttRecv=malloc(250);
-			message=*(StringInfo *)OSQPend(JsonQ,0, &err);
-			while(Qnum.OSNMsgs!=0)
+			if (current_device->protocol == MODBUS_READ)
 			{
-				sprintf(mqttRecv,"%s",message.p);
-			}
-		}
-		if(current_node->nextNode!=NULL)
+				protocol_485=1;
+				uint16_t data[currentModbusParams->registerByteNum /2]; // modbus寄存器长度
+				mmodbus_set16bitOrder(current_device->MDBbigLittleFormat);
+				if (currentModbusParams->functionCode == 0x03 | currentModbusParams->functionCode == 0x01)
+				{
+					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(pubJsonString + strlen(pubJsonString), "{\"data\":[{\"deviceId\":\"%s\",\"%s\":%d}]},", 
+																									current_device->deviceID, currentModbusParams->keyword, value);
+					currentModbusParams = currentModbusParams->nextParams;
+					if (currentModbusParams == NULL)	
+						{
+							sprintf(pubJsonString + strlen(pubJsonString) - 1, ""); 
+							return 0;
+						}
+					}
+				}
+			}	
+
+			else if (current_device->protocol == MODBUS_WRITE)
 		{
-			current_node=current_node->nextNode;
-			nodeIndex++;
+			protocol_485=1;
 		}
-		else
+		else if (current_device->protocol == DLT645_07 || current_device->protocol == DLT645_97)
 		{
-			nodeIndex=0;
-			current_node=get->node_params;
-		}
-		
-	}
-#else
-	dlt645_init(100);
-	mmodbus_init(100);
-	Radio->StartRx();
-	while (1)
-	{
-		slave_task();
-		OSTimeDlyHMSM(0, 0, 0, 200);
-	}
-#endif
-}
-
-/*
-*********************************************************************************************************
-*	函 数 名: void master_task(char *string)
-*	功能说明: 主网关sx1278轮询发送调用接口,发送结束后就将状态切换到接收状态,等待从机的响应值,当从机超时或接收到数据进行后续数据处理的流程
-*	形    参:无
-*	返 回 值: 无
-*********************************************************************************************************
-*/
+			protocol_485=2;
+			uint8_t read_buf[10];
+			
+			currentDLT645Params->rxLen = 0;
+			memset(read_buf, 0, 10);
+			memset(currentDLT645Params->data, 0, 10);
 
-volatile uint32_t startTime; // 记录开始的时间且开始时间到结束时间超过一定范围则判定为超时状态,这时对其进行再次发送
-void master_task(uint8_t *string,uint16_t stringlength)
-{
-	int retry_count=0;                                    //发送计数,暂时不开启失败重发机制
-	while (1)
-	{
-		switch (Radio->Process())
-		{
-		case RF_RX_DONE:
-			Radio->GetRxPacket(Buffer, &BufferSize);
-			if(GatewayProtocolAnalysis(Buffer,BufferSize)==0)
+			dlt645_set_addr(&dlt645, current_device->params->gateway_read_dlt645_command->deviceID645);
+			int8_t rs;
+			if (current_device->protocol == DLT645_07)
 			{
-				//接收的信息不属于节点信息重新进入接收模式
-				Radio->StartRx();
+				rs = dlt645_read_data(&dlt645, currentDLT645Params->Identification, read_buf, DLT645_2007);
 			}
-			else //完成一次发射应答过程
+			else if (current_device->protocol == DLT645_97)
 			{
-				delay_ms(1000);
-				return;
+				rs = dlt645_read_data(&dlt645, currentDLT645Params->Identification, read_buf, DLT645_1997);
 			}
-		case RF_TX_DONE:
-			Radio->StartRx();
-		case RF_BUSY:
-		case RF_IDLE:
-			if(retry_count==0)   //第一次发送
+			if (rs != -1)
 			{
-				startTime = OSTimeGet();
-				Radio->SetTxPacket(string, stringlength);
-				retry_count++;
+				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;
+				}
 			}
-			if (OSTimeGet() - startTime > 10000) // 每次发送信号时记录上一次发送的时间如果超过一段时间没有响应则进行下一次发送
+			else
 			{
-				return;
+				currentDLT645Params->rxLen =0;
 			}
-		default:
-			OSTimeDlyHMSM(0, 0, 0, 200);
-			break;
-		}
+			sprintf(pubJsonString + strlen(pubJsonString), "{\"data\":[{\"deviceId\":\"%s\",\"%s\":%s},]}", 
+																				current_device->deviceID, currentDLT645Params->keyword, currentDLT645Params->data);
+			currentDLT645Params = currentDLT645Params->nextParams;
+			if (currentDLT645Params == NULL)	
+			{
+				return 0;
+			}
+		}			
 	}
+		return 1;
 }
 
-/*
-*********************************************************************************************************
-*	函 数 名: void slave_task(char *string)
-*	功能说明: 负责从站数据处理
-*	形    参:无
-*	返 回 值: 无
-*********************************************************************************************************
-*/
-volatile bool rxflag = false;
-void slave_task()
-{
-	switch (Radio->Process())
-	{
-	case RF_RX_DONE:
-		Radio->GetRxPacket(Buffer, &BufferSize);
-	  if(SlaveProtocolAnalysis(Buffer,BufferSize)==1)           //判断是否为该节点信息
-		{
-			data_acquisition();
-		  uint8_t node_string[256];                     
-			uint16_t node_string_Length;
-			nodeSendReaddValue(node_string,&node_string_Length);   //组装回传包
-			Radio->SetTxPacket(node_string, node_string_Length);   //发送回传包
-		}
-		delay_ms(1000);
-		break;
-	case RF_TX_DONE:
-		Radio->StartRx();
-		break;
-	case RF_IDLE:
-	case RF_BUSY:
-	default:
-		OSTimeDlyHMSM(0, 0, 0, 200);
+void WRITE_MODBUS_DATA(char* buf){
+	GATEWAY_PARAMS* get;
+	get = get_gateway_config_params();
+	GATEWAY_WRITE_MODBUS_COMMAND *currentModbusParams = get->device_params->params->gateway_write_modbus_command;
+
+	value data;
+	data.PowerValue = parseIntField(buf, "\"power\":");
+	data.TempValue = parseIntField(buf, "\"temp\":");
+	data.ModeValue = parseIntField(buf, "\"mode\":");
+	data.FanValue = parseIntField(buf, "\"fan\":");
+	data.RoomTempValue = parseIntField(buf, "\"roomTemp\":");
+	uint16_t slaveAddress;
+	uint16_t number;
+
+	if(data.PowerValue){
+			slaveAddress = currentModbusParams->slaveAddress;
+			number =currentModbusParams->registerAddress;
+			mmodbus_writeHoldingRegister16i(slaveAddress, number, data.PowerValue);
+	}
+	if(data.TempValue){
+			currentModbusParams = currentModbusParams->nextParams;
+			slaveAddress = currentModbusParams->slaveAddress;
+			number =currentModbusParams->registerAddress;
+			mmodbus_writeHoldingRegister16i(slaveAddress, number, data.TempValue);
+	}
+	if(data.ModeValue){
+			currentModbusParams = currentModbusParams->nextParams;
+			slaveAddress = currentModbusParams->slaveAddress;
+			number =currentModbusParams->registerAddress;
+			mmodbus_writeHoldingRegister16i(slaveAddress, number, data.ModeValue);
+	}
+	if(data.FanValue){
+			currentModbusParams = currentModbusParams->nextParams;
+			slaveAddress = currentModbusParams->slaveAddress;
+			number =currentModbusParams->registerAddress;
+			mmodbus_writeHoldingRegister16i(slaveAddress, number, data.FanValue);
+	}
+	if(data.RoomTempValue){
+			currentModbusParams = currentModbusParams->nextParams;
+			slaveAddress = currentModbusParams->slaveAddress;
+			number =currentModbusParams->registerAddress;
+			mmodbus_writeHoldingRegister16i(slaveAddress, number, data.RoomTempValue);
 	}
 }
-
-	#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			
- 	

+ 9 - 10
app/USER/main.c

@@ -16,7 +16,7 @@
 #include "sys_http.h"
 #include "task.h"
 #include "sys_sx1278.h"
-
+#include "node_data_acquisition.h"
 
 
 #define UNIQUE_ID 0x1fff7a10
@@ -71,7 +71,7 @@ char  gatewayId[11];
 int main(void)
 {
 	load_unique();
-	sprintf(gatewayId,"DT8pd3ac6h");
+	sprintf(gatewayId,"DTtest0001");//DT8pd3ac6h  DTbma5ac6h  DTtest0001
 	NVIC_Configuration();
 	my_mem_init(SRAMEX);
 	my_mem_init(SRAMIN);
@@ -80,15 +80,18 @@ int main(void)
 	//nandflash并不用初始化,调用fafts时会初始化
 	NET_STATUS_LED_Config();
 	USART_485_config();	
+  USART_485_DE_TX();
   USART_232_config();	
 	NAND_Init();
-	
 	OSInit();
+	#if 0
 	//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);
+	#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);
+	//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 */
@@ -170,8 +173,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
 /*
@@ -223,7 +226,3 @@ void load_unique(void)
  *  函数作用:加载本地存储配置如果上位机配置过则不走http配置路线,防止两线冲突
  */
 
-void load_232_config()
-{
-	
-}

+ 21 - 15
app/modbus/mmodbus.c

@@ -3,6 +3,7 @@
 #include "mmodbus.h"
 #include "usart.h"
 #include "delay.h"
+
 MModBus_t mmodbus;
 
 // #####################################################################################################
@@ -61,7 +62,6 @@ uint16_t mmodbus_crc16(const uint8_t *nData, uint16_t wLength)
 // #####################################################################################################
 void mmodbus_callback(void)
 {
-	OSIntEnter();
   if (RESET != USART_GetFlagStatus(_MMODBUS_USART,USART_FLAG_RXNE))
   {
     if (mmodbus.rxIndex < _MMODBUS_RXSIZE - 1)
@@ -73,21 +73,26 @@ void mmodbus_callback(void)
       USART_ReceiveData(_MMODBUS_USART);
   }
 
-  if ((mmodbus.rxIndex > 0) && RESET != USART_GetFlagStatus(_MMODBUS_USART, USART_FLAG_IDLE))
-  {
-    USART_ReceiveData(_MMODBUS_USART);
-    mmodbus.done = 1;
-		
-    return;
-  }
-  else
-  {
-    USART_GetITStatus(_MMODBUS_USART, USART_FLAG_RXNE);
-  }
+		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;
+		}
+		else
+		{
+			USART_GetITStatus(_MMODBUS_USART, USART_FLAG_RXNE);
+		}
   mmodbus.rxTime = gettick();
-	OSIntExit();
 }
 
+
 // ##################################################################################################
 uint16_t mmodbus_receiveRaw(uint32_t timeout)
 {
@@ -117,16 +122,17 @@ 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();
 	uint32_t startTime = gettick();
 	GPIO_WriteBit(_MMODBUS_CTRL_GPIO, _MMODBUS_CTRL_PIN,1);
 	mmodbus_delay(1);
 	for (uint16_t i = 0; i < size; i++)
 	{
 		USART_SendData(_MMODBUS_USART,data[i]);
-		
+		while (USART_GetFlagStatus(_MMODBUS_USART, USART_FLAG_TXE) == RESET);
 	} 
 	while (RESET == USART_GetFlagStatus(_MMODBUS_USART, USART_FLAG_TC));
-	
+//	OSIntExit();
 	GPIO_WriteBit(_MMODBUS_CTRL_GPIO, _MMODBUS_CTRL_PIN,0);
 	mmodbus.done=0;
 	mmodbus.txBusy = 0;

+ 1 - 1
app/modbus/mmodbusConfig.h

@@ -4,7 +4,7 @@
 #define _MMODBUS_FREERTOS         0
 #define _MMODBUS_RTU              1
 #define _MMODBUS_ASCII            0 //  not implemented yet
-#define _MMODBUS_USART            USART1             
+#define _MMODBUS_USART            USART3            
 #define _MMODBUS_RXSIZE           64  
 #define _MMODBUS_TXDMA            0   
 

+ 4 - 30
app/mqtt/MQTTClient.c

@@ -49,11 +49,13 @@ int mqtt_connectToMqttServer(int sock)
 	int len;
 	int buflen = sizeof(mqtt_sendBuf);
 	MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
+	GATEWAY_PARAMS *get;
+	get= get_gateway_config_params();
 	
 	//配置连接参数
 	data.clientID.cstring = CLIENT_ID;
-	data.username.cstring = USER_NAME;
-	data.password.cstring = PASSWORD;
+	data.username.cstring = get->username;
+	data.password.cstring = get->passwd;
 	data.keepAliveInterval = KEEPLIVE_TIME;
 	data.MQTTVersion = MQTT_VERSION;     
 	data.cleansession = 1;
@@ -700,31 +702,3 @@ void mqtt_disconnectServer(int sock)
 
 
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-

+ 1 - 0
app/mqtt/MQTTPacket.c

@@ -17,6 +17,7 @@
 
 #include "StackTrace.h"
 #include "MQTTPacket.h"
+#include "gateway_message.h"
 
 #include <string.h>
 

+ 7 - 8
app/mqtt/transport.c

@@ -78,14 +78,13 @@ int transport_receive(int sock)
 	int       optval;
 	
 	rc = read(sock, transport_recvBuf, sizeof(transport_recvBuf));
-
-	if(rc > 0) transport_dataLength = rc;
-	else       
-	{
-		transport_dataLength  = 0;
-		getsockopt(sock, SOL_SOCKET, SO_ERROR, &optval, &optlen);
-		rc = optval;
-	}
+		if(rc > 0) transport_dataLength = rc;
+		else       
+		{
+			transport_dataLength  = 0;
+			getsockopt(sock, SOL_SOCKET, SO_ERROR, &optval, &optlen);
+			rc = optval;
+		}
 	transport_dataStart = 0;
 	
 	return rc;