zhao006 vor 9 Monaten
Ursprung
Commit
e5185a7b3c

+ 6 - 2
app/HARDWARE/includes/log.h

@@ -12,6 +12,10 @@
 
 #define LOG_LEN_MAX 256
 
+#define _LOG_DEBUG
+#define _LOG_WARN
+#define _LOG_INFO
+
 typedef enum LOG_LEVEL_U
 {
 	LOG_NONE = 0,
@@ -22,7 +26,7 @@ typedef enum LOG_LEVEL_U
 	LOG_MAX,
 }logLevel_t;
 
-void LogPrint(logLevel_t logLevel, const char *func, const int line, char * fmt, ...);
-
+void LogPrint(logLevel_t logLevel,const char *file, const char *func, const int line, char * fmt, ...);
+void log_init(void);
 #define LOG_PRINT(logLevel, fmt, ...) LogPrint(logLevel, __FUNCTION__, __LINE__, fmt, ##__VA_ARGS__)
 #endif /* INC_LOG_H_ */

+ 5 - 0
app/HARDWARE/includes/tcp_server.h

@@ -0,0 +1,5 @@
+#ifndef TCP_SERVER_H
+#define TCP_SERVER_H
+void tcp_server_init();
+void tcp_server_task(void *Parameters);
+#endif

+ 11 - 0
app/HARDWARE/includes/udp_send.h

@@ -0,0 +1,11 @@
+#ifndef __UDP__H
+#define __UDP__H
+
+/* function declarations */
+/* initialize the tcp_client application */
+
+int udp_log_start(void);
+void udp_log_close(void);
+int udp_send_printf(char *p);
+
+#endif

+ 53 - 46
app/HARDWARE/sources/log.c

@@ -5,57 +5,64 @@
  *      Author: tangm
  */
 #include "log.h"
+#include "usart.h"
 #include <stdarg.h>
 #include <stdio.h>
-#include "usart.h"
-///重定向c库函数printf到串口,重定向后可使用printf函数
-int fputc(int ch, FILE *f)
-{
-		/* 发送一个字节数据到串口 */
-		USART_SendData(USART_232, (uint8_t) ch);
-		
-		/* 等待发送完毕 */
-		while (USART_GetFlagStatus(USART_232, USART_FLAG_TXE) == RESET);	
-		return (ch);
-}
+#include "ucos_ii.h"
 
