问题
INT32 PkgSendMqtt_EcRequestSensorEvent(EventInfoStruct *pEventInfo)
{
MQTTAsync_responseOptions sendOptions =
MQTTAsync_responseOptions_initializer;
MQTTAsync_message pubMsg = MQTTAsync_message_initializer;
INT32 rc = 0;
CHAR msgSend[128] = {0};
json_object *pValue = NULL;
json_object *pObjectSerPro = NULL;
INT32 waitCount = 0;
while(!ConnectedFlag && ++waitCount<WAIT_COUNT);
if (waitCount >= WAIT_COUNT)
{
printf("(%s) Failed to wait for the connection.\n", __FUNCTION__);
return DEAL_ERROR;
}
/*
{
"loop_number":1,"sensor_address":1, "sensor_event":0-7,
"video_flag":0/1
}
*/
pObjectSerPro = json_object_new_object();
pValue = json_object_new_int(1);
json_object_object_add(pObjectSerPro, "loop_number", pValue);
pValue = json_object_new_int(2);
json_object_object_add(pObjectSerPro,"video_flag", pValue);
pValue = json_object_new_int(3);
json_object_object_add(pObjectSerPro, "sensor_address", pValue);
pValue = json_object_new_int(4);
json_object_object_add(pObjectSerPro, "sensor_event", pValue);
memset(msgSend, 0, 128);
sprintf(msgSend, "%s", json_object_to_json_string(pObjectSerPro));
printf("strlen=%d\n",strlen(json_object_to_json_string(pObjectSerPro)));
//strlen = 77
printf("%s\n", json_object_to_json_string(pObjectSerPro)); //flag1
printf("%s\n", msgSend); //flag2
json_object_put(pObjectSerPro);
sendOptions.onSuccess = onSend;
sendOptions.context = GlobalClient;
pubMsg.payload = msgSend;
pubMsg.payloadlen = strlen(pubMsg.payload);
pubMsg.qos = 1;
pubMsg.retained = 1;
printf(" #The send messsage is # : %s\n\n",
pubMsg.payload);//flag3
if ((rc = MQTTAsync_sendMessage(GlobalClient, "test", &pubMsg,
&sendOptions)) != MQTTASYNC_SUCCESS)
{
printf("(%s) Failed to start sendMessage, return code %d\n",
__FUNCTION__, rc);
return -1;
}
return -1;
}
I'm going to encapsulate a JSON format data and send it out. Flag1, Flag2 and flag3 print {"loop_number": 1, "video_flag": 2, "sensor_address": 3, "sensor_event ": 4}
, but mosquito_sub -t "#" shows {"loop_number": 1, "video_flag": 2, "se▒▒or_ address": 3, "sensor_event ": 4}
, I changed other strings, and there is still the garbled code.
来源:https://stackoverflow.com/questions/62187883/i-printed-the-payload-correctly-but-mosquito-sub-monitoring-broker-has-become