|
@@ -18,15 +18,20 @@
|
|
|
#include "gateway_message.h"
|
|
|
#include "MQTTClient.h"
|
|
|
#include "cJSON.h"
|
|
|
+#include "time_count.h"
|
|
|
+
|
|
|
|
|
|
void master_task(uint8_t *string,uint16_t stringlength);
|
|
|
void slave_task();
|
|
|
-int READ_MODBUS_DATA(DEVICE_PARAMS *current_device);
|
|
|
-
|
|
|
+int READ_MODBUS_DATA(DEVICE_PARAMS *current_device,char* buf);
|
|
|
+///////////////////////////////////////////////////////////////////////////////////////////
|
|
|
+char string[512];
|
|
|
+void findDifference(char* data1, char* data2, char* string) ;
|
|
|
+int jsoncunt = 1;
|
|
|
|
|
|
+////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
uint16_t BufferSize;
|
|
|
uint8_t Buffer[256];
|
|
|
-
|
|
|
uint32_t rx_num = 0;
|
|
|
|
|
|
uint8_t PingMsg[] = "PING\0";
|
|
@@ -34,10 +39,10 @@ uint8_t PongMsg[] = "PONG\0";
|
|
|
tRadioDriver *Radio=NULL;
|
|
|
/*
|
|
|
*********************************************************************************************************
|
|
|
-* 锟斤拷 锟斤拷 锟斤拷: void data_task(void *pdata)
|
|
|
-* 锟斤拷锟斤拷说锟斤拷: 锟斤拷要锟斤拷data_task锟斤拷锟斤拷锟竭程o拷锟斤拷锟饺硷拷锟竭★拷锟斤拷锟斤拷锟斤拷锟竭硷拷锟角斤拷nandflash锟叫碉拷锟斤拷锟捷斤拷锟斤拷锟斤拷锟斤拷锟斤拷询锟斤拷锟斤拷锟斤拷锟斤拷
|
|
|
-* 锟斤拷 锟轿o拷锟斤拷
|
|
|
-* 锟斤拷 锟斤拷 值: 锟斤拷
|
|
|
+* 函 数 名: void data_task(void *pdata)
|
|
|
+* 功能说明: 主要是data_task处理线程,优先级高。其运行逻辑是将nandflash中的数据解析出来轮询发送数据
|
|
|
+* 形 参:无
|
|
|
+* 返 回 值: 无
|
|
|
*********************************************************************************************************
|
|
|
*/
|
|
|
|
|
@@ -48,47 +53,76 @@ void data_task(void *pdata)
|
|
|
// 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;
|
|
|
+
|
|
|
+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);
|
|
|
+// 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);
|
|
|
+
|
|
|
+ while (current_device!=NULL)
|
|
|
+ {
|
|
|
+ char buf[1000]; // 接收modbus读取的数据
|
|
|
+ READ_MODBUS_DATA(current_device, buf);
|
|
|
if(current_device->nextDevice!=NULL)
|
|
|
{
|
|
|
- current_device=current_device->nextDevice;
|
|
|
+ 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;
|
|
|
+ {
|
|
|
+ sprintf(buf + strlen(buf) - 1, "}");
|
|
|
+ time1 = OSTimeGet();
|
|
|
+ if( jsoncunt || time2 <= time1 - 10000)
|
|
|
+ {
|
|
|
+ memset(pubJsonStringCopy,0, strlen(pubJsonStringCopy));
|
|
|
+ memcpy(pubJsonStringCopy + strlen(pubJsonStringCopy), buf, strlen(buf));//备份上一次的数据
|
|
|
+ sprintf(pubJsonString,"{\"DEVICEID\":\"%s\",\"data\":[%s]",get->deviceId, buf);
|
|
|
+ int msg = MBOX_USER_PUBLISHQOS2;
|
|
|
+ if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
|
|
|
+ current_device=get->device_params;
|
|
|
+ jsoncunt = 0;
|
|
|
+ time2 = OSTimeGet();
|
|
|
+ }//10s进行一次
|
|
|
+
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if(strcmp(buf,pubJsonStringCopy))
|
|
|
+ {
|
|
|
+ memset(string, 0 , strlen(string));
|
|
|
+ findDifference(buf, pubJsonStringCopy, string);
|
|
|
+ memset(pubJsonString,0, strlen(pubJsonString));
|
|
|
+ sprintf(pubJsonString,"{\"deviceId\":\"%s\",\"data\":[{%s}]}",get->deviceId, string);
|
|
|
+ memset(pubJsonStringCopy,0, strlen(pubJsonStringCopy));
|
|
|
+ sprintf(pubJsonStringCopy, buf, strlen(buf));
|
|
|
+
|
|
|
+ int msg = MBOX_USER_PUBLISHQOS2;
|
|
|
+ if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ memset(buf,0,strlen(buf));
|
|
|
+ current_device=get->device_params;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- #if 0 //锟斤拷锟皆达拷锟诫不锟斤拷锟斤拷转锟斤拷直锟接斤拷锟斤拷锟斤拷应锟侥斤拷锟斤拷
|
|
|
+ #if 0 //测试代码不经过转发直接进行相应的解析
|
|
|
SlaveProtocolAnalysis(string,bufferLength);
|
|
|
data_acquisition();
|
|
|
uint8_t node_string[256];
|
|
@@ -99,16 +133,18 @@ void data_task(void *pdata)
|
|
|
|
|
|
/*
|
|
|
*********************************************************************************************************
|
|
|
-* 锟斤拷 锟斤拷 锟斤拷: int READ_MODBUS_DATA(DEVICE_PARAMS *device)
|
|
|
-* 锟斤拷锟斤拷说锟斤拷: 锟斤拷取锟斤拷前锟节碉拷锟较碉拷modbus锟斤拷锟斤拷
|
|
|
-* 锟斤拷 锟轿o拷DEVICE_PARAMS *device 锟斤拷前锟借备
|
|
|
-* 锟斤拷 锟斤拷 值: 1 锟斤拷锟缴癸拷 0锟斤拷失锟斤拷
|
|
|
+* 函 数 名: int READ_MODBUS_DATA(DEVICE_PARAMS *device)
|
|
|
+* 功能说明: 读取当前节点上的modbus数据
|
|
|
+* 形 参:DEVICE_PARAMS *device 当前设备
|
|
|
+* 返 回 值: 1 :成功 0:失败
|
|
|
*********************************************************************************************************
|
|
|
*/
|
|
|
|
|
|
-int READ_MODBUS_DATA(DEVICE_PARAMS *device)
|
|
|
+int READ_MODBUS_DATA(DEVICE_PARAMS *device, char* buf)
|
|
|
{
|
|
|
DEVICE_PARAMS *current_device=device;
|
|
|
+ GATEWAY_PARAMS* get;
|
|
|
+ get = get_gateway_config_params();
|
|
|
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)
|
|
@@ -116,7 +152,7 @@ int READ_MODBUS_DATA(DEVICE_PARAMS *device)
|
|
|
if (current_device->protocol == MODBUS_READ)
|
|
|
{
|
|
|
protocol_485=1;
|
|
|
- uint16_t data[currentModbusParams->registerByteNum /2]; // modbus锟侥达拷锟斤拷锟斤拷锟斤拷
|
|
|
+ uint16_t data[currentModbusParams->registerByteNum /2]; // modbus寄存器长度
|
|
|
mmodbus_set16bitOrder(current_device->MDBbigLittleFormat);
|
|
|
if (currentModbusParams->functionCode == 0x03 | currentModbusParams->functionCode == 0x01)
|
|
|
{
|
|
@@ -147,80 +183,27 @@ int READ_MODBUS_DATA(DEVICE_PARAMS *device)
|
|
|
float convertedValue = (float)value / pow(10, currentModbusParams->decimalPoint);
|
|
|
memcpy(currentModbusParams->value, &convertedValue, 4);
|
|
|
}
|
|
|
- sprintf(pubJsonString + strlen(pubJsonString), "{\"data\":[{\"deviceId\":\"%s\",\"%s\":%d}]},",
|
|
|
+ sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":%d},",
|
|
|
current_device->deviceID, currentModbusParams->keyword, value);
|
|
|
currentModbusParams = currentModbusParams->nextParams;
|
|
|
if (currentModbusParams == NULL)
|
|
|
- {
|
|
|
- sprintf(pubJsonString + strlen(pubJsonString) - 1, "");
|
|
|
- return 0;
|
|
|
- }
|
|
|
+ {
|
|
|
+ sprintf(buf + strlen(buf) - 1, "");
|
|
|
+ return 0;
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- else if (current_device->protocol == MODBUS_WRITE)
|
|
|
- {
|
|
|
- protocol_485=1;
|
|
|
- }
|
|
|
- else if (current_device->protocol == DLT645_07 || current_device->protocol == DLT645_97)
|
|
|
- {
|
|
|
- protocol_485=2;
|
|
|
- uint8_t read_buf[10];
|
|
|
-
|
|
|
- currentDLT645Params->rxLen = 0;
|
|
|
- memset(read_buf, 0, 10);
|
|
|
- memset(currentDLT645Params->data, 0, 10);
|
|
|
-
|
|
|
- dlt645_set_addr(&dlt645, current_device->params->gateway_read_dlt645_command->deviceID645);
|
|
|
- int8_t rs;
|
|
|
- if (current_device->protocol == DLT645_07)
|
|
|
- {
|
|
|
- rs = dlt645_read_data(&dlt645, currentDLT645Params->Identification, read_buf, DLT645_2007);
|
|
|
- }
|
|
|
- else if (current_device->protocol == DLT645_97)
|
|
|
- {
|
|
|
- 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;
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- currentDLT645Params->rxLen =0;
|
|
|
- }
|
|
|
- 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 WRITE_MODBUS_DATA(char* cJSONstring)
|
|
|
-* 锟斤拷锟斤拷说锟斤拷: 锟斤拷锟斤拷mqtt锟斤拷锟捷诧拷写锟斤拷modbus锟侥达拷锟斤拷
|
|
|
-* 锟斤拷 锟轿o拷char* cJSONstring mqtt锟斤拷锟秸碉拷锟斤拷锟斤拷锟斤拷
|
|
|
-* 锟斤拷 锟斤拷 值: 锟斤拷
|
|
|
+* 函 数 名:void WRITE_MODBUS_DATA(char* cJSONstring)
|
|
|
+* 功能说明: 接收mqtt数据并写入modbus寄存器
|
|
|
+* 形 参:char* cJSONstring mqtt接收到的数据
|
|
|
+* 返 回 值: 无
|
|
|
*********************************************************************************************************
|
|
|
*/
|
|
|
void WRITE_MODBUS_DATA(char* cJSONstring)
|
|
@@ -246,7 +229,7 @@ void WRITE_MODBUS_DATA(char* cJSONstring)
|
|
|
GATEWAY_WRITE_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_write_modbus_command;
|
|
|
if(!strcmp(device_ID,deviceId))
|
|
|
{
|
|
|
- OSTimeDlyHMSM(0, 0, 0, 60);
|
|
|
+ OSTimeDlyHMSM(0, 0, 0, 70);
|
|
|
OSIntEnter();
|
|
|
if(power)
|
|
|
{
|
|
@@ -262,7 +245,6 @@ void WRITE_MODBUS_DATA(char* cJSONstring)
|
|
|
number =currentModbusParams->registerAddress;
|
|
|
data = temp->valueint;
|
|
|
mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
|
|
|
-
|
|
|
}
|
|
|
if(mode)
|
|
|
{
|
|
@@ -271,7 +253,6 @@ void WRITE_MODBUS_DATA(char* cJSONstring)
|
|
|
number =currentModbusParams->registerAddress;
|
|
|
data = mode->valueint;
|
|
|
mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
|
|
|
-
|
|
|
}
|
|
|
if(fan)
|
|
|
{
|
|
@@ -280,14 +261,49 @@ void WRITE_MODBUS_DATA(char* cJSONstring)
|
|
|
number =currentModbusParams->registerAddress;
|
|
|
data = fan->valueint;
|
|
|
mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
|
|
|
-
|
|
|
}
|
|
|
OSIntExit();
|
|
|
}
|
|
|
current_device = current_device->nextDevice;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
cJSON_Delete(root);
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// 比较两个 JSONItem 数组,找到变化的部分并打印
|
|
|
+void findDifference(char* buf, char* pubJsonStringCopy, char* string)
|
|
|
+{
|
|
|
+ const char* delimiter = "{}";
|
|
|
+ char* saveptr1;
|
|
|
+ char* saveptr2;
|
|
|
+ char* data1 = malloc(strlen(buf) + 1);
|
|
|
+ char* data2 = malloc(strlen(pubJsonStringCopy) + 1); ;
|
|
|
+ memcpy(data1, buf, strlen(buf));
|
|
|
+ memcpy(data2, pubJsonStringCopy, strlen(pubJsonStringCopy));
|
|
|
+ // 利用strtok_r函数分割字符串,并逐一比较
|
|
|
+ char* token1 = strtok_r((char*)data1, delimiter, &saveptr1);
|
|
|
+ char* token2 = strtok_r((char*)data2, delimiter, &saveptr2);
|
|
|
+
|
|
|
+ while (token1 != NULL && token2 != NULL) {
|
|
|
+ if (strcmp(token1, token2) != 0) {
|
|
|
+ memcpy(string + strlen(string), token1, strlen(token1));
|
|
|
+ }
|
|
|
+ token1 = strtok_r(NULL, delimiter, &saveptr1);
|
|
|
+ token2 = strtok_r(NULL, delimiter, &saveptr2);
|
|
|
+ }
|
|
|
+
|
|
|
+// // 如果有剩余字符串未比较,则打印剩余字符串
|
|
|
+// while (token1 != NULL) {
|
|
|
+// sprintf(string + strlen(string),"%s,", token1);
|
|
|
+// token1 = strtok_r(NULL, delimiter, &saveptr1);
|
|
|
+// }
|
|
|
+
|
|
|
+// while (token2 != NULL) {
|
|
|
+// //sprintf(string + strlen(string),"{%s},", token2);
|
|
|
+// token2 = strtok_r(NULL, delimiter, &saveptr2);
|
|
|
+// }
|
|
|
+ free(data1);
|
|
|
+ free(data2);
|
|
|
+}
|
|
|
+
|