|
@@ -35,7 +35,7 @@ void data_task(void *pdata)
|
|
OS_CPU_SR cpu_sr;
|
|
OS_CPU_SR cpu_sr;
|
|
pdata = pdata;
|
|
pdata = pdata;
|
|
dlt645_init(100);
|
|
dlt645_init(100);
|
|
- //mmodbus_init(1);
|
|
|
|
|
|
+ mmodbus_init(1);
|
|
|
|
|
|
int jsonCunt = 1;
|
|
int jsonCunt = 1;
|
|
char *device_config_json = mymalloc(SRAMEX, 9 * 1024);
|
|
char *device_config_json = mymalloc(SRAMEX, 9 * 1024);
|
|
@@ -48,17 +48,19 @@ void data_task(void *pdata)
|
|
DEVICE_PARAMS *current_device=get->device_params;
|
|
DEVICE_PARAMS *current_device=get->device_params;
|
|
// Config_485_Port(get->baudrate, get->dataBits, get->stopBit, get->parity, get->flowControl);
|
|
// Config_485_Port(get->baudrate, get->dataBits, get->stopBit, get->parity, get->flowControl);
|
|
char *buf = mymalloc(SRAMEX, 9 * 1024); // 接收读取的数据
|
|
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)
|
|
while (current_device!=NULL)
|
|
{
|
|
{
|
|
read_device_data(current_device, buf); //读取数据
|
|
read_device_data(current_device, buf); //读取数据
|
|
- send_mqtt(buf, jsonCunt); //发送数据
|
|
|
|
|
|
+ send_mqtt(buf, jsonCunt,pubJsonStringCopy); //发送数据
|
|
jsonCunt = 0;
|
|
jsonCunt = 0;
|
|
memset(buf,0,strlen(buf));
|
|
memset(buf,0,strlen(buf));
|
|
current_device=get->device_params;
|
|
current_device=get->device_params;
|
|
OSTimeDly(1);
|
|
OSTimeDly(1);
|
|
}
|
|
}
|
|
myfree(SRAMEX, buf);
|
|
myfree(SRAMEX, buf);
|
|
|
|
+ myfree(SRAMEX,pubJsonStringCopy);
|
|
}
|
|
}
|
|
/*
|
|
/*
|
|
*********************************************************************************************************
|
|
*********************************************************************************************************
|
|
@@ -134,11 +136,11 @@ int read_device_data(DEVICE_PARAMS *device, char* buf)
|
|
sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":%d},",
|
|
sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":%d},",
|
|
current_device->deviceID, currentModbusParams->keyword, value);
|
|
current_device->deviceID, currentModbusParams->keyword, value);
|
|
}
|
|
}
|
|
- else
|
|
|
|
- {
|
|
|
|
- printf("read modbus register fail\n");
|
|
|
|
- return 0;
|
|
|
|
- }
|
|
|
|
|
|
+// else
|
|
|
|
+// {
|
|
|
|
+// printf("read modbus register fail\n");
|
|
|
|
+// return 0;
|
|
|
|
+// }
|
|
|
|
|
|
/* 每读完一个寄存器,进行message判断 */
|
|
/* 每读完一个寄存器,进行message判断 */
|
|
mqtt_to_device();
|
|
mqtt_to_device();
|
|
@@ -156,10 +158,11 @@ int read_device_data(DEVICE_PARAMS *device, char* buf)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- if (current_device->protocol == DLT645_2007 || current_device->protocol == DLT645_97)
|
|
|
|
|
|
+ else if (current_device->protocol == DLT645_2007 || current_device->protocol == DLT645_97)
|
|
{
|
|
{
|
|
protocol_485=2;
|
|
protocol_485=2;
|
|
uint8_t read_buf[10];
|
|
uint8_t read_buf[10];
|
|
|
|
+ uint32_t dltValue;
|
|
|
|
|
|
currentDLT645Params->rxLen = 0;
|
|
currentDLT645Params->rxLen = 0;
|
|
memset(read_buf, 0, 10);
|
|
memset(read_buf, 0, 10);
|
|
@@ -192,11 +195,15 @@ int read_device_data(DEVICE_PARAMS *device, char* buf)
|
|
memcpy(currentDLT645Params->data, read_buf, 9);
|
|
memcpy(currentDLT645Params->data, read_buf, 9);
|
|
currentDLT645Params->rxLen = 9;
|
|
currentDLT645Params->rxLen = 9;
|
|
}
|
|
}
|
|
- sprintf(buf + strlen(buf), "{\"identifier\":\"%s\",\"deviceID645\":\"%02x%02x%02x%02x%02x%02x\",\"identifier645\":%d}",
|
|
|
|
|
|
+
|
|
|
|
+ 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->keyword, currentDLT645Params->deviceID645[0],
|
|
currentDLT645Params->deviceID645[1],currentDLT645Params->deviceID645[2],
|
|
currentDLT645Params->deviceID645[1],currentDLT645Params->deviceID645[2],
|
|
currentDLT645Params->deviceID645[3],currentDLT645Params->deviceID645[4],
|
|
currentDLT645Params->deviceID645[3],currentDLT645Params->deviceID645[4],
|
|
- currentDLT645Params->deviceID645[5],currentDLT645Params->Identification);
|
|
|
|
|
|
+ currentDLT645Params->deviceID645[5],currentDLT645Params->Identification,dltValue);
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|
|
@@ -204,10 +211,7 @@ int read_device_data(DEVICE_PARAMS *device, char* buf)
|
|
// {
|
|
// {
|
|
// currentDLT645Params->rxLen = 0;
|
|
// currentDLT645Params->rxLen = 0;
|
|
// printf("read DLT current data fail\n");
|
|
// printf("read DLT current data fail\n");
|
|
-// }
|
|
|
|
- /* 每读完一个寄存器,进行message判断 */
|
|
|
|
- mqtt_to_device();
|
|
|
|
-
|
|
|
|
|
|
+// }
|
|
currentDLT645Params = currentDLT645Params->nextParams;
|
|
currentDLT645Params = currentDLT645Params->nextParams;
|
|
if (currentDLT645Params == NULL)
|
|
if (currentDLT645Params == NULL)
|
|
{
|
|
{
|
|
@@ -233,15 +237,11 @@ int read_device_data(DEVICE_PARAMS *device, char* buf)
|
|
*********************************************************************************************************
|
|
*********************************************************************************************************
|
|
*/
|
|
*/
|
|
void write_modbus_data(char* cJSONstring)
|
|
void write_modbus_data(char* cJSONstring)
|
|
-{
|
|
|
|
- uint16_t data;
|
|
|
|
- uint16_t number;
|
|
|
|
- uint16_t slaveAddress;
|
|
|
|
-
|
|
|
|
|
|
+{
|
|
GATEWAY_PARAMS* get;
|
|
GATEWAY_PARAMS* get;
|
|
get = get_gateway_config_params();
|
|
get = get_gateway_config_params();
|
|
DEVICE_PARAMS* current_device = get->device_params;
|
|
DEVICE_PARAMS* current_device = get->device_params;
|
|
-
|
|
|
|
|
|
+ /* 利用cJSOn_Parse解析数据,获取各类型数据 */
|
|
cJSON *root = cJSON_Parse(cJSONstring);
|
|
cJSON *root = cJSON_Parse(cJSONstring);
|
|
const char *deviceId = cJSON_GetStringValue(cJSON_GetObjectItem(root, "deviceId"));
|
|
const char *deviceId = cJSON_GetStringValue(cJSON_GetObjectItem(root, "deviceId"));
|
|
const cJSON *power = cJSON_GetObjectItemCaseSensitive(root, "power");
|
|
const cJSON *power = cJSON_GetObjectItemCaseSensitive(root, "power");
|
|
@@ -253,9 +253,10 @@ void write_modbus_data(char* cJSONstring)
|
|
{
|
|
{
|
|
char* device_ID = (char*)current_device->deviceID;
|
|
char* device_ID = (char*)current_device->deviceID;
|
|
GATEWAY_WRITE_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_write_modbus_command;
|
|
GATEWAY_WRITE_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_write_modbus_command;
|
|
- if(!strcmp(device_ID,deviceId))
|
|
|
|
|
|
+ if(!strcmp(device_ID,deviceId)) //匹配ID
|
|
{
|
|
{
|
|
OSTimeDly(100);
|
|
OSTimeDly(100);
|
|
|
|
+ /* 写入寄存器操作 */
|
|
if(power)
|
|
if(power)
|
|
{
|
|
{
|
|
mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress,
|
|
mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress,
|
|
@@ -346,34 +347,63 @@ void find_difference(char* buf, char* pubJsonStringCopy, char* string)
|
|
* 返 回 值: 无
|
|
* 返 回 值: 无
|
|
*********************************************************************************************************
|
|
*********************************************************************************************************
|
|
*/
|
|
*/
|
|
-void send_mqtt(char*buf, int jsonCunt){
|
|
|
|
|
|
+void send_mqtt(char*buf, int jsonCunt, char*pubJsonStringCopy){
|
|
GATEWAY_PARAMS *get;
|
|
GATEWAY_PARAMS *get;
|
|
get= get_gateway_config_params();
|
|
get= get_gateway_config_params();
|
|
- time1 = GetCurrentTime();
|
|
|
|
|
|
+ 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数据通道
|
|
|
|
|
|
- if(jsonCunt || time2 <= time1 - (3 * 1000)) // 20s进行一次
|
|
|
|
|
|
+}
|
|
|
|
+/*
|
|
|
|
+*********************************************************************************************************
|
|
|
|
+* 函 数 名: 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));
|
|
memset(pubJsonStringCopy,0, strlen(pubJsonStringCopy));
|
|
memcpy(pubJsonStringCopy + strlen(pubJsonStringCopy), buf, strlen(buf));//备份上一次的数据
|
|
memcpy(pubJsonStringCopy + strlen(pubJsonStringCopy), buf, strlen(buf));//备份上一次的数据
|
|
- sprintf(pubJsonString,"{\"DEVICEID\":\"%s\",\"data\":[%s]",get->deviceId, buf);
|
|
|
|
-
|
|
|
|
|
|
+ sprintf(pubJsonString,"{\"DEVICEID\":\"%s\",\"data\":[%s]",get->deviceId, buf); // 组成要发送的json语句
|
|
int msg = MBOX_USER_PUBLISHQOS0;
|
|
int msg = MBOX_USER_PUBLISHQOS0;
|
|
if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
|
|
if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
|
|
- time2 = GetCurrentTime();
|
|
|
|
|
|
+ time2 = GetCurrentTime(); // 获取当前时间
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- if(strcmp(buf,pubJsonStringCopy))
|
|
|
|
|
|
+ if(strcmp(buf,pubJsonStringCopy)) // 比较两次数据是否不同
|
|
{
|
|
{
|
|
memset(string, 0 , strlen(string));
|
|
memset(string, 0 , strlen(string));
|
|
- find_difference(buf, pubJsonStringCopy, string);
|
|
|
|
|
|
+ find_difference(buf, pubJsonStringCopy, string);// 比较两次采集的数据不同之处
|
|
memset(pubJsonString,0, strlen(pubJsonString));
|
|
memset(pubJsonString,0, strlen(pubJsonString));
|
|
- sprintf(pubJsonString,"{\"deviceId\":\"%s\",\"data\":[{%s}]}",get->deviceId, string);
|
|
|
|
|
|
+ sprintf(pubJsonString,"{\"DEVICEID\":\"%s\",\"data\":[{%s}]}",get->deviceId, string);// 组成要发送的json语句
|
|
memset(pubJsonStringCopy,0, strlen(pubJsonStringCopy));
|
|
memset(pubJsonStringCopy,0, strlen(pubJsonStringCopy));
|
|
- sprintf(pubJsonStringCopy, buf, strlen(buf));
|
|
|
|
-
|
|
|
|
|
|
+ sprintf(pubJsonStringCopy, buf, strlen(buf));// 备份当前数据
|
|
int msg = MBOX_USER_PUBLISHQOS0;
|
|
int msg = MBOX_USER_PUBLISHQOS0;
|
|
if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
|
|
if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
+/*
|
|
|
|
+*********************************************************************************************************
|
|
|
|
+* 函 数 名: 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);
|
|
|
|
+}
|