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