-///重定向c库函数scanf到串口,重写向后可使用scanf、getchar等函数
-int fgetc(FILE *f)
-{
-		/* 等待串口输入数据 */
-		while (USART_GetFlagStatus(USART_232, USART_FLAG_RXNE) == RESET);
+#include "main.h"
+#include "udp_send.h"
+#include "stdlib.h"
+#include "malloc.h"
 
-		return (int)USART_ReceiveData(USART_232);
+static OS_EVENT  *logMutex;
+uint8_t perr;
+void LogPrint(logLevel_t logLevel,const char *file, const char *func, const int line, char * fmt, ...)
+{
+	
+	OSMutexPend(logMutex, 5000, &perr);
+	if(perr == OS_ERR_NONE)
+	{	
+			udp_log_start();
+			va_list args;
+			va_start(args, fmt);
+			char buf[LOG_LEN_MAX];
+			vsnprintf(buf, sizeof(buf), fmt, args);
+			va_end(args);
+			char *p=mymalloc(SRAMEX, 1024);
+			switch (logLevel)
+			{
+				#ifdef _LOG_INFO
+						case LOG_INFO:
+							sprintf(p,"[%s][%s][%s:%4d] %s\r\n", "INFO", file, func, line, buf);
+							break;
+				#endif
+				#ifdef _LOG_DEBUG
+						case LOG_DEBUG:
+							sprintf(p,"[%s][%s][%s:%4d] %s\r\n", "DEBUG",file, func, line, buf);
+							break;
+				#endif
+				#ifdef _LOG_WARN
+						case LOG_WARN:
+							sprintf(p,"[%s][%s][%s:%4d] %s\r\n", "WARN", file, func, line, buf);
+							break;
+				#endif
+						case LOG_ERROR:
+							sprintf(p,"[%s][%s][%s:%4d] %s\r\n", "ERROR",file, func, line, buf);
+							break;
+						default:
+							break;
+			}
+			udp_send_printf(p);
+			myfree(SRAMEX, p);
+			udp_log_close();
+			perr = OSMutexPost(logMutex);
+		}
 }
 
-void LogPrint(logLevel_t logLevel, const char *func, const int line, char * fmt, ...)
+void log_init()
 {
-	va_list args;
-	va_start(args, fmt);
-	char buf[LOG_LEN_MAX];
-	vsnprintf(buf, sizeof(buf), fmt, args);
-	va_end(args);
-	switch (logLevel)
-	{
-#ifdef _LOG_INFO
-		case LOG_INFO:
-			printf("LOG[%-5s][%-20s:%4d] %s\r\n", "INFO", func, line, buf);
-			break;
-#endif
-#ifdef _LOG_DEBUG
-		case LOG_DEBUG:
-			printf("LOG[%-5s][%-20s:%4d] %s\r\n", "DEBUG", func, line, buf);
-			break;
-#endif
-#ifdef _LOG_WARN
-		case LOG_WARN:
-			printf("LOG[%-5s][%-20s:%4d] %s\r\n", "WARN", func, line, buf);
-			break;
-#endif
-		case LOG_ERROR:
-			printf("LOG[%-5s][%-20s:%4d] %s\r\n", "ERROR", func, line, buf);
-			break;
-		default:
-			break;
-	}
+	
+	logMutex = OSMutexCreate (0, &perr);//创建一个log互斥量
+	OSTimeDly(100);
 }

+ 71 - 0
app/HARDWARE/sources/tcp_server.c

@@ -0,0 +1,71 @@
+#include <string.h>
+#include <stdio.h>
+#include "tcp_server.h"
+#include "stm32f2xx.h"
+#include "main.h"
+#include "log.h"
+
+#include "lwip/tcp.h"
+#include "lwip/memp.h"
+#include "lwip/api.h"
+#include "lwip/sockets.h"
+#include "lwip/opt.h"
+#include "lwip/sys.h"
+
+
+void tcp_server_task(void *Parameters)
+{
+    int ret,sockfd;
+    struct sockaddr_in tcpServerSock;
+    struct sockaddr_in client_sock;
+    tcpServerSock.sin_family = AF_INET;
+		inet_aton("192.168.2.212",&(tcpServerSock.sin_addr));
+//    tcpServerSock.sin_addr.s_addr = htonl(IPADDR_ANY);
+    tcpServerSock.sin_port = htons(8080);
+tcp_server_begin:
+    sockfd = socket(AF_INET, SOCK_STREAM, 0);
+    if (sockfd < 0)
+    {
+        goto tcp_server_begin;
+    }
+    ret = bind(sockfd, (struct sockaddr *)&tcpServerSock, sizeof(tcpServerSock));
+    if (ret < 0)
+    {
+        lwip_close(sockfd);
+        sockfd = -1;
+        goto tcp_server_begin;
+    }
+    ret = listen(sockfd, 10);
+    if (ret < 0)
+    {
+        lwip_close(sockfd);
+        sockfd = -1;
+        goto tcp_server_begin;
+    }
+		
+    while (1)
+    {
+				OSTimeDly(1000);
+        socklen_t len = sizeof(client_sock);
+        int client_socket = accept(sockfd, (struct sockaddr*)&client_sock,&len);
+				LogPrint(LOG_INFO,__FILE__,__FUNCTION__,__LINE__,"ÉÏλ»ú³É¹¦Á¬½Ó%s");
+        if (client_socket<0)
+        {
+            printf("error");
+        }
+    }
+    
+}
+/*!
+    \brief      initialize the tcp_client application
+    \param[in]  none
+    \param[out] none
+    \retval     none
+*/
+#define  TCP_TASK_PRIO     9
+#define  TCP_STK_SIZE  	 	1024
+OS_STK 	 TCP_TASK_STK[TCP_STK_SIZE];
+void tcp_server_init(void)
+{ 
+		OSTaskCreate(tcp_server_task,(void*)0,(OS_STK*)&TCP_TASK_STK[TCP_STK_SIZE - 1],TCP_TASK_PRIO);
+}

+ 74 - 0
app/HARDWARE/sources/udp_send.c

@@ -0,0 +1,74 @@
+
+#include "udp_send.h" 
+#include "main.h"
+#include <string.h>
+#include <stdio.h>
+#include "stm32f2xx.h"
+#include "lwip/opt.h"
+#include "lwip/tcp.h"
+#include "lwip/sys.h"
+#include "lwip/memp.h"
+#include "lwip/api.h"
+
+#include "lwip/sockets.h"
+
+volatile int8_t sockfd=-1;
+#define UDP_LOCAL_PORT 12345
+#define UDP_REMOTE_PORT 54321
+#define SERCER_IP_ADDRESS "192.168.2.211"       //接收服务器ip
+struct sockaddr_in remote_addr;
+
+int udp_log_start()
+{
+	int ret;
+	struct sockaddr_in local_addr;
+	// 创建UDP套接字
+  sockfd = socket(AF_INET, SOCK_DGRAM, 0);
+  if (sockfd < -1) {
+    perror("Error creating socket");
+		goto exit;
+  }
+	local_addr.sin_family=AF_INET;
+	local_addr.sin_port=htons(UDP_LOCAL_PORT);
+	local_addr.sin_addr.s_addr=INADDR_ANY;
+	ret = bind(sockfd, (struct sockaddr *)&local_addr, sizeof(local_addr));
+  if(ret < 0)
+	{ 
+    lwip_close(sockfd);
+    sockfd = -1;
+    goto exit;
+  }
+	//设置远程服务器地址 TODO:服务器地址配置后自动获取
+	remote_addr.sin_family=AF_INET;
+	remote_addr.sin_port=htons(UDP_REMOTE_PORT);
+	inet_aton(SERCER_IP_ADDRESS,&(remote_addr.sin_addr));
+	return 1;
+	
+exit:
+			return -1;
+}
+
+void udp_log_close()
+{
+	lwip_close(sockfd);
+	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 udp_send_printf(char *p)
+{
+	if(sockfd>=0)
+	{
+		 sendto(sockfd, p, strlen(p), 0, (struct sockaddr *)&remote_addr,sizeof(remote_addr));
+	}
+	return 1;
+}

+ 114 - 90
app/MDKProject/lora_gateway.uvoptx

@@ -178,14 +178,14 @@
         <Ww>
           <count>4</count>
           <WinNumber>1</WinNumber>
-          <ItemText>dlt645</ItemText>
+          <ItemText>read_modbus_command</ItemText>
         </Ww>
       </WatchWindow1>
       <MemoryWindow1>
         <Mm>
           <WinNumber>1</WinNumber>
           <SubType>8</SubType>
-          <ItemText>pubJsonString</ItemText>
+          <ItemText>buf</ItemText>
           <AccSizeX>0</AccSizeX>
         </Mm>
       </MemoryWindow1>
@@ -951,6 +951,30 @@
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
+    <File>
+      <GroupNumber>5</GroupNumber>
+      <FileNumber>57</FileNumber>
+      <FileType>1</FileType>
+      <tvExp>0</tvExp>
+      <tvExpOptDlg>0</tvExpOptDlg>
+      <bDave2>0</bDave2>
+      <PathWithFileName>..\HARDWARE\sources\tcp_server.c</PathWithFileName>
+      <FilenameWithoutPath>tcp_server.c</FilenameWithoutPath>
+      <RteFlg>0</RteFlg>
+      <bShared>0</bShared>
+    </File>
+    <File>
+      <GroupNumber>5</GroupNumber>
+      <FileNumber>58</FileNumber>
+      <FileType>1</FileType>
+      <tvExp>0</tvExp>
+      <tvExpOptDlg>0</tvExpOptDlg>
+      <bDave2>0</bDave2>
+      <PathWithFileName>..\HARDWARE\sources\udp_send.c</PathWithFileName>
+      <FilenameWithoutPath>udp_send.c</FilenameWithoutPath>
+      <RteFlg>0</RteFlg>
+      <bShared>0</bShared>
+    </File>
   </Group>
 
   <Group>
@@ -961,7 +985,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>6</GroupNumber>
-      <FileNumber>57</FileNumber>
+      <FileNumber>59</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -973,7 +997,7 @@
     </File>
     <File>
       <GroupNumber>6</GroupNumber>
-      <FileNumber>58</FileNumber>
+      <FileNumber>60</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -985,7 +1009,7 @@
     </File>
     <File>
       <GroupNumber>6</GroupNumber>
-      <FileNumber>59</FileNumber>
+      <FileNumber>61</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1005,7 +1029,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>7</GroupNumber>
-      <FileNumber>60</FileNumber>
+      <FileNumber>62</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1017,7 +1041,7 @@
     </File>
     <File>
       <GroupNumber>7</GroupNumber>
-      <FileNumber>61</FileNumber>
+      <FileNumber>63</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1029,7 +1053,7 @@
     </File>
     <File>
       <GroupNumber>7</GroupNumber>
-      <FileNumber>62</FileNumber>
+      <FileNumber>64</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1041,7 +1065,7 @@
     </File>
     <File>
       <GroupNumber>7</GroupNumber>
-      <FileNumber>63</FileNumber>
+      <FileNumber>65</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1053,7 +1077,7 @@
     </File>
     <File>
       <GroupNumber>7</GroupNumber>
-      <FileNumber>64</FileNumber>
+      <FileNumber>66</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1065,7 +1089,7 @@
     </File>
     <File>
       <GroupNumber>7</GroupNumber>
-      <FileNumber>65</FileNumber>
+      <FileNumber>67</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1077,7 +1101,7 @@
     </File>
     <File>
       <GroupNumber>7</GroupNumber>
-      <FileNumber>66</FileNumber>
+      <FileNumber>68</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1097,7 +1121,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>8</GroupNumber>
-      <FileNumber>67</FileNumber>
+      <FileNumber>69</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1109,7 +1133,7 @@
     </File>
     <File>
       <GroupNumber>8</GroupNumber>
-      <FileNumber>68</FileNumber>
+      <FileNumber>70</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1121,7 +1145,7 @@
     </File>
     <File>
       <GroupNumber>8</GroupNumber>
-      <FileNumber>69</FileNumber>
+      <FileNumber>71</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1133,7 +1157,7 @@
     </File>
     <File>
       <GroupNumber>8</GroupNumber>
-      <FileNumber>70</FileNumber>
+      <FileNumber>72</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1145,7 +1169,7 @@
     </File>
     <File>
       <GroupNumber>8</GroupNumber>
-      <FileNumber>71</FileNumber>
+      <FileNumber>73</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1157,7 +1181,7 @@
     </File>
     <File>
       <GroupNumber>8</GroupNumber>
-      <FileNumber>72</FileNumber>
+      <FileNumber>74</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1169,7 +1193,7 @@
     </File>
     <File>
       <GroupNumber>8</GroupNumber>
-      <FileNumber>73</FileNumber>
+      <FileNumber>75</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1181,7 +1205,7 @@
     </File>
     <File>
       <GroupNumber>8</GroupNumber>
-      <FileNumber>74</FileNumber>
+      <FileNumber>76</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1193,7 +1217,7 @@
     </File>
     <File>
       <GroupNumber>8</GroupNumber>
-      <FileNumber>75</FileNumber>
+      <FileNumber>77</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1205,7 +1229,7 @@
     </File>
     <File>
       <GroupNumber>8</GroupNumber>
-      <FileNumber>76</FileNumber>
+      <FileNumber>78</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1225,7 +1249,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>9</GroupNumber>
-      <FileNumber>77</FileNumber>
+      <FileNumber>79</FileNumber>
       <FileType>5</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1237,7 +1261,7 @@
     </File>
     <File>
       <GroupNumber>9</GroupNumber>
-      <FileNumber>78</FileNumber>
+      <FileNumber>80</FileNumber>
       <FileType>2</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1249,7 +1273,7 @@
     </File>
     <File>
       <GroupNumber>9</GroupNumber>
-      <FileNumber>79</FileNumber>
+      <FileNumber>81</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1269,7 +1293,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>10</GroupNumber>
-      <FileNumber>80</FileNumber>
+      <FileNumber>82</FileNumber>
       <FileType>5</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1281,7 +1305,7 @@
     </File>
     <File>
       <GroupNumber>10</GroupNumber>
-      <FileNumber>81</FileNumber>
+      <FileNumber>83</FileNumber>
       <FileType>5</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1301,7 +1325,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>82</FileNumber>
+      <FileNumber>84</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1313,7 +1337,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>83</FileNumber>
+      <FileNumber>85</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1325,7 +1349,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>84</FileNumber>
+      <FileNumber>86</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1337,7 +1361,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>85</FileNumber>
+      <FileNumber>87</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1349,7 +1373,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>86</FileNumber>
+      <FileNumber>88</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1361,7 +1385,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>87</FileNumber>
+      <FileNumber>89</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1373,7 +1397,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>88</FileNumber>
+      <FileNumber>90</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1385,7 +1409,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>89</FileNumber>
+      <FileNumber>91</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1397,7 +1421,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>90</FileNumber>
+      <FileNumber>92</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1409,7 +1433,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>91</FileNumber>
+      <FileNumber>93</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1421,7 +1445,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>92</FileNumber>
+      <FileNumber>94</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1433,7 +1457,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>93</FileNumber>
+      <FileNumber>95</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1445,7 +1469,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>94</FileNumber>
+      <FileNumber>96</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1457,7 +1481,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>95</FileNumber>
+      <FileNumber>97</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1469,7 +1493,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>96</FileNumber>
+      <FileNumber>98</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1481,7 +1505,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>97</FileNumber>
+      <FileNumber>99</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1493,7 +1517,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>98</FileNumber>
+      <FileNumber>100</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1505,7 +1529,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>99</FileNumber>
+      <FileNumber>101</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1517,7 +1541,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>100</FileNumber>
+      <FileNumber>102</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1529,7 +1553,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>101</FileNumber>
+      <FileNumber>103</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1541,7 +1565,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>102</FileNumber>
+      <FileNumber>104</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1553,7 +1577,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>103</FileNumber>
+      <FileNumber>105</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1565,7 +1589,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>104</FileNumber>
+      <FileNumber>106</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1577,7 +1601,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>105</FileNumber>
+      <FileNumber>107</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1589,7 +1613,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>106</FileNumber>
+      <FileNumber>108</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1601,7 +1625,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>107</FileNumber>
+      <FileNumber>109</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1613,7 +1637,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>108</FileNumber>
+      <FileNumber>110</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1625,7 +1649,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>109</FileNumber>
+      <FileNumber>111</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1637,7 +1661,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>110</FileNumber>
+      <FileNumber>112</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1649,7 +1673,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>111</FileNumber>
+      <FileNumber>113</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1661,7 +1685,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>112</FileNumber>
+      <FileNumber>114</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1673,7 +1697,7 @@
     </File>
     <File>
       <GroupNumber>11</GroupNumber>
-      <FileNumber>113</FileNumber>
+      <FileNumber>115</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1693,7 +1717,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>12</GroupNumber>
-      <FileNumber>114</FileNumber>
+      <FileNumber>116</FileNumber>
       <FileType>5</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1705,7 +1729,7 @@
     </File>
     <File>
       <GroupNumber>12</GroupNumber>
-      <FileNumber>115</FileNumber>
+      <FileNumber>117</FileNumber>
       <FileType>5</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1717,7 +1741,7 @@
     </File>
     <File>
       <GroupNumber>12</GroupNumber>
-      <FileNumber>116</FileNumber>
+      <FileNumber>118</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1729,7 +1753,7 @@
     </File>
     <File>
       <GroupNumber>12</GroupNumber>
-      <FileNumber>117</FileNumber>
+      <FileNumber>119</FileNumber>
       <FileType>5</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1741,7 +1765,7 @@
     </File>
     <File>
       <GroupNumber>12</GroupNumber>
-      <FileNumber>118</FileNumber>
+      <FileNumber>120</FileNumber>
       <FileType>5</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1753,7 +1777,7 @@
     </File>
     <File>
       <GroupNumber>12</GroupNumber>
-      <FileNumber>119</FileNumber>
+      <FileNumber>121</FileNumber>
       <FileType>5</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1765,7 +1789,7 @@
     </File>
     <File>
       <GroupNumber>12</GroupNumber>
-      <FileNumber>120</FileNumber>
+      <FileNumber>122</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1777,7 +1801,7 @@
     </File>
     <File>
       <GroupNumber>12</GroupNumber>
-      <FileNumber>121</FileNumber>
+      <FileNumber>123</FileNumber>
       <FileType>5</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1789,7 +1813,7 @@
     </File>
     <File>
       <GroupNumber>12</GroupNumber>
-      <FileNumber>122</FileNumber>
+      <FileNumber>124</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1809,7 +1833,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>13</GroupNumber>
-      <FileNumber>123</FileNumber>
+      <FileNumber>125</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1821,7 +1845,7 @@
     </File>
     <File>
       <GroupNumber>13</GroupNumber>
-      <FileNumber>124</FileNumber>
+      <FileNumber>126</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1833,7 +1857,7 @@
     </File>
     <File>
       <GroupNumber>13</GroupNumber>
-      <FileNumber>125</FileNumber>
+      <FileNumber>127</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1845,7 +1869,7 @@
     </File>
     <File>
       <GroupNumber>13</GroupNumber>
-      <FileNumber>126</FileNumber>
+      <FileNumber>128</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1857,7 +1881,7 @@
     </File>
     <File>
       <GroupNumber>13</GroupNumber>
-      <FileNumber>127</FileNumber>
+      <FileNumber>129</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1869,7 +1893,7 @@
     </File>
     <File>
       <GroupNumber>13</GroupNumber>
-      <FileNumber>128</FileNumber>
+      <FileNumber>130</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1881,7 +1905,7 @@
     </File>
     <File>
       <GroupNumber>13</GroupNumber>
-      <FileNumber>129</FileNumber>
+      <FileNumber>131</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1893,7 +1917,7 @@
     </File>
     <File>
       <GroupNumber>13</GroupNumber>
-      <FileNumber>130</FileNumber>
+      <FileNumber>132</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1905,7 +1929,7 @@
     </File>
     <File>
       <GroupNumber>13</GroupNumber>
-      <FileNumber>131</FileNumber>
+      <FileNumber>133</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1917,7 +1941,7 @@
     </File>
     <File>
       <GroupNumber>13</GroupNumber>
-      <FileNumber>132</FileNumber>
+      <FileNumber>134</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1929,7 +1953,7 @@
     </File>
     <File>
       <GroupNumber>13</GroupNumber>
-      <FileNumber>133</FileNumber>
+      <FileNumber>135</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1941,7 +1965,7 @@
     </File>
     <File>
       <GroupNumber>13</GroupNumber>
-      <FileNumber>134</FileNumber>
+      <FileNumber>136</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1953,7 +1977,7 @@
     </File>
     <File>
       <GroupNumber>13</GroupNumber>
-      <FileNumber>135</FileNumber>
+      <FileNumber>137</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1965,7 +1989,7 @@
     </File>
     <File>
       <GroupNumber>13</GroupNumber>
-      <FileNumber>136</FileNumber>
+      <FileNumber>138</FileNumber>
       <FileType>5</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1985,7 +2009,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>14</GroupNumber>
-      <FileNumber>137</FileNumber>
+      <FileNumber>139</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1997,7 +2021,7 @@
     </File>
     <File>
       <GroupNumber>14</GroupNumber>
-      <FileNumber>138</FileNumber>
+      <FileNumber>140</FileNumber>
       <FileType>5</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -2017,7 +2041,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>15</GroupNumber>
-      <FileNumber>139</FileNumber>
+      <FileNumber>141</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -2029,7 +2053,7 @@
     </File>
     <File>
       <GroupNumber>15</GroupNumber>
-      <FileNumber>140</FileNumber>
+      <FileNumber>142</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -2041,7 +2065,7 @@
     </File>
     <File>
       <GroupNumber>15</GroupNumber>
-      <FileNumber>141</FileNumber>
+      <FileNumber>143</FileNumber>
       <FileType>1</FileType>
       <tvExp>1</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -2053,7 +2077,7 @@
     </File>
     <File>
       <GroupNumber>15</GroupNumber>
-      <FileNumber>142</FileNumber>
+      <FileNumber>144</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -2065,7 +2089,7 @@
     </File>
     <File>
       <GroupNumber>15</GroupNumber>
-      <FileNumber>143</FileNumber>
+      <FileNumber>145</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -2085,7 +2109,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>16</GroupNumber>
-      <FileNumber>144</FileNumber>
+      <FileNumber>146</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>

+ 10 - 0
app/MDKProject/lora_gateway.uvprojx

@@ -684,6 +684,16 @@
               <FileType>1</FileType>
               <FilePath>..\HARDWARE\sources\malloc.c</FilePath>
             </File>
+            <File>
+              <FileName>tcp_server.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\HARDWARE\sources\tcp_server.c</FilePath>
+            </File>
+            <File>
+              <FileName>udp_send.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\HARDWARE\sources\udp_send.c</FilePath>
+            </File>
           </Files>
         </Group>
         <Group>

BIN
app/OBJ/lora_gateway.axf


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


+ 32 - 2
app/System/includes/gateway_message.h

@@ -24,7 +24,7 @@ typedef struct _GATEWAY_READ_MODBUS_COMMAND
 	uint8_t keyword[20]; // 所读属性的名称
 	uint16_t registerAddress;  // 寄存器地址
 	uint8_t read_command_flag; // 是否成功读取到的标志
-	uint8_t value[4];          //读取到的数据值
+	uint32_t value;          //读取到的数据值
 	uint16_t registerByteNum;
 	uint8_t decimalPoint;
 	struct _GATEWAY_READ_MODBUS_COMMAND *nextParams;
@@ -104,7 +104,37 @@ int extract_substring(const char *input_string, const char *start_token, const c
 
 //////////////////////////////////////////////////////////////////////////////////////////////////////////
 
+void analysis_read_data(DEVICE_PARAMS *device, char* buf);
 
+typedef struct _MODBUS_READ_DATA
+{
+	uint8_t deviceId[20];
+	// modbus读出的数据名
+	uint8_t power;	
+	uint8_t temp;
+	uint8_t mode;
+	uint8_t fan;
+	uint8_t roomTemp;
+	uint8_t fault;
+	
+	struct _MODBUS_READ_DATA *nextParams;
+} MODBUS_READ_DATA;
+
+typedef struct _DLT_READ_DATA
+{
+	uint8_t deviceId[20];
+	// dlt读出的数据名
+	uint8_t deviceID645[6];
+	uint8_t keyword[20];   // 所读属性的名称
+	uint32_t Identification; // 645数据标识
+	uint8_t data[9];
+	
+	struct _DLT_READ_DATA *nextParams;
+} DLT_READ_DATA;
 
-
+typedef struct _READ_DATA
+{
+	MODBUS_READ_DATA   *read_modbus_data;
+	DLT_READ_DATA  		 *read_dlt645_data;
+}READ_DATA;
 #endif

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

@@ -44,9 +44,9 @@ extern OS_EVENT *JsonQ;
 void mqtt_threadCreate(void);
 
 
-#define jsonMaxSize 1000
+#define jsonMaxSize 2 * 1024
 extern char pubJsonString[jsonMaxSize];
-//extern char pubJsonStringCopy[jsonMaxSize];
+extern char pubJsonStringCopy[jsonMaxSize];
 #endif 
 
 

+ 4 - 11
app/System/includes/task.h

@@ -7,21 +7,14 @@
 //主要的数据处理线程
 #define DATA_TASK_PRIO 8
 #define DATA_STK_SIZE  2*1024
-
-#define GetCurrentTime()  OSTimeGet()
+#define   GetCurrentTime()  OSTimeGet()
 extern void data_task(void *pdata);
 extern tRadioDriver *Radio;
 
-/* 写入modbus寄存器 */
 void write_modbus_data(char* buf);
-/* 发送至mqtt */
-void send_mqtt(char*buf, int jsonCunt,char*pubJsonStringCopy);
-/* 比较data1与data2*/
+void send_mqtt(char*buf);
 void find_difference(char* data1, char* data2, char* string);
-/* 读取设备数据 */
-int read_device_data(DEVICE_PARAMS *current_device,char* buf);
-
-void send_modbus_data(GATEWAY_PARAMS *get, char* buf, int jsonCunt, char*pubJsonStringCopy);
-void send_dlt645_data(GATEWAY_PARAMS *get, char* buf, int jsonCunt, char*pubJsonStringCopy);
+int read_device_data1(DEVICE_PARAMS *current_device,char* buf);
+int read_device_data2(DEVICE_PARAMS *current_device,char* buf);
 
 #endif

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

@@ -310,3 +310,85 @@ 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;														
+//			}
+//		}	
+//}

+ 3 - 0
app/System/source/sys_http.c

@@ -7,6 +7,7 @@
 #include "myFIle.h"
 #include "includes.h"
 #include "sys_http.h"
+#include "log.h"
 
 uint8_t load_http_config=0;
 //get请求  http://gpu.ringzle.com:8082/iot/transmit/getTransmitConfig/DT8pd3ac6h 端口8082
@@ -27,6 +28,8 @@ void http_getDemo(void)
 	}
 	HTTP_PRINTF("%s", (char *)http_data);
 	HTTP_PRINTF("\r\n ret=%d datalen=%d\r\n", ret, datalen);
+	LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "%s", (char *)http_data);
+	LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "\r\n ret=%d datalen=%d\r\n", ret, datalen);
 	myfree(SRAMEX,http_data);
 	myfree(SRAMEX,http);
 }

