#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;istring, size); int i; for(i=0;istring, 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;istring, 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;istring, 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 %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;tempifd=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_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)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 #include #include #include #include #include "public.h" #include "modbus.h" #include #include #include #include //功能函数定义相关函数 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 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] 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]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