问题描述:
运行代码,在定位该函数时出现Segmentation fault问题,在函数中添加打印信息,发现问题处在*Info1 *Info2,
void parseJson_2data(cJSON* cjson_test, const char* fieldName1, const char* fieldName2, uint16_t* Info1, uint16_t* Info2) {
printf("1111111111111111111\n");
cJSON* cjson_data = cJSON_GetObjectItem(cjson_test, "data");
cJSON* cjson_field1 = cJSON_GetObjectItem(cjson_data, fieldName1);
cJSON* cjson_field2 = cJSON_GetObjectItem(cjson_data, fieldName2);
printf("2222222222222\n");
if (cjson_field1 == NULL || cjson_field2 == NULL) {
printf("Field not found in JSON data.\n");
cJSON_Delete(cjson_test);
return;
}
printf("cjson_field1->valueint:%d\n",cjson_field1->valueint);
printf("cjson_field2->valueint:%d\n",cjson_field2->valueint);
*Info1 = (uint16_t)atoi(cjson_field1->valuestring);
*Info2 = (uint16_t)atoi(cjson_field2->valuestring);
printf("33333333333333333\n");
cJSON_Delete(cjson_test);
}
解决后的代码
void parseJson_2data(cJSON* cjson_test, const char* fieldName1, const char* fieldName2, uint16_t* Info1, uint16_t* Info2) {
printf("1111111111111111111\n");
cJSON* cjson_data = cJSON_GetObjectItem(cjson_test, "data");
cJSON* cjson_field1 = cJSON_GetObjectItem(cjson_data, fieldName1);
cJSON* cjson_field2 = cJSON_GetObjectItem(cjson_data, fieldName2);
printf("2222222222222\n");
if (cjson_field1 == NULL || cjson_field2 == NULL) {
printf("Field not found in JSON data.\n");
cJSON_Delete(cjson_test);
return;
}
if (cjson_field1->type == cJSON_Number) {
printf("cjson_field1->valueint: %d\n", cjson_field1->valueint);
*Info1 = (uint16_t)cjson_field1->valueint;
} else {
printf("Invalid type for cjson_field1.\n");
cJSON_Delete(cjson_test);
return;
}
if (cjson_field2->type == cJSON_Number) {
printf("cjson_field2->valueint: %d\n", cjson_field2->valueint);
*Info2 = (uint16_t)cjson_field2->valueint;
} else {
printf("Invalid type for cjson_field2.\n");
cJSON_Delete(cjson_test);
return;
}
printf("33333333333333333\n");
cJSON_Delete(cjson_test);
}
关于我的测试解析线程
//测试解析线程用,不得已用不到
// void writeModbusRegisters(modbus_t* ctx, const uint16_t* values, int numValues) {
// int rc = modbus_write_registers(ctx, 0, numValues, values);
// if (rc == -1) {
// fprintf(stderr, "Failed to write registers: %s\n", modbus_strerror(errno));
// }
// }
// void* writeModbusRegistersWrapper(void* arg) {
// modbus_t* ctx = modbus_new_rtu("/dev/ttyS0", 9600, 'N', 8, 1);
// if (ctx == NULL) {
// fprintf(stderr, "Unable to allocate libmodbus context\n");
// return NULL;
// }
// // printf( "111111111111ctx sucess\n");
// // 设置从机地址
// modbus_set_slave(ctx, SERVER_ID);
// // 设置485模式
// modbus_rtu_set_serial_mode(ctx, MODBUS_RTU_RS485);
// printf( "2222222 485\n");
// // 连接设备
// if (modbus_connect(ctx) == -1) {
// fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
// modbus_free(ctx);
// // printf( "modbus_connect failed\n");
// return NULL;
// }
// uint16_t basic_machine_info[5] = {12, 13, 1, 1, 30};
// int numValues = sizeof(basic_machine_info) / sizeof(basic_machine_info[0]);
// while (1) {
// writeModbusRegisters(ctx, basic_machine_info, numValues);
// // 打印发送的报文
// // int headerLength = modbus_get_header_length(ctx);
// // const uint8_t* requestPacket = modbus_get_header_adu(ctx);
// // printModbusPacket(requestPacket, headerLength + numValues * 2 + 2); // 加上 CRC 校验的两个字节
// usleep(100000); // 延时100ms,0.1s
// }
// modbus_close(ctx);
// modbus_free(ctx);
// return NULL;
// }
// int main() {
// pthread_t thread1;
// int ret = pthread_create(&thread1, NULL, writeModbusRegistersWrapper, NULL);
// if (ret != 0) {
// fprintf(stderr, "Failed to create thread: %d\n", ret);
// return 1;
// }
// // printf( "lalalalala\n");
// // 在这里可以进行其他操作或等待线程结束
// pthread_join(thread1, NULL);
// return 0;
// }
// int main() {
// //Create thread 1
// ModbusData data1; //modbus数据
// ThreadData2 data2;
// // Set up the ModbusData structure
// int ret = pthread_create(&thread1, NULL, writeModbusRegistersWrapper, NULL);
// if (ret != 0) {
// fprintf(stderr, "无法创建线程\n");
// return EXIT_FAILURE;
// }
// // 解析json的数据并不断地放到数组创建线程并传递ThreadData结构体变量作为参数
// //pthread_create(&thread2, NULL, receiveJSONDataWrapper, (void*)&data2);//这里传数组的数据?把更新的数组在线程2里进行赋值
// // 等待线程结束
// pthread_join(thread1, NULL);
// modbus_close(ctx);
// // pthread_join(thread2, NULL);
// modbus_free(ctx);
// printf("qq Registers written successfully\n");
// return 0;
//}