+ 18 - 5
app/System/source/sys_mqtt.c

@@ -14,6 +14,7 @@
 #include "myFile.h"
 #include "gateway_message.h"
 #include "task.h"
+#include "log.h"
 
 /********************************************************
 * @brief  连接到服务器
@@ -32,17 +33,20 @@ int mqtt_userConnect(void)
 	if(sock < 0) 
 	{
 		MQTT_PRINTF("connect tcp server error \r\n");
+//		LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "connect tcp server error");
 		return -1;
 	}
 	MQTT_PRINTF("connect tcp server success \r\n");
+//	LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "connect tcp server success");
 	//连接到mqtt服务器
 	if(mqtt_connectToMqttServer(sock) <= 0) 
 	{
 		MQTT_PRINTF("connect mqtt server error \r\n");
+//		LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "connect mqtt server error");
 		return -1;
 	}
   MQTT_PRINTF("connect mqtt server success \r\n");
-	
+	LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "connect mqtt server success");
 	return sock;
 }
 
@@ -71,6 +75,7 @@ void mqtt_outputMsg(MQTTString *name, uint8_t *msgbuf, int msglen, uint16_t id,
 {
 	int lenght=msglen;
 	MQTT_PRINTF("receive a msg: id=%d qos=%d topic=%s msg=%s \r\n", id, qos, name->lenstring.data, msgbuf);
+//	LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "receive a msg: id=%d qos=%d topic=%s msg=%s \r\n", id, qos, name->lenstring.data, msgbuf);
 	StringInfo message;
 	message.p=mymalloc(SRAMEX ,msglen);
 	memcpy(message.p,msgbuf,msglen);
@@ -157,7 +162,7 @@ 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;
@@ -207,7 +212,7 @@ void mqtt_userManThread(void *arg)
 	void *mboxMsg;
 
 	MQTT_PRINTF("mqtt mainthread start \r\n");
-	
+//	LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "mqtt mainthread start");
 __MQTT_START:	
 	
 	//1.建立与服务器的连接
@@ -218,6 +223,7 @@ __MQTT_START:
 		mysock = mqtt_userConnect();
 		OSTimeDly(2000);
 	}
+//	LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "mqtt connect success");
 	OSMboxAccept(mqtt_sendMseeageMbox); //清空mbox的数据
 	mqtt_connectFlag = 1;
 	
@@ -226,9 +232,14 @@ __MQTT_START:
 	if(rc <= 0)
 	{
 		MQTT_PRINTF("subscribe error \r\n");
+//		LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "subscribe error");
 		if(rc == -1) goto __MQTT_START;          //如果网络发生错误,重新建立连接
 	}
-	else MQTT_PRINTF("subscribe success \r\n");
+	else 
+	{
+		MQTT_PRINTF("subscribe success \r\n");
+//		LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "subscribe success");
+	}
 	
 	//3.循环发送数据
 	while(1)
@@ -265,7 +276,7 @@ void mqtt_userReceiveThread(void *arg)
 	int packetType;
 	int msg;
 	MQTT_PRINTF("mqtt receivethread start \r\n");
-	
+//	LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "mqtt receivethread start");
 	while(1)
 	{
 		if(mqtt_connectFlag == 1)
@@ -276,11 +287,13 @@ void mqtt_userReceiveThread(void *arg)
 				if(len == EWOULDBLOCK)     //接收数据超时,重新接收
 				{
 					MQTT_PRINTF("receive data timeout \r\n");
+//					LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "receive data timeout");
 					continue;
 				}
 				else  //接收数据时,网络发生了异常,给主线程发送消息,重新建立连接
 				{
 					MQTT_PRINTF("sock close \r\n");
+//					LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "sock close");
 					transport_close(mysock);
 					
 					mqtt_connectFlag = 0;

+ 188 - 77
app/System/source/task.c

@@ -22,6 +22,9 @@
 #include "dlt645_1997_private.h"
 
 char string[512];
+uint8_t read_cnt = 0;
+uint8_t count = 0;
+uint8_t jsonCunt = 1;	
 /*
 *********************************************************************************************************
 *	函 数 名: void data_task(void *pdata)
@@ -32,12 +35,14 @@ char string[512];
 */
 void data_task(void *pdata)
 {
+		
 		OS_CPU_SR cpu_sr;
 		pdata = pdata;
+
 		dlt645_init(100);
 		mmodbus_init(1);
 		
-		int jsonCunt = 1;	
+		
 		char *device_config_json = mymalloc(SRAMEX, 9 * 1024);
 		read_file("device.txt", device_config_json);
 		addGatewayParams(device_config_json);
@@ -48,19 +53,38 @@ void data_task(void *pdata)
 		DEVICE_PARAMS *current_device=get->device_params;
 //		Config_485_Port(get->baudrate, get->dataBits, get->stopBit, get->parity, get->flowControl);
 		char *buf = mymalloc(SRAMEX, 9 * 1024);	// 接收读取的数据
-		char *pubJsonStringCopy = mymalloc(SRAMEX, 9 * 1024);	// 备份数据
-		memset(buf, 0, 9 * 1024);	
+		memset(buf, 0, 9 * 1024);		
 		while (current_device!=NULL)
-		{						
-				read_device_data(current_device, buf);	//读取数据	
-				send_mqtt(buf, jsonCunt,pubJsonStringCopy);								//发送数据
-				jsonCunt = 0;		
-				memset(buf,0,strlen(buf));	
-				current_device=get->device_params;		
-				OSTimeDly(1);
+		{		
+			time1 = GetCurrentTime();
+			if(mqtt_connectFlag)
+			{
+					if(jsonCunt || time2  <= time1 - (10 * 1000))// 10s进行一次全数据发送
+					{
+							read_device_data1(current_device, buf);
+							send_mqtt(buf);
+							jsonCunt = 0;
+							memset(buf,0,strlen(buf));	
+							current_device=get->device_params;	
+							time2 = GetCurrentTime();
+//							LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "data for all");
+					}
+					else
+					{
+						read_device_data2(current_device, buf);
+						if(count > 0)// count检测buf内是否含有数据
+						{
+							send_mqtt(buf);
+							memset(buf,0,strlen(buf));	
+							current_device=get->device_params;
+							count = 0;
+//							LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "different data");
+						}
+					}	
+			}		
+		OSTimeDly(100);
 		}
 		myfree(SRAMEX, buf);
-		myfree(SRAMEX,pubJsonStringCopy);
 }	
 /*
 *********************************************************************************************************
@@ -83,6 +107,10 @@ void mqtt_to_device(){
 		}	
 }
 
+
+void find_diff(char* buf, char* string) {
+	
+}
 /*
 *********************************************************************************************************
 *	函 数 名: int READ_MODBUS_DATA(DEVICE_PARAMS *device)
@@ -91,8 +119,9 @@ void mqtt_to_device(){
 *	返 回 值: 1: 成功 0:失败
 *********************************************************************************************************
 */
-int read_device_data(DEVICE_PARAMS *device, char* buf)
+int read_device_data1(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;	
@@ -110,6 +139,7 @@ int read_device_data(DEVICE_PARAMS *device, char* buf)
 																		 currentModbusParams->registerAddress,
 																		 currentModbusParams->registerByteNum /2,
 																		 data);
+
 								if (success)
 								{
 										uint32_t value;
@@ -123,15 +153,13 @@ int read_device_data(DEVICE_PARAMS *device, char* buf)
 										}		
 										if (currentModbusParams->decimalPoint == 0)
 										{
-												currentModbusParams->value[0] = value;
-												currentModbusParams->value[1] = value << 8;
-												currentModbusParams->value[2] = value << 16;
-												currentModbusParams->value[3] = value << 24;
+												currentModbusParams->value = value;
+
 										}
 										else
 										{
 												float convertedValue = (float)value / pow(10, currentModbusParams->decimalPoint);
-												memcpy(currentModbusParams->value, &convertedValue, 4);
+												currentModbusParams->value=convertedValue;
 										}
 										sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":%d},", 
 																							current_device->deviceID, currentModbusParams->keyword, value);									
