EdgeGateway_FSU/DevicePortGet/c.bk

1503 lines
54 KiB
Plaintext
Raw Normal View History

2024-03-15 17:25:04 +08:00
#if 0
char *ptr =NULL;
char str[]="1&2&100&12&200";
int tempil;
int Eflag;
//char str[]="12";
// ptr = strchr(str, '&');
// if (NULL != ptr) {
// printf("Character: %s\n", ptr);
// printf("Character: %c\n", *ptr);
// printf("Offset: %ld\n", ptr - str);
// }
// ptr++;
// ptr = strchr(ptr, '&');
// if (NULL != ptr) {
// printf("Character: %s\n", ptr);
// printf("Character: %c\n", *ptr);
// printf("Offset: %ld\n", ptr - str);
// }
DebugPrint("Str:%s\n",str);
Eflag=eSDAddFind_Str_Notzero;
for(tempil=0;tempil<32;tempil++)
{
ptr=strcpynsubstr(str,'&',tempil,&Eflag);
if (NULL != ptr)
{
DebugPrint("Ch%d: %s %d\n",tempil, ptr,Eflag);
//DebugPrint("Ch%d: %c\n",tempil, *ptr);
if(Eflag==eSDAddFind_Str_End)
break;
}
else
{
break;
}
}
return 0;
#endif
#define BROKER_ADDRESS "116.236.50.106:8764" //汇珏网络测试
#define CLIENTID "hjcid-01" //客户端id
#define USERNAME "hjemq-c1" //用户名
#define PASSWORD "Hj57471000" //密码
#if 0
MQTTClient client;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
MQTTClient_willOptions will_opts = MQTTClient_willOptions_initializer;
MQTTClient_message pubmsg = MQTTClient_message_initializer;
int rc;
int Temp_Counter;
/* 创建mqtt客户端对象 */
if (MQTTCLIENT_SUCCESS !=
(rc = MQTTClient_create(&client, BROKER_ADDRESS, CLIENTID,
MQTTCLIENT_PERSISTENCE_NONE, NULL))) {
printf("Failed to create client, return code %d\n", rc);
rc = EXIT_FAILURE;
//goto exit;
}
#endif
void analysisJsonPrint(cJSON *json) {
if (NULL == json) {
return;
}
cJSON *json_name = cJSON_GetObjectItem(json, "name");
printJsonObjvalue(json_name);
cJSON *json_age = cJSON_GetObjectItem(json, "age");
printJsonObjvalue(json_age);
cJSON *json_professional = cJSON_GetObjectItem(json, "professional");
cJSON *json_professional_child = json_professional->child;
while (json_professional_child != NULL) {
printJsonObjvalue(json_professional_child);
json_professional_child = json_professional_child->next;
}
cJSON *json_languages = cJSON_GetObjectItem(json, "languages");
if (NULL == json_languages) {
int size = cJSON_GetArraySize(json_languages);
printf("%s size:%d\n", json_languages->string, size);
int i;
for(i=0;i<size;i++) {
cJSON *json_languages_child = cJSON_GetArrayItem(json_languages, i);
printJsonObjvalue(json_languages_child);
}
}
cJSON *json_phone = cJSON_GetObjectItem(json, "phone");
cJSON *json_phone_number = cJSON_GetObjectItem(json_phone,"number");
cJSON *json_phone_type = cJSON_GetObjectItem(json_phone,"type");
printJsonObjvalue(json_phone_number);
printJsonObjvalue(json_phone_type);
cJSON *json_courses = cJSON_GetObjectItem(json, "courses");
if (json_courses) {
int size = cJSON_GetArraySize(json_courses);
printf("%s size:%d\n", json_courses->string, size);
int i;
for(i=0;i<size;i++) {
cJSON *arrayItem = cJSON_GetArrayItem(json_courses, i);
if (NULL != arrayItem) {
cJSON *json_course_name = cJSON_GetObjectItem(arrayItem,"name");
cJSON *json_course_price = cJSON_GetObjectItem(arrayItem,"price");
printJsonObjvalue(json_course_name);
printJsonObjvalue(json_course_price);
}
}
}
cJSON *json_vip = cJSON_GetObjectItem(json, "vip");
printJsonObjvalue(json_vip);
cJSON *json_address = cJSON_GetObjectItem(json, "address");
printJsonObjvalue(json_address);
}
//读取JSON
cJSON* readJsonFile(char *fileName) {
if (NULL == fileName) {
return NULL;
}
FILE *fp = NULL;
cJSON *json = NULL;
char line[1024] = {0};
char *data = NULL;
//打开一个文本文件,文件必须存在,只允许读
fp = fopen(fileName, "r");
if (NULL != fp) {
//seek末尾
fseek(fp, 0, SEEK_END);
//读取文件大小
long len = ftell(fp);
//seek起始位值
fseek(fp, 0, SEEK_SET);
data = (char*)malloc(len + 1);
fread(data, 1, len, fp);
fclose(fp);
}
printf("readJsonFile data:%s\n", data);
cJSON *json_root = cJSON_Parse(data);
if (NULL == json_root) {
printf("cJSON_Parse error:%s\n", cJSON_GetErrorPtr());
}
if (NULL != data) {
free(data);
}
return json_root;
}
//解析JSON
void analysisJsonObj(cJSON *json) {
if (NULL == json) {
return;
}
//解析name
cJSON *json_name = cJSON_GetObjectItem(json, "name");
printf("root[%s:%s]\n", json_name->string, json_name->valuestring);
//解析age
cJSON *json_age = cJSON_GetObjectItem(json, "age");
printf("root[%s:%d]\n", json_age->string, json_age->valueint);
//解析professional, 通过调用child和next遍历解析
cJSON *json_professional = cJSON_GetObjectItem(json, "professional");
cJSON *json_professional_child = json_professional->child;
while (json_professional_child != NULL) {
printf("%s[%s:%d]\n", json_professional->string, json_professional_child->string, json_professional_child->valueint);
json_professional_child = json_professional_child->next;
}
//解析languages, 注意languages是一个数组
cJSON *json_languages = cJSON_GetObjectItem(json, "languages");
if (json_languages) {
int size = cJSON_GetArraySize(json_languages);
printf("%s size:%d\n", json_languages->string, size);
int i;
for(i=0;i<size;i++) {
cJSON *json_languages_child =cJSON_GetArrayItem(json_languages, i);
printf("%s[%d:%s]\n", json_languages->string, i, json_languages_child->valuestring);
}
}
//解析phone. 另一种方式解析, 直接通过名字获取
cJSON *json_phone = cJSON_GetObjectItem(json, "phone");
cJSON *json_phone_number = cJSON_GetObjectItem(json_phone,"number");
cJSON *json_phone_type = cJSON_GetObjectItem(json_phone,"type");
printf("%s[%s:%s]\n", json_phone->string, json_phone_number->string, json_phone_number->valuestring);
printf("%s[%s:%s]\n", json_phone->string, json_phone_type->string, json_phone_type->valuestring);
//解析courses
cJSON *json_courses = cJSON_GetObjectItem(json, "courses");
if (json_courses) {
int size = cJSON_GetArraySize(json_courses);
printf("%s size:%d\n", json_courses->string, size);
int i;
for(i=0;i<size;i++) {
cJSON *json_array = cJSON_GetArrayItem(json_courses, i);
if (json_array) {
cJSON *json_course_name = cJSON_GetObjectItem(json_array,"name");
cJSON *json_course_price = cJSON_GetObjectItem(json_array,"price");
printf("%s[%s:%.1lf]\n", json_courses->string, json_course_name->string, json_course_name->valuedouble);
printf("%s[%s:%.1lf]\n", json_courses->string, json_course_price->string, json_course_price->valuedouble);
}
}
}
//解析vip, vip是布尔值, 有两种方式解析获取
cJSON *json_vip = cJSON_GetObjectItem(json, "vip");
/*
if (json_vip->type == cJSON_True) {
printf("root[%s:true]\n", json_vip->string);
} else if (json_vip->type == cJSON_False) {
printf("root[%s:false]\n", json_vip->string);
} else {
printf("root[%s:false]\n", json_vip->string);
}
*/
cJSON_bool bvip = cJSON_IsBool(json_vip);
if (bvip == cJSON_True) {//cJSON_False
printf("root[%s:true]\n", json_vip->string);
} else {
printf("root[%s:false]\n", json_vip->string);
}
//解析address
cJSON *json_address = cJSON_GetObjectItem(json, "address");
if (json_address->type ==cJSON_NULL) {
printf("root[%s:null]\n", json_address->string);
}
}
//获取cJSON对象的类型, 并同时打印值
static void printJsonObjvalue(const cJSON *json) {
if (NULL == json) {
printf("NULL object!\n");
return;
}
switch (json->type) {
case cJSON_False:
printf("%s: false\n", json->string);
break;
case cJSON_True:
printf("%s: true\n", json->string);
break;
case cJSON_NULL:
printf("%s: cJSON_NULL\n", json->string);
break;
case cJSON_Number:
printf("%s: %d, %f\n", json->string, json->valueint, json->valuedouble);
break;
case cJSON_String:
printf("%s: %s\n", json->string, json->valuestring);
break;
case cJSON_Array:
printf("%s: cJSON_Array\n", json->string);
break;
case cJSON_Object:
printf("%s: cJSON_Object\n", json->string);
break;
default:
printf("unknown type\n");
break;
}
cJSON *root = NULL;
//读取json
cJSON *json_root = readJsonFile(fileName);
//解析json
analysisJsonObj(json_root);
//解析json
analysisJsonPrint(json_root);
// char *fileName = "../JsonTable/persontest.json";
char *fileName = "/home/EdgeGateway_FSU_App/persontest.json";
}
#define TEMPCOUNTER_MAX 60
#define TEMPCOUNTER_TEMP_S 10
#define TEMPCOUNTER_Time_S 2
#define TEMPCOUNTER_Weather_S 60
MQTTClient_message tempmsg = MQTTClient_message_initializer;
char temp_str[10] = {0};
char tm_str[50] = {0};
char Wh_str[2][50] = {"clear day","cloudy day"};
int weather_Flag=0;
//int fd;
/* 向服务端发布芯片温度信息 */
for ( ; ; ) {
if(Temp_Counter<TEMPCOUNTER_MAX)
{
Temp_Counter++;
}
else
{
Temp_Counter=0;
}
if((Temp_Counter%TEMPCOUNTER_TEMP_S)==0) //温度
{
/* 读取温度值 */
fd = open("/sys/class/thermal/thermal_zone0/temp", O_RDONLY);
read(fd, temp_str, sizeof(temp_str));//读取temp属性文件即可获取温度
close(fd);
printf("Temp %s\n", temp_str);
/* 发布温度信息 */
tempmsg.payload = temp_str; //消息的内容
tempmsg.payloadlen = strlen(temp_str); //内容的长度
tempmsg.qos = 0; //QoS等级
tempmsg.retained = 1; //保留消息
if (MQTTCLIENT_SUCCESS !=
(rc = MQTTClient_publishMessage(client, TEMP_TOPIC, &tempmsg, NULL))) {
printf("Failed to publish message, return code %d\n", rc);
rc = EXIT_FAILURE;
goto unsubscribe_exit;
}
}
if((Temp_Counter%TEMPCOUNTER_Time_S)==0) //时间
{
/* 读取时间值 */
time_t tm;
/* 获取时间 */
tm = time(NULL);
if (-1 == tm) {
perror("time error");
exit(-1);
}
/* 将时间转换为字符串形式 */
ctime_r(&tm, tm_str);
/* 打印输出 */
printf("当前时间: %s", tm_str);
/* 发布时间信息 */
tempmsg.payload = tm_str; //消息的内容
tempmsg.payloadlen = strlen(tm_str); //内容的长度
tempmsg.qos = 0; //QoS等级
tempmsg.retained = 1; //保留消息
if (MQTTCLIENT_SUCCESS !=
(rc = MQTTClient_publishMessage(client, TIME_TOPIC, &tempmsg, NULL))) {
printf("Failed to publish message, return code %d\n", rc);
rc = EXIT_FAILURE;
goto unsubscribe_exit;
}
}
if((Temp_Counter%TEMPCOUNTER_Weather_S)==0) //天气
{
/* 天气 */
printf("Temp %s\n", Wh_str[weather_Flag]);
/* 发布天气信息 */
tempmsg.payload = Wh_str[weather_Flag]; //消息的内容
tempmsg.payloadlen = strlen(Wh_str[weather_Flag]); //内容的长度
tempmsg.qos = 0; //QoS等级
tempmsg.retained = 1; //保留消息
if (MQTTCLIENT_SUCCESS !=
(rc = MQTTClient_publishMessage(client, WEATHER_TOPIC, &tempmsg, NULL))) {
printf("Failed to publish message, return code %d\n", rc);
rc = EXIT_FAILURE;
goto unsubscribe_exit;
}
if(weather_Flag)
{
weather_Flag=0;
}
else
{
weather_Flag=1;
}
}
sleep(1); //每隔1秒 更新一次数据
//sleep(30); //每隔30秒 更新一次数据
}
unsubscribe_exit:
if (MQTTCLIENT_SUCCESS !=
(rc = MQTTClient_unsubscribe(client, LED_TOPIC))) {
printf("Failed to unsubscribe, return code %d\n", rc);
rc = EXIT_FAILURE;
}
disconnect_exit:
if (MQTTCLIENT_SUCCESS !=
(rc = MQTTClient_disconnect(client, 10000))) {
printf("Failed to disconnect, return code %d\n", rc);
rc = EXIT_FAILURE;
}
destroy_exit:
MQTTClient_destroy(&client);
exit:
return rc;
}
#define TEMPCOUNTER_MAX 60
#define TEMPCOUNTER_TEMP_S 10
#define TEMPCOUNTER_Time_S 2
#define TEMPCOUNTER_Weather_S 60
MQTTClient_message tempmsg = MQTTClient_message_initializer;
char temp_str[10] = {0};
char tm_str[50] = {0};
char Wh_str[2][50] = {"clear day","cloudy day"};
int weather_Flag=0;
//int fd;
/* 向服务端发布芯片温度信息 */
for ( ; ; ) {
if(Temp_Counter<TEMPCOUNTER_MAX)
{
Temp_Counter++;
}
else
{
Temp_Counter=0;
}
if((Temp_Counter%TEMPCOUNTER_Time_S)==0) //时间
{
/* 读取时间值 */
time_t tm;
/* 获取时间 */
tm = time(NULL);
if (-1 == tm) {
perror("time error");
exit(-1);
}
/* 将时间转换为字符串形式 */
ctime_r(&tm, tm_str);
/* 打印输出 */
printf("当前时间: %s", tm_str);
/* 发布时间信息 */
tempmsg.payload = tm_str; //消息的内容
tempmsg.payloadlen = strlen(tm_str); //内容的长度
tempmsg.qos = 0; //QoS等级
tempmsg.retained = 1; //保留消息
if (MQTTCLIENT_SUCCESS !=
(rc = MQTTClient_publishMessage(client, TIME_TOPIC, &tempmsg, NULL))) {
printf("Failed to publish message, return code %d\n", rc);
rc = EXIT_FAILURE;
goto unsubscribe_exit;
}
}
if((Temp_Counter%TEMPCOUNTER_Weather_S)==0) //天气
{
/* 天气 */
printf("Temp %s\n", Wh_str[weather_Flag]);
/* 发布天气信息 */
tempmsg.payload = Wh_str[weather_Flag]; //消息的内容
tempmsg.payloadlen = strlen(Wh_str[weather_Flag]); //内容的长度
tempmsg.qos = 0; //QoS等级
tempmsg.retained = 1; //保留消息
if (MQTTCLIENT_SUCCESS !=
(rc = MQTTClient_publishMessage(client, WEATHER_TOPIC, &tempmsg, NULL))) {
printf("Failed to publish message, return code %d\n", rc);
rc = EXIT_FAILURE;
goto unsubscribe_exit;
}
if(weather_Flag)
{
weather_Flag=0;
}
else
{
weather_Flag=1;
}
}
sleep(1); //每隔1秒 更新一次数据
//sleep(30); //每隔30秒 更新一次数据
}
unsubscribe_exit:
if (MQTTCLIENT_SUCCESS !=
(rc = MQTTClient_unsubscribe(client, LED_TOPIC))) {
printf("Failed to unsubscribe, return code %d\n", rc);
rc = EXIT_FAILURE;
}
disconnect_exit:
if (MQTTCLIENT_SUCCESS !=
(rc = MQTTClient_disconnect(client, 10000))) {
printf("Failed to disconnect, return code %d\n", rc);
rc = EXIT_FAILURE;
}
destroy_exit:
MQTTClient_destroy(&client);
exit:
return rc;
/* 客户端id、用户名、密码 *
* 当您成功申请到然也物联平台的社区版MQTT服务后
* 然也物联工作人员会给你发送8组用于连接社区版MQTT服务器
* 的客户端连接认证信息也就是客户端id、用户名和密码
* 注意一共有8组您选择其中一组覆盖下面的示例值
* 后续我们使用MQTT.fx或MQTTool的时候 也需要使用一组连接认证信息
* 去连接社区版MQTT服务器
* 由于这是属于个人隐私 笔者不可能将自己的信息写到下面 */
//#define CLIENTID "您的客户端ID" //客户端id
//#define USERNAME "您的用户名" //用户名
//#define PASSWORD "您的密码" //密码
// #define CLIENTID "MQTT_kooloo1989" //客户端id
// #define USERNAME "您的用户名" //用户名
// #define PASSWORD "您的密码" //密码
// #define CLIENTID "hjcid-01" //客户端id
// #define USERNAME "hjemq-c1" //用户名
// #define PASSWORD "Hj57471000" //密码
//获取 网关发布遗嘱主题到服务器
strSql = "select OID,Value,Factor from CfgTab_devInfo";
nResult = sqlite3_get_table(pdb, strSql, &pResult, &nRow, &nCol, &errmsg);
ms_info1("koius nCol =%d nRow =%d %s\n", nCol,nRow,pResult[0]);
int kk;
for(kk=0;kk<(nRow);kk++)
{
ms_info2("%s ",pResult[kk*3]);
ms_info2("%s ",pResult[kk*3+1]);
ms_info2("%s ",pResult[kk*3+2]);
ms_info2("\r\n");
}
if ((nResult != SQLITE_OK) || (pResult[nCol] == NULL))
{
goto end;
}
else
{
//strncpy(g_DeviceCfg.MqttConf.MqttWillToplic_Server, pResult[nCol], ePublicTopic_Len);
ms_info1("网关发布遗嘱主题到服务器--> %s\n", g_DeviceCfg.MqttConf.MqttWillToplic_Server);
}
//汉字比对测试
//koolootest
if (!strcmp("汇珏网络", g_DeviceCfg.devConf.Brand))
{
printf("汉字测试\r\n"); //kooloo add 202312
}
char Temp_Str[100]={0}; //临时变量 kooloo 直接往strSql尾部添加字符串会造成Segmentation fault
//查表CfgTab_S_DevicePort0FuncDef kooloo add 2023092101
strcpy(Temp_Str,"select * from "); //先复制 再添加 kooloo add 202309
strcat(Temp_Str,CtgTab_S_DevicePortFuncDef_Buf[Tempj]); //将表名添加 kooloo add 20230928
nResult = sqlite3_get_table(pdb, Temp_Str, &pResult, &nRow, &nCol, &errmsg);
/* 打印系统信息 */
// ms_info1("uptime: %ld\r\n", pSysPara->sys_info.uptime); /* 自系统启动之后所经过的时间(以秒为单位) */
// ms_info1("totalram: %lu\r\n", pSysPara->sys_info.totalram);/* 总的可用内存大小 */
// ms_info1("freeram: %lu\r\n", pSysPara->sys_info.freeram);/* 还未被使用的内存大小 */
// ms_info1("procs: %u\r\n", pSysPara->sys_info.procs); /* 系统当前进程数量 */
C_FSUDEVICETABLE
//解析courses
// cJSON *json_professional=cJSON_CreateObject();
//printfJson(json_courses); //打印测试 kooloo add 202312
// cJSON *json_array = cJSON_GetArrayItem(json_courses, 49-1);
// printf("lenkljakhldjf %d \r\n",strlen(json_array));
//printfJson(json_array);
//cJSON *json_st=cJSON_CreateObject();
//cJSON_AddItemToObject(json_st, "IDCodeContent", json_courses);
//printfJson(json_st); //打印测试 kooloo add 202312
//if (json_courses)
{
cJSON *json_array = cJSON_GetArrayItem(json_courses, 49-1);
// //修改替换pSysPara->sys_info.uptime
cJSON_ReplaceItemInObject(json_array, "Value", cJSON_CreateString("80"));
printf("lenkljakhldjf %d \r\n",strlen(json_array));
printfJson(json_array);
// cJSON *json_course_Value = cJSON_GetObjectItem(json_array,"Value");
//json_course_Value->valuedouble
// cJSON *json_course_Val=cJSON_CreateObject();
//cJSON_AddItemToObject(json_course_Val, "name", cJSON_CreateString("milo"));
//更新DataArr第1元素
//cJSON_ReplaceItemInArray(json_courses, 49-1, json_array);
}
//printfJson(json_courses); //打印测试 kooloo add 202312
cJSON_CreateStringArray
printf("json_Arr:\r\n");
printfJson(json_Arr);
cJSON *json_arraydata = cJSON_GetArrayItem(json_Arr, 49-1);
printf("json_arraydata:\r\n");
printfJson(json_arraydata);
//更新Type的值
int koolotest=cJSON_ReplaceItemInObject(json_arraydata, "Value", cJSON_CreateString("char"));
// cJSON *json_arraydjhjhj =cJSON_Duplicate(json_arraydata,1);
if(koolotest==0)
{
printf("替换失败\r\n");
}
else
{
printf("替换ok\r\n");
}
SearchName
// cJSON * DataArr = cJSON_GetObjectItem(json_root,"IDCodeContent");
//ms_info1("cJSON_GetObjectItem DataArr\r\n"); //数据库初始化 kooloo add 202309
//printfJson(DataArr); //打印测试 kooloo add 202312
cJSON * DataAtest=GetJson_FSUDevice_UpdateData(json_FSUSelf_root);
// cJSON *json_st=cJSON_CreateObject();
//cJSON_AddItemToObject(json_st, "IDCodeConte0nt", DataAtest);
//printfJson(json_st); //打印测试 kooloo add 202312
//printfJson(DataAtest); //打印测试 kooloo add 202312
printfJson(DataAtest); //打印测试 kooloo add 202312
g_DeviceCfg.FSUDeviceSelfTableName
cJSON *json_root=cJSON_CreateObject();
cJSON_AddItemToObject(json_root, C_JSON_FUNCCFGDEF_NUMBER, cJSON_CreateString("2"));
cJSON *json_courses = cJSON_CreateArray(); //注意: courses也是一个数组, 其内部有两个对象
cJSON* json_pItem1 = cJSON_CreateObject();
cJSON_AddItemToObject(json_pItem1, C_JSON_FUNTYPEID, cJSON_CreateString("1"));
cJSON_AddItemToObject(json_pItem1, C_JSON_FUNTYPEID_ADD, cJSON_CreateString("1"));
cJSON_AddItemToArray(json_courses, json_pItem1);
cJSON_AddItemToObject(json_root, C_JSON_FUNCCFGDEF, json_courses);
printfJson_sql(json_root);//koolootest
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** 函数名称: GetJson_JsonPackageData
** 功能描述: 将json 封包
** 参数描述:jsonarr 数组 dattype 数据类型
** 日  期: 2023年9月02日
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
cJSON * GetJson_JsonPackageData(cJSON *jsonarr,int dattype)
{
cJSON *json_r0t=cJSON_GetObjectItem(json_roottable, g_DeviceCfg.FSUDeviceSelfTableName); //获取表中数组; //指向根节点
int tempi;
cJSON *json_Arr = cJSON_GetObjectItem(json_r0t, C_IDCODECONTENT); //获取表中数组
//cJSON *json_Arr = cJSON_GetObjectItem(json_r0t, "FsuCode"); //获取表中数组
cJSON *json_rt=cJSON_GetObjectItem(json_roottable, C_GATEWAY_DATAFORMATPUBLIC); //获取表中数组; //指向根节点 获取公共表
ms_info1("json_r0t test %d\r\n",json_r0t->type); //数据库初始化 kooloo add 202309
printfJson(json_r0t);
ms_info1("json_Arr test %d\r\n",json_Arr->type); //数据库初始化 kooloo add 202309
printfJson(json_Arr);
ms_info1("json_rt test %d\r\n",json_rt->type); //数据库初始化 kooloo add 202309
printfJson(json_rt);
cJSON *jsonsubnode=cJSON_CreateArray();
cJSON* json_pItem1 = cJSON_CreateObject();
cJSON_AddItemToObject(json_pItem1,"JH", cJSON_CreateString("pResult[tempi*3]")); //添加到json表中 kooloo add 202312
cJSON_AddItemToObject(json_pItem1,"pResult[1]", cJSON_CreateString("pResult[tempi*3+1]"));
cJSON_AddItemToObject(json_pItem1,"pResult[2]", cJSON_CreateString("pResult[tempi*3+2]"));
cJSON_AddItemToArray(jsonsubnode, json_pItem1);
ms_info1("jsonsubnode test %d\r\n",jsonsubnode->type); //数据库初始化 kooloo add 202309
printfJson(jsonsubnode);
tempi=cJSON_ReplaceItemInObject(json_rt, C_IDCODECONTENT, jsonsubnode);
ms_info1("json_rt rep1 test %d rt =%d\r\n",json_rt->type,tempi); //数据库初始化 kooloo add 202309
printfJson(json_rt);
tempi=cJSON_ReplaceItemInObject(json_rt, C_IDCODECONTENT, json_Arr);
ms_info1("json_rt rep3 test %d rt =%d\r\n",json_rt->type,tempi); //数据库初始化 kooloo add 202309
printfJson(json_rt);
tempi=cJSON_ReplaceItemInObject(json_rt, C_GATEWAY_DATATYPE, cJSON_CreateString("YYY"));
ms_info1("json_rt rep2 test %d rt =%d\r\n",json_rt->type,tempi); //数据库初始化 kooloo add 202309
printfJson(json_rt);
//获取时间戳 kooloo add 202312 更新时间戳
char strbuf[20] = {0};
GetLocalTimeStr(strbuf);
cJSON_ReplaceItemInObject(json_rt, C_TIMESTAMP, cJSON_CreateString(strbuf));
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** 函数名称: GetJson_JsonPackageData
** 功能描述: 将json 封包
** 参数描述:jsonarr 数组 dattype 数据类型
** 日  期: 2023年9月02日
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
cJSON * GetJson_JsonPackageData(cJSON *jsonarr,char *strtype)
{
cJSON *json_rt=cJSON_GetObjectItem(json_roottable, C_GATEWAY_DATAFORMATPUBLIC); //获取表中数组; //指向根节点 获取公共表
cJSON *json_arr=cJSON_CreateArray(); //临时组
int tempi,size=cJSON_GetArraySize(jsonarr);
printf("size =%d\r\n",size);
for(tempi=0;tempi<size;tempi++)
{
cJSON *json_pItem1=cJSON_GetArrayItem(jsonarr, tempi);//获取json 数组 kooloo add 202312
cJSON_AddItemToArray(json_arr, json_pItem1);
printf("tempi =%d\r\n",tempi);
printfJson(json_pItem1);
}
printfJson(json_arr);
cJSON_ReplaceItemInObject(json_rt, C_IDCODECONTENT, json_arr); //替换内容 kooloo add 202312
//cJSON_ReplaceItemInObject(json_rt, C_IDCODECONTENT, jsonarr); //替换内容 kooloo add 202312
cJSON_ReplaceItemInObject(json_rt, C_GATEWAY_DATATYPE, cJSON_CreateString(strtype)); //替换类型 kooloo add 202312
//获取时间戳 kooloo add 202312 更新时间戳
char strbuf[20] = {0};
GetLocalTimeStr(strbuf);
cJSON_ReplaceItemInObject(json_rt, C_TIMESTAMP, cJSON_CreateString(strbuf));
return json_rt; //返回
}
cJSON *json_r0t=cJSON_GetObjectItem(json_roottable, g_DeviceCfg.FSUDeviceSelfTableName); //获取表中数组; //指向根节点
cJSON *json_Arr = cJSON_GetObjectItem(json_r0t, C_IDCODECONTENT); //获取表中数组
ms_info1("GetJson_JsonPackageData test\r\n"); //数据库初始化 kooloo add 202309
cJSON *cjson_kltest=GetJson_JsonPackageData(json_Arr ,0);
printfJson(cjson_kltest); //打印测试 kooloo add 202312
//读取JSON
cJSON* readJsonFile(char *fileName) {
if (NULL == fileName) {
return NULL;
}
FILE *fp = NULL;
cJSON *json = NULL;
char line[1024] = {0};
char *data = NULL;
//打开一个文本文件,文件必须存在,只允许读
fp = fopen(fileName, "r");
if (NULL != fp) {
//seek末尾
fseek(fp, 0, SEEK_END);
//读取文件大小
long len = ftell(fp);
//seek起始位值
fseek(fp, 0, SEEK_SET);
data = (char*)malloc(len + 1);
fread(data, 1, len, fp);
fclose(fp);
}
printf("06readJsonFile data:%s\n", data);
cJSON *json_root = cJSON_Parse(data);
if (NULL == json_root) {
printf("cJSON_Parse error:%s\n", cJSON_GetErrorPtr());
}
if (NULL != data) {
free(data);
}
return json_root;
}
// char *fileName = "../JsonTable/persontest.json";
char *fileName = "/home/EdgeGateway_FSU_App/persontest.json";
FILE *fp = NULL;
cJSON *json = NULL;
char line[1024] = {0};
char *data = NULL;
long datalen;
//打开一个文本文件,文件必须存在,只允许读
fp = fopen(fileName, "r");
if (NULL != fp) {
//seek末尾
fseek(fp, 0, SEEK_END);
//读取文件大小
datalen= ftell(fp);
//seek起始位值
fseek(fp, 0, SEEK_SET);
data = (char*)malloc(datalen + 1);
fread(data, 1, datalen, fp);
fclose(fp);
}
printf("09readJsonFile data:%s\n", data);
cJSON *json_root = cJSON_Parse(data);
if (NULL == json_root) {
printf("cJSON_Parse error:%s\n", cJSON_GetErrorPtr());
}
//uint8_t buf[103];
//memset(data, 'A', 100);
int len;
len=mq_send(MqdSrcToDst, data, datalen, 0);
printf("N1N%s %d\r\n", data,len);
printf("N1N%d\r\n", MqdSrcToDst);
len=mq_send(MqdSrcToDst, data, datalen, 0);
printf("N4NCCC%s %d\r\n", data,len);
printf("N4NOIU%d\r\n", MqdSrcToDst);
//koolootest
len=mq_send(MqdSrcToDst, data, datalen, 0);
printf("sendbuf%s return %d strlen %d\r\n", data,len,datalen);
//koolootest
#define S_SystemApp_PATH "source /test_enterapp.sh" //进入到指定目录 kooloo add 202312
#define S_TimeAlignedApp_NAME "TimeAlignedApp"
#define S_TimeAlignedApp_KillNAME "killall TimeAlignedApp"
#define S_TimeAlignedApp_PATH_EXE "./home/EdgeGateway_FSU_App/App/TimeAlignedApp &"
#define S_TimeAlignedApp_ISRUNNING "ps -e -o pid,comm | grep TimeAlignedApp | grep -v PID | grep -v grep"
#define S_MainProcessApp_NAME "MainProcessApp"
#define S_MainProcessApp_KillNAME "killall MainProcessApp"
#define S_MainProcessApp_PATH_EXE "./home/EdgeGateway_FSU_App/App/MainProcessApp &"
#define S_MainProcessApp_ISRUNNING "ps -e -o pid,comm | grep MainProcessApp | grep -v PID | grep -v grep"
#define S_MqttClientApp_NAME "MqttClientApp"
#define S_MqttClientApp_KillNAME "killall MqttClientApp"
#define S_MqttClientApp_PATH_EXE "./home/EdgeGateway_FSU_App/App/MqttClientApp &"
#define S_MqttClientApp_ISRUNNING "ps -e -o pid,comm | grep MqttClientApp | grep -v PID | grep -v grep"
#define S_DevicePortGetApp_NAME "DevicePortGetApp"
#define S_DevicePortGetApp_KillNAME "killall DevicePortGetApp"
#define S_DevicePortGetApp_PATH_EXE "./home/EdgeGateway_FSU_App/App/DevicePortGetApp &"
#define S_DevicePortGetApp_ISRUNNING "ps -e -o pid,comm | grep DevicePortGetApp | grep -v PID | grep -v grep"
printf("koooo %d %d\n",tFunTyID_DevCod_buf[1].DevCod,tFunTyID_DevCod_buf[2].DevCod);
int i;
for( i=0;i<100;i++)
{
printf("koooo %d %d\n",tFunTyID_DevCod_buf[i].FunTyID,tFunTyID_DevCod_buf[i].DevCod);
}
OID_Content_t toid;
toid.Status =0;
//获取 表 根据检索表 kooloo add 202312
char Temp_oidStrA[100]={0}; //临时变量 kooloo
char Temp_oidStrB[100]={0}; //临时变量 kooloo
char *ptr = NULL;
ptr = strrchr(oidstr, '.'); //倒数 kooloo add 202312
printf("test start %d\n",toid.Status);
if(ptr==NULL)
{
return toid;
}
toid.SimilarDevSN=atoi(ptr); //获取首个值 kooloo add 202401
printf("ptr 1 %s\n",ptr);
printf("test %d\n",toid.SimilarDevSN);
strncpy(Temp_oidStrA,oidstr,(ptr-oidstr)); //将表名 添加 kooloo add 20230928
printf("Temp_oidStr %s\n",Temp_oidStrA);
ptr = strrchr(Temp_oidStrA, '.'); //倒数 kooloo add 202312
if(ptr==NULL)
{
return toid;
}
toid.SignalSN=atoi(ptr); //获取首个值 kooloo add 202401
printf("ptr 2 %s\n",ptr);
strncpy(Temp_oidStrB,oidstr,(ptr-Temp_oidStrA)); //将表名 添加 kooloo add 20230928
printf("Temp_oidStr %s\n",Temp_oidStrB);
ptr = strrchr(Temp_oidStrB, '.'); //倒数 kooloo add 202312
if(ptr==NULL)
{
return toid;
}
toid.SignalType=atoi(ptr); //获取首个值 kooloo add 202401
printf("ptr 3 %s\n",ptr);
memset(Temp_oidStrA,0,100);
strncpy(Temp_oidStrA,oidstr,(ptr-Temp_oidStrB)); //将表名 添加 kooloo add 20230928
printf("Temp_oidStr %s\n",Temp_oidStrA);
ptr = strrchr(Temp_oidStrA, '.'); //倒数 kooloo add 202312
if(ptr==NULL)
{
return toid;
}
toid.SignalSN=atoi(ptr); //获取首个值 kooloo add 202401
printf("ptr 2 %s\n",ptr);
strncpy(Temp_oidStrB,oidstr,(ptr-Temp_oidStrA)); //将表名 添加 kooloo add 20230928
printf("Temp_oidStr %s\n",Temp_oidStrB);
OID_Content_t testk=GetTyidadd_fromoidstr(".1.3.6.1.4.1.61262.4.98.1.2.1");
printf("DevType %d\n",testk.DevType);
while(1)
{
sleep(1);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** 函数名称: UART_Init
** 功能描述: 串口初始化
** 参数描述: 无
** 作  者: kooloo
** 日  期: 2022年03月21
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
void UART_Init(void)
{
int Tempi,Tempj;
int TempTypeID;
for(Tempi=0;Tempi<eSDPort_Number_Max;Tempi++) //首次初始化 则先初始化第一个设备 kooloo add 20230928
{
TempTypeID =g_CfgTab_Rs485_Gather.g_SDPortFuncDef[Tempi].g_DeviceFuncDef[g_CfgTab_Rs485_Gather.g_SDPortFuncDef[Tempi].TypeIndex].FunctionConfigTypeID;
ms_info1("PortID:%d,TypeID:%d\n",Tempi,TempTypeID); //数据库初始化 kooloo add 202309
if(Port_BindingFuncTypeID_FuncBuf[TempTypeID][eBFuncTID_Init]!=NULL)
{
Port_BindingFuncTypeID_FuncBuf[TempTypeID][eBFuncTID_Init](Tempi);
}
if(TempTypeID==C_HJWL_SERIALPORT_VPORT_TYID) //如果为透传接口 则同步初始化 其他透传口 kooloo add 20240201
{
CfgTab_SmartDevicePortFuncDef_t *SDP;
CfgTab_SmartDevicePortFuncDef_t *SDPsrc=&g_CfgTab_Rs485_Gather.g_SDPortFuncDef[Tempi];
for(Tempj=0;Tempj<eVirtuallyPort_Number_Max;Tempj++) //首次初始化 则先初始化第一个设备 kooloo add 20230928
{
SDP=&g_CfgTab_Rs485_Gather.g_SDPortFuncDef[Tempj+eSDPort_Number_Max];
SDP->fd=SDPsrc->fd;
SDP->ctx=SDPsrc->ctx;
}
}
}
}
Uint32 u32un;
u32un.DatBuf[0]=0x00;
u32un.DatBuf[1]=0xD7;
u32un.DatBuf[2]=0x47;
u32un.DatBuf[3]=0x42;
printf(" test %4X 0x4247D700\n",u32un.U32);
if((g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].g_DeviceFuncDef[g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeIndex_Send].PollArrIndexNow_Send+1)<
g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].g_DeviceFuncDef[g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeIndex_Send].PollArrNumber
) //如果小于 则需要进行下一个地址轮询 kooloo add 202310
{
g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].g_DeviceFuncDef[g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeIndex_Send].PollArrIndexNow_Send++;
}
else
{
g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].g_DeviceFuncDef[g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeIndex_Send].PollArrIndexNow_Send=0;
if((g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeIndex_Send+1)<g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeIndex_Send)
{
g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeIndex_Send++;
}
else
{
g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeIndex_Send=0; //从头开始轮询 同设备 kooloo add 202310
}
}
i_dex=g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].g_DeviceFuncDef[g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeIndex_Send].PollArrIndexNow_Send;
i_id =g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].g_DeviceFuncDef[g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeIndex_Send].FunctionConfigTypeID;
if(portnum<(eSDPort_Number_Max+eVirtuallyPort_Number_Max-1)) //根据端口数量
{
portnum++;
}
else
{
portnum=0;
}
if(i_id&&(i_id!=C_HJWL_SERIALPORT_VPORT_TYID))
{
mq_sendJson_subdevfromID(i_id,i_dex); //根据ID和轮询号发送数据 kooloo add 202312
pSysPara->g_SendSubDevData_TimeCnt=Timer_getNowTicks();
}
printf("SendData Test %d\n",eSDPort_Number_Max+eVirtuallyPort_Number_Max-1);
for(portnum=0;portnum<(eSDPort_Number_Max+eVirtuallyPort_Number_Max-1);portnum++) //根据端口数量 虚拟端口
{
printf("SendData portnum %d Tnum %d\n",portnum,g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeNumber);
ms_info1("portnum %d poll ID... num %d \n", portnum,g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeNumber);
for(TempTypeIndex=0;(TempTypeIndex)<g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeNumber;TempTypeIndex++)
{
//获取ID
i_id =g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].g_DeviceFuncDef[TempTypeIndex].FunctionConfigTypeID;
if(Port_BindingFuncTypeID_FuncBuf[i_id][eBFuncTID_ServiceLoop]!=NULL)
{
printf("SendData Test2 %d i_id%d\n",TempTypeIndex,i_id);
if(i_id&&(i_id!=C_HJWL_SERIALPORT_VPORT_TYID))
{
ms_info1("SendData PortID %d TypeID %d, Index %d\n",portnum,i_id,TempTypeIndex);
for(TempPollArrIndexNow=0;(TempPollArrIndexNow)<g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].g_DeviceFuncDef[TempTypeIndex].PollArrNumber;TempPollArrIndexNow++)
{
if(i_id&&(i_id!=C_HJWL_SERIALPORT_VPORT_TYID))
{
mq_sendJson_subdevfromID(i_id,i_dex); //根据ID和轮询号发送数据 kooloo add 202312
printf("SendData PortID %d TypeID %d, Index %d polli %d\n",portnum,i_id,TempTypeIndex,TempPollArrIndexNow);
sleep(1);
}
}
}
}
else
{
continue;
}
}
}
// /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// ** 函数名称: UART_Init
// ** 功能描述: 异步I/O初始化函数
// ** 参数描述: 无
// ** 作  者: kooloo
// ** 日  期: 2022年03月21
// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
// static void async_io_init(void)
// {
// struct sigaction sigatn;
// int flag;
// int fd; //koolootest
// /* 使能异步I/O */
// flag = fcntl(fd, F_GETFL); //使能串口的异步I/O功能
// flag |= O_ASYNC;
// fcntl(fd, F_SETFL, flag);
// /* 设置异步I/O的所有者 */
// fcntl(fd, F_SETOWN, getpid());
// /* 指定实时信号SIGRTMIN作为异步I/O通知信号 */
// fcntl(fd, F_SETSIG, SIGRTMIN);
// /* 为实时信号SIGRTMIN注册信号处理函数 */
// //sigatn.sa_sigaction = io_handler; //当串口有数据可读时会跳转到io_handler函数
// sigatn.sa_flags = SA_SIGINFO;
// sigemptyset(&sigatn.sa_mask);
// sigaction(SIGRTMIN, &sigatn, NULL);
// }
// /**
// ** 信号处理函数,当串口有数据可读时,会跳转到该函数执行
// **/
// static void io_handler(int sig, siginfo_t *info, void *context)
// {
// unsigned char buf[10] = {0};
// int ret;
// int n;
// int fd; //koolootest
// if(SIGRTMIN != sig)
// return;
// /* 判断串口是否有数据可读 */
// if (POLL_IN == info->si_code) {
// ret = read(fd, buf, 8); //一次最多读8个字节数据
// printf("[ ");
// for (n = 0; n < ret; n++)
// printf("0x%hhx ", buf[n]);
// printf("]\n");
// }
// }
测试使用
Uint_bit_flag Uint_bit;
char strbuf[20] = {0}; //临时用变量 转换用
Uint_bit.byte=0;
unsigned short aaa=0xFFFFFFFFF;
printf("short %d\n",aaa);
sprintf(strbuf, "%d", Uint_bit.bits.bit0);
printf("str %s %d %d \n",strbuf,Uint_bit.byte,Uint_bit.bits.bit0);
Uint_bit.bits.bit0=1;
sprintf(strbuf, "%d", (int)Uint_bit.bits.bit0);
printf("str %s %d %d \n",strbuf,Uint_bit.byte,(int)Uint_bit.bits.bit0);
Uint_bit.byte=1;
sprintf(strbuf, "%d", Uint_bit.bits.bit0);
printf("str %s %d %d \n",strbuf,Uint_bit.byte,(unsigned int)Uint_bit.bits.bit0);
Uint16 Uintstrut;
Uintstrut.Uint16=0x11;
sprintf(strbuf, "%d", Uintstrut.Uint16&0xFF);
printf("str %s %d \n",strbuf,Uintstrut.Uint16);
return 0;
extern int (*Port_BindingFuncTypeID_FuncBuf[eFCT_DeviceNumber_Max][eFCT_FuncNumber_Max])(int PortID);
if(Port_BindingFuncTypeID_FuncBuf[0][0]==NULL)
{
printf("Port_BindingFuncTypeID_FuncBuf[0][0](0); 执行前\n");
sleep(1);
Port_BindingFuncTypeID_FuncBuf[0][0](0);
printf("Port_BindingFuncTypeID_FuncBuf[0][0](0); 执行后\n");
}
/***************************************************************
Copyright © huijue Network Co., Ltd. 1998-2129. All rights reserved.
Copyright © 上海汇珏网络通信设备股份有限公司 1998-2129. All rights reserved.
文件名 : slavePoll.c
作者 : kooloo
版本 : V1.0
描述 : 动环监控/边缘网关FSU RS485接口监测轮询
硬件平台 : iMX6ULL
内核版本 : linux-imx-4.1.15-2.1.0-g3dc0a4b-v2.7
编译器版本 gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf
日志 : 初版V1.0 2023/7/15 kooloo创建
***************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "public.h"
#include "modbus.h"
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <FuncConfigTypeID_Public.h> //功能函数定义相关函数 kooloo add 20230922
#include "mslog.h"
#if 1 //方便折叠 kooloo add 2023
static const int RS485_idx[eSDPort_Number_Max]={0,1,2,3,4,5,6,7};
static const int RS485_idx_non=8;
static void pollThreadcreate(void);
extern void mbPollUartTreat(uint8_t idx);
extern void non_mbPollUartTreat(uint8_t idx);
#endif
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** 函数名称: slavePollThread
** 功能描述: 动环监控从设备轮询线程
** 参数描述: 无
** 作  者: kooloo
** 日  期: 2022年03月21
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
void * slavePollThread(void *arg)
{
ms_info1("pollThreadcreate\n"); //创建轮询主线程 kooloo add 202309
pollThreadcreate(); //子线程创建 kooloo add 202309
sleep(10); //正常不会进入此
return 0;
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** 函数名称: mbPollThread
** 功能描述: 标准modbus接口轮询线程 全部以标准Modbus接口创建线程 内部会根据数据库定义其数据
** 参数描述: 无
** 作  者: kooloo
** 日  期: 2022年03月21
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
void * mbPollThread(void *arg)
{
uint8_t num=*(uint8_t *)arg;
ms_info1("RS485 PORT %d CREATE \n",num);
if(num<eSDPort_Number_Max)
{
mbPollUartTreat(num); //modbus串口处理
}
ms_info1("no data create to poll ,so RS485 PORT %d thread EXIT \n",num);
pthread_exit((void *)0);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** 函数名称: non_mbPollThread
** 功能描述: 非标设备轮询线程
** 参数描述:
** 作  者: kooloo
** 日  期: 2022年03月21
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
void * non_mbPollThread(void *arg)
{
int num=*(int *)arg;
ms_info1("Non-std PORT %d CREATE\n",num);
non_mbPollUartTreat(num);
ms_info1("no data create to Non-std poll ,so RS485 PORT %d thread EXIT \n",num);
pthread_exit((void *)0);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** 函数名称: pollThreadcreate
** 功能描述: 标准modbus接口轮询线程 全部以标准Modbus接口创建线程 内部会根据数据库定义
** 参数描述: 无
** 作  者: kooloo
** 日  期: 2022年03月21
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
static void pollThreadcreate(void)
{
int Tempi;
void *retval;
pthread_t thr[eSDPort_Number_Max];
pthread_t non_thr;
if(pthread_create(&non_thr, NULL, non_mbPollThread, (void *)&RS485_idx_non)!=0)
{
ms_error1("create non thread 8 failed1\n");
return;
}
for(Tempi=0;Tempi<eSDPort_Number_Max;Tempi++) //6个modbus接口轮询线程
{
if(pthread_create(&thr[Tempi], NULL, mbPollThread, (void *)&RS485_idx[Tempi])!=0)
{
ms_error1("create thread %d failed!\n",Tempi);
return;
}
}
for(Tempi=0;Tempi<eSDPort_Number_Max;Tempi++) //等待线程 kooloo add 202309
{
pthread_join(thr[Tempi], &retval);
}
pthread_join(non_thr, &retval);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** 函数名称: mbPollUartTreat
** 功能描述: modbus设备轮询及数据处理
** 参数描述: idx 串口设备("/dev/ttymxc2"
** 作  者: kooloo
** 日  期: 2022年03月21
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
void mbPollUartTreat(uint8_t idx)
{
struct timespec NanoSleepTime_t={C_PTHREAD_SLEEPTIME_TV_SEC,C_PTHREAD_SLEEPTIME_TV_NSEC};
int DbgTime[eSDPort_Number_Max];
int TempTypeID =g_CfgTab_Rs485_Gather.g_SDPortFuncDef[idx].g_DeviceFuncDef[g_CfgTab_Rs485_Gather.g_SDPortFuncDef[idx].TypeIndex].FunctionConfigTypeID;
//uint8_t MqmsgbufVp[C_SERIALPORT_MQ_MSGSIZE];
Dev_TransmitCmd_t tDev_TCmd;
tDev_TCmd.Rxbuf= (char*)malloc(C_SERIALPORT_MQ_MSGSIZE);
memset(tDev_TCmd.Rxbuf, 0, C_SERIALPORT_MQ_MSGSIZE);
int MqmsglenSp;
while(1)
{
MqmsglenSp = mq_receive(MqdSerialPort, &tDev_TCmd, C_SERIALPORT_MQ_MSGSIZE, NULL);
if (MqmsglenSp > 0)
{
if(Port_BindingFuncTypeID_FuncBuf[tDev_TCmd.TyID][eBFuncTID_Fast_SetCtrlHandleFunc]!=NULL)
{
Port_BindingFuncTypeID_FuncBuf[tDev_TCmd.TyID][eBFuncTID_Fast_SetCtrlHandleFunc](&tDev_TCmd);
}
printf("Rxcmd: ID:%d Data:%s Len:%d\n",tDev_TCmd.TyID, tDev_TCmd.Rxbuf, MqmsglenSp);
}
#if 1 //调试信息打印
if(DbgTime[idx]<C_PTHREAD_DBG_TIME_NUMBER)
{
DbgTime[idx]++;
}
else
{
DbgTime[idx]=0;
ms_info1("mb port %d poll...\n", idx);
}
#endif
if(Port_BindingFuncTypeID_FuncBuf[TempTypeID][eBFuncTID_ServiceLoop]!=NULL)
{
if(Port_BindingFuncTypeID_FuncBuf[TempTypeID][eBFuncTID_ServiceLoop](idx)==eEndOfRoundFlag_ok) //如果同地址数据读取完毕 则进行下一个数据读取 kooloo add 202310
{
if((g_CfgTab_Rs485_Gather.g_SDPortFuncDef[idx].TypeIndex+1)<g_CfgTab_Rs485_Gather.g_SDPortFuncDef[idx].TypeNumber)
{
g_CfgTab_Rs485_Gather.g_SDPortFuncDef[idx].TypeIndex++;
}
else
{
g_CfgTab_Rs485_Gather.g_SDPortFuncDef[idx].TypeIndex=0; //从头开始轮询 同设备 kooloo add 202310
}
TempTypeID =g_CfgTab_Rs485_Gather.g_SDPortFuncDef[idx].g_DeviceFuncDef[g_CfgTab_Rs485_Gather.g_SDPortFuncDef[idx].TypeIndex].FunctionConfigTypeID;
if(Port_BindingFuncTypeID_FuncBuf[TempTypeID][eBFuncTID_Init]!=NULL)
{
//Port_BindingFuncTypeID_FuncBuf[TempTypeID][eBFuncTID_Init](idx);
}
}
ms_info1("PortID %d TypeID %d, Index %d\n",idx,TempTypeID,g_CfgTab_Rs485_Gather.g_SDPortFuncDef[idx].TypeIndex);
}
sleep(3);
nanosleep(&NanoSleepTime_t, NULL);
}
if(Port_BindingFuncTypeID_FuncBuf[TempTypeID][eBFuncTID_ResRelease]!=NULL)
{
Port_BindingFuncTypeID_FuncBuf[TempTypeID][eBFuncTID_ResRelease](idx);
ms_info1("PortID Res Release %d \n",idx);
}
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** 函数名称: non_mbPollUartTreat
** 功能描述: 非标准 modbus设备轮询及数据处理 注意这里是非标准口
** 参数描述: idx 串口设备("/dev/ttymxc2"
** 作  者: kooloo
** 日  期: 2022年03月21
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
void non_mbPollUartTreat(uint8_t idx)
{
ms_info1("non_mbPollUartTreat Thread\n"); //创建轮询主线程 kooloo add 202309 虚拟串口主线程
ms_info1("Non-std poll... %d\n",idx);
int nonDbgTime[eVirtuallyPort_Number_Max];
int Vportnum=eSDPort_Number_Max;
g_CfgTab_Rs485_Gather.g_SDPortFuncDef[Vportnum].TypeIndex=0;
int TempTypeID =g_CfgTab_Rs485_Gather.g_SDPortFuncDef[Vportnum].g_DeviceFuncDef[g_CfgTab_Rs485_Gather.g_SDPortFuncDef[Vportnum].TypeIndex].FunctionConfigTypeID;
//uint8_t MqmsgbufVp[C_SERIALPORT_MQ_MSGSIZE];
Dev_TransmitCmd_t tDev_TCmd;
tDev_TCmd.Rxbuf= (char*)malloc(C_SERIALPORT_MQ_MSGSIZE);
memset(tDev_TCmd.Rxbuf, 0, C_SERIALPORT_MQ_MSGSIZE);
int MqmsglenSp;
while(1)
{
MqmsglenSp = mq_receive(MqdSerialPort, &tDev_TCmd, C_SERIALPORT_MQ_MSGSIZE, NULL);
if (MqmsglenSp > 0)
{
if(Port_BindingFuncTypeID_FuncBuf[tDev_TCmd.TyID][eBFuncTID_Fast_SetCtrlHandleFunc]!=NULL)
{
Port_BindingFuncTypeID_FuncBuf[tDev_TCmd.TyID][eBFuncTID_Fast_SetCtrlHandleFunc](&tDev_TCmd);
}
printf("Rxcmd: ID:%d Data:%s Len:%d\n",tDev_TCmd.TyID, tDev_TCmd.Rxbuf, MqmsglenSp);
}
#if 1 //调试信息打印
if(nonDbgTime[Vportnum-eSDPort_Number_Max]<C_PTHREAD_DBG_TIME_NUMBER)
{
nonDbgTime[Vportnum-eSDPort_Number_Max]++;
}
else
{
nonDbgTime[Vportnum-eSDPort_Number_Max]=0;
ms_info1("non Vport %d poll...\n", Vportnum-eSDPort_Number_Max);
}
#endif
for(Vportnum=eSDPort_Number_Max;Vportnum<(eSDPort_Number_Max+eVirtuallyPort_Number_Max-1);Vportnum++) //根据端口数量 虚拟端口
{
//Vportnum=eSDPort_Number_Max; //koolootest
for(g_CfgTab_Rs485_Gather.g_SDPortFuncDef[Vportnum].TypeIndex=0;(g_CfgTab_Rs485_Gather.g_SDPortFuncDef[Vportnum].TypeIndex)<g_CfgTab_Rs485_Gather.g_SDPortFuncDef[Vportnum].TypeNumber;
g_CfgTab_Rs485_Gather.g_SDPortFuncDef[Vportnum].TypeIndex++)
{
//g_CfgTab_Rs485_Gather.g_SDPortFuncDef[Vportnum].TypeIndex=0;//koolootest
TempTypeID =g_CfgTab_Rs485_Gather.g_SDPortFuncDef[Vportnum].g_DeviceFuncDef[g_CfgTab_Rs485_Gather.g_SDPortFuncDef[Vportnum].TypeIndex].FunctionConfigTypeID;
if(Port_BindingFuncTypeID_FuncBuf[TempTypeID][eBFuncTID_ServiceLoop]!=NULL)
{
if(Port_BindingFuncTypeID_FuncBuf[TempTypeID][eBFuncTID_ServiceLoop](Vportnum)==eEndOfRoundFlag_ok) //如果同地址数据读取完毕 则进行下一个数据读取 kooloo add 202310
{
if(Port_BindingFuncTypeID_FuncBuf[TempTypeID][eBFuncTID_Init]!=NULL)
{
//Port_BindingFuncTypeID_FuncBuf[TempTypeID][eBFuncTID_Init](Vportnum);
}
}
ms_info1("PortID %d TypeID %d, Index %d\n",Vportnum,TempTypeID,g_CfgTab_Rs485_Gather.g_SDPortFuncDef[Vportnum].TypeIndex);
}
else
{
continue;
}
}
ms_info1("non Vport %d poll... num %d \n", Vportnum-eSDPort_Number_Max,g_CfgTab_Rs485_Gather.g_SDPortFuncDef[Vportnum].TypeNumber);
}
sleep(3);
}
}
//cJSON_ReplaceItemInObject(json_rt, C_GATEWAY_DATATYPE, cJSON_CreateString(C_GATEWAY_WRITEDATA));
//mq_sendJson(json_rt); //将遥*数据发送 kooloo add 202312
Dev_TransmitCmd_t tDev_TCmd;
tDev_TCmd.TyID=tyid;
tDev_TCmd.Rxbuf = (char*)malloc(C_SERIALPORT_MQ_MSGSIZE);
memset(tDev_TCmd.Rxbuf, 0, C_SERIALPORT_MQ_MSGSIZE);
strncpy(tDev_TCmd.Rxbuf, msgbuf, C_SERIALPORT_MQ_MSGSIZE); //将数据拷贝 kooloo add 202401
if(tFunTyID_DevCod_buf[tyid].PortID<eSDPort_Number_Max) //如果为普通串口 则将消息传递到 普通串口队列中,否则传到虚拟串口中 kooloo add 20240312
{
len = mq_send(MqdSerialPort, (const char * )&tDev_TCmd, strlen(tDev_TCmd.Rxbuf)+sizeof(int), 0);
printf("MqdSerialPort:type :%d jsonlen %d json:%s,sendlen:%d\n",oid.SignalType, strlen(tDev_TCmd.Rxbuf)+sizeof(int), tDev_TCmd.Rxbuf, len);
}
else if(tFunTyID_DevCod_buf[tyid].PortID<(eSDPort_Number_Max+eVirtuallyPort_Number_Max)) //虚拟串口 kooloo add 20240312
{
len = mq_send(MqdVtSerialPort, (const char * )&tDev_TCmd, strlen(tDev_TCmd.Rxbuf)+sizeof(int), 0);
printf("MqdVtSerialPort:type :%d jsonlen %d json:%s,sendlen:%d\n",oid.SignalType, strlen(tDev_TCmd.Rxbuf)+sizeof(int), tDev_TCmd.Rxbuf, len);
}
free(tDev_TCmd.Rxbuf); //用完释放 kooloo add 202401
//mq_sendRerJson_subdevfromID(tyid, oid.SimilarDevSN - 1);
//ms_info1("type :%d cjson:%s,Rxlen:%d\n",oid.SignalType,tmsgbuf, strlen(tmsgbuf));
}
else if (!strcmp(C_SERVER_ALARMDATA, json_Type->valuestring)) //如果接收到的消息是FSUCODE 正确 则将消息转发 否则忽略 遥信 不需要调整
{
mq_sendResJson_subdevfromID(tyid, oid.SimilarDevSN - 1);
ms_info1("type :%d cjson:%s,Rxlen:%d\n",oid.SignalType,tmsgbuf, strlen(tmsgbuf));
}
else if (!strcmp(C_SERVER_CRTLDATA, json_Type->valuestring)) //如果接收到的消息是FSUCODE 正确 则将消息转发 否则忽略 遥控 更新数据
{
//cJSON_ReplaceItemInObject(json_rt, C_GATEWAY_DATATYPE, cJSON_CreateString(C_GATEWAY_CTRLDATA));
//mq_sendJson(json_rt); //将遥*数据发送 kooloo add 202312
Dev_TransmitCmd_t tDev_TCmd;
tDev_TCmd.TyID=tyid;
tDev_TCmd.Rxbuf = (char*)malloc(C_SERIALPORT_MQ_MSGSIZE);
memset(tDev_TCmd.Rxbuf, 0, C_SERIALPORT_MQ_MSGSIZE);
strncpy(tDev_TCmd.Rxbuf, msgbuf, C_SERIALPORT_MQ_MSGSIZE); //将数据拷贝 kooloo add 202401
if(tFunTyID_DevCod_buf[tyid].PortID<eSDPort_Number_Max) //如果为普通串口 则将消息传递到 普通串口队列中,否则传到虚拟串口中 kooloo add 20240312
{
len = mq_send(MqdSerialPort, (const char * )&tDev_TCmd, strlen(tDev_TCmd.Rxbuf)+sizeof(int), 0);
printf("MqdSerialPort:type :%d jsonlen %d json:%s,sendlen:%d\n",oid.SignalType, strlen(tDev_TCmd.Rxbuf)+sizeof(int), tDev_TCmd.Rxbuf, len);
}
else if(tFunTyID_DevCod_buf[tyid].PortID<(eSDPort_Number_Max+eVirtuallyPort_Number_Max)) //虚拟串口 kooloo add 20240312
{
len = mq_send(MqdVtSerialPort, (const char * )&tDev_TCmd, strlen(tDev_TCmd.Rxbuf)+sizeof(int), 0);
printf("MqdVtSerialPort:type :%d jsonlen %d json:%s,sendlen:%d\n",oid.SignalType, strlen(tDev_TCmd.Rxbuf)+sizeof(int), tDev_TCmd.Rxbuf, len);
}
free(tDev_TCmd.Rxbuf); //用完释放 kooloo add 202401
//mq_sendRecJson_subdevfromID(tyid, oid.SimilarDevSN - 1);
//ms_info1("type :%d cjson:%s,Rxlen:%d\n",oid.SignalType,tmsgbuf, strlen(tmsgbuf));
//cJSON_ReplaceItemInObject(json_rt, C_GATEWAY_DATATYPE, cJSON_CreateString(C_GATEWAY_WRITEDATA));
//mq_sendJson(json_rt); //将遥*数据发送 kooloo add 202312
char* tmsgbuf_spvp = (char*)malloc(C_SERIALPORT_MQ_MSGSIZE);
memset(tmsgbuf_spvp, 0, C_SERIALPORT_MQ_MSGSIZE);
strncpy(tmsgbuf_spvp, msgbuf, C_SERIALPORT_MQ_MSGSIZE); //将数据拷贝 kooloo add 202401
if(tFunTyID_DevCod_buf[tyid].PortID<eSDPort_Number_Max) //如果为普通串口 则将消息传递到 普通串口队列中,否则传到虚拟串口中 kooloo add 20240312
{
len = mq_send(MqdSerialPort, tmsgbuf_spvp, strlen(tmsgbuf_spvp), 0);
printf("MqdSerialPort:type :%d jsonlen %d json:%s,sendlen:%d\n",oid.SignalType, strlen(tmsgbuf_spvp), tmsgbuf_spvp, len);
}
else if(tFunTyID_DevCod_buf[tyid].PortID<(eSDPort_Number_Max+eVirtuallyPort_Number_Max)) //虚拟串口 kooloo add 20240312
{
len = mq_send(MqdVtSerialPort, tmsgbuf_spvp, strlen(tmsgbuf_spvp), 0);
printf("MqdVtSerialPort:type :%d jsonlen %d json:%s,sendlen:%d\n",oid.SignalType, strlen(tmsgbuf_spvp), tmsgbuf_spvp, len);
}
free(tmsgbuf_spvp); //用完释放 kooloo add 202401
//mq_sendRerJson_subdevfromID(tyid, oid.SimilarDevSN - 1);
//ms_info1("type :%d cjson:%s,Rxlen:%d\n",oid.SignalType,tmsgbuf, strlen(tmsgbuf));