@@ -152,7 +180,7 @@ int read_device_data(DEVICE_PARAMS *device, char* buf)
 										currentModbusParams = current_device->params->gateway_read_modbus_command;
 										if(current_device == NULL)
 										{
-												sprintf(buf + strlen(buf) - 1, "}"); 
+												sprintf(buf + strlen(buf) - 1, ""); 
 												return 1;
 										}
 								}												
@@ -219,7 +247,7 @@ int read_device_data(DEVICE_PARAMS *device, char* buf)
 										currentDLT645Params = current_device->params->gateway_read_dlt645_command;
 										if(current_device == NULL)
 										{
-												sprintf(buf + strlen(buf) - 1, "}"); 
+												sprintf(buf + strlen(buf) - 1, ""); 
 												return 1;
 										}
 								}										
@@ -228,6 +256,137 @@ int read_device_data(DEVICE_PARAMS *device, char* buf)
 		}		
 		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)
+						{
+								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((value - currentModbusParams->value) != 0)
+										{
+												sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":%d},", 
+																									current_device->deviceID, currentModbusParams->keyword, value);
+												count++;
+										}
+											if (currentModbusParams->decimalPoint == 0)
+										{
+												currentModbusParams->value = value;
+
+										}
+										else
+										{
+												float convertedValue = (float)value / pow(10, currentModbusParams->decimalPoint);
+												currentModbusParams->value=convertedValue;
+										}
+																	
+								}					
+								currentModbusParams = currentModbusParams->nextParams;
+								if (currentModbusParams == NULL)	
+								{
+										current_device = current_device->nextDevice;
+										currentModbusParams = current_device->params->gateway_read_modbus_command;
+										if(current_device == NULL)
+										{
+												sprintf(buf + strlen(buf) - 1, ""); 
+												return 1;
+										}
+								}												
+						}					
+				}
+				else if (current_device->protocol == DLT645_2007 || current_device->protocol == DLT645_97)
+				{
+						protocol_485=2;
+						uint8_t read_buf[10];
+						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);
+								
+			
+						}
+		
+								currentDLT645Params = currentDLT645Params->nextParams;		
+								if (currentDLT645Params == NULL)	
+								{
+										current_device = current_device->nextDevice;
+										currentDLT645Params = current_device->params->gateway_read_dlt645_command;
+										if(current_device == NULL)
+										{
+												sprintf(buf + strlen(buf) - 1, ""); 
+												return 1;
+										}
+								}										
+				}			
+				
+		}		
+		return 1;
+	
+}
 /*
 *********************************************************************************************************
 *	函 数 名:void WRITE_MODBUS_DATA(char* cJSONstring)
@@ -306,8 +465,8 @@ void find_difference(char* buf, char* pubJsonStringCopy, char* string)
     const char* delimiter = "{}";
     char* saveptr1;
     char* saveptr2;
-		char* data1 = malloc(strlen(buf) + 1);	
-		char* data2 = malloc(strlen(pubJsonStringCopy) + 1);	;
+		char* data1 = mymalloc(SRAMEX, strlen(buf));	
+		char* data2 = mymalloc(SRAMEX, strlen(pubJsonStringCopy));
 		
 		memcpy(data1, buf, strlen(buf));
 		memcpy(data2, pubJsonStringCopy, strlen(pubJsonStringCopy));
@@ -335,10 +494,12 @@ void find_difference(char* buf, char* pubJsonStringCopy, char* string)
 //        //sprintf(string + strlen(string),"{%s},", token2);
 //        token2 = strtok_r(NULL, delimiter, &saveptr2);
 //    }
-		free(data1);
-		free(data2);
+		myfree(SRAMEX, data1);
+		myfree(SRAMEX, data2);
 }
 
+
+
 /*
 *********************************************************************************************************
 *	函 数 名: void send_mqtt(char*buf, int jsonCunt)
@@ -347,63 +508,13 @@ void find_difference(char* buf, char* pubJsonStringCopy, char* string)
 *	返 回 值: 无
 *********************************************************************************************************
 */
-void send_mqtt(char*buf, int jsonCunt, char*pubJsonStringCopy){
+void send_mqtt(char*buf){
 		GATEWAY_PARAMS *get;
 		get= get_gateway_config_params();
-		if(get->device_params->protocol == MODBUS_READ)
-				send_modbus_data(get, buf, jsonCunt,pubJsonStringCopy);// modbus数据通道
-		else if(get->device_params->protocol == DLT645_2007 || get->device_params->protocol == DLT645_1997)
-				send_dlt645_data(get, buf, jsonCunt,pubJsonStringCopy);// DLT645数据通道
-		
-}
-/*
-*********************************************************************************************************
-*	函 数 名: void send_modbus_data(GATEWAY_PARAMS *get, char*buf, int jsonCunt, char*pubJsonStringCopy)
-*	功能说明: 发送modubs数据需求的通道
-*	形    参: 参数1:网关信息 参数2: 读取的信息 参数3: 第一次发送标志 参数4: 备份信息
-*	返 回 值: 无
-*********************************************************************************************************
-*/
 
-void send_modbus_data(GATEWAY_PARAMS *get, char*buf, int jsonCunt, char*pubJsonStringCopy){
-		time1 = GetCurrentTime();// 获取当前时间
-		if(jsonCunt || time2  <= time1 - (20 * 1000)) // 20s进行一次
-		{
-				memset(pubJsonStringCopy,0, strlen(pubJsonStringCopy));
-				memcpy(pubJsonStringCopy + strlen(pubJsonStringCopy), buf, strlen(buf));//备份上一次的数据
-				sprintf(pubJsonString,"{\"DEVICEID\":\"%s\",\"data\":[%s]",get->deviceId, buf);	// 组成要发送的json语句
-				int msg = MBOX_USER_PUBLISHQOS0;	
-				if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
-				time2 = GetCurrentTime();	// 获取当前时间		
-		}
-		else
-		{
-				if(strcmp(buf,pubJsonStringCopy)) // 比较两次数据是否不同
-				{
-						memset(string, 0 , strlen(string));
-						find_difference(buf, pubJsonStringCopy, string);// 比较两次采集的数据不同之处
-						memset(pubJsonString,0, strlen(pubJsonString));
-						sprintf(pubJsonString,"{\"DEVICEID\":\"%s\",\"data\":[{%s}]}",get->deviceId, string);// 组成要发送的json语句
-						memset(pubJsonStringCopy,0, strlen(pubJsonStringCopy));
-						sprintf(pubJsonStringCopy, buf, strlen(buf));// 备份当前数据		
-						int msg = MBOX_USER_PUBLISHQOS0;	
-						if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);	
-				}
-		}	
+		sprintf(pubJsonString,"{\"DEVICEID\":\"%s\",\"data\":[%s]}",get->deviceId, buf);	// 组成要发送的json语句							
 	
-}
-/*
-*********************************************************************************************************
-*	函 数 名: void send_dlt645_data(GATEWAY_PARAMS *get, char*buf, int jsonCunt, char*pubJsonStringCopy)
-*	功能说明: 发送modubs数据需求的通道
-*	形    参: 参数1:网关信息 参数2: 读取的信息 参数3: 第一次发送标志 参数4: 备份信息
-*	返 回 值: 无
-*********************************************************************************************************
-*/
-void send_dlt645_data(GATEWAY_PARAMS *get, char*buf, int jsonCunt, char*pubJsonStringCopy){
-		memset(pubJsonStringCopy,0, strlen(pubJsonStringCopy));
-		memcpy(pubJsonStringCopy + strlen(pubJsonStringCopy), buf, strlen(buf));//备份上一次的数据
-		sprintf(pubJsonString,"{\"DEVICEID\":\"%s\",\"data\":[%s]",get->deviceId, buf);	// 组成要发送的json语句
 		int msg = MBOX_USER_PUBLISHQOS0;	
 		if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
-}
+
+}

+ 13 - 3
app/USER/main.c

@@ -17,6 +17,8 @@
 #include "task.h"
 #include "sys_sx1278.h"
 #include "node_data_acquisition.h"
+#include "log.h"
+#include "tcp_server.h"
 
 
 #define UNIQUE_ID 0x1fff7a10
@@ -33,7 +35,7 @@
 *  6: 用于 MQTT数据发送线程  APP_TASK_MQTTMAIN_PRIO
 *  7: 用于 MQTT数据接收线程  APP_TASK_MQTTRECEIVE_PRIO
 *  8: 用于 sx1278发送/接收处理数据	DATA_TASK_PRIO
-*  9: 用于 本机状态led显示      
+*  9: 用于 本机状态led显示      / 现用于tcpServer_init   TCP_TASK_PRIO /
 * 10:用于网络状态led显示
 * 11:用于轮询读出本机保存信息发送到sx1278发送接收线程
 * 12:用于管理所有线程
@@ -72,7 +74,7 @@ int main(void)
 {
 	int status;
 	load_unique();
-	sprintf(gatewayId,"DT8pd3ac6h");//DT8pd3ac6h  DTbma5ac6h  DTtest0001
+	sprintf(gatewayId,"DTtest0001");//DT8pd3ac6h  DTbma5ac6h  DTtest0001
 	NVIC_Configuration();
 	my_mem_init(SRAMEX);
 	my_mem_init(SRAMIN);
@@ -114,9 +116,17 @@ void period_taskFuntcion(void *arg)
 #if OS_TASK_STAT_EN > 0u
   OSStatInit();
 #endif
-	
+
+	int time1,time2;
+	time1 = OSTimeGet();
 	lwIP_Init();
 
+	// 输出日志
+//	log_init();
+//	tcp_server_init();
+	
+//	LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "system start");
+	time2 = OSTimeGet() - time1;
 	http_getDemo();
 	http_postDemo();