I am developing a MQTT app that takes some values through a board and bring it to a GUI. So I programmed also in C and in python (for the GUI). I have a problem with UTF-8 codification. In C I wrote it:
sprintf((char*)diagnostic_payload, "{\"Diagnostic response: \%X %X %X %X %X %X %X %X %X}", frameRead.can_id, frameRead.data[0], frameRead.data[1], frameRead.data[2], frameRead.data[3]
, frameRead.data[4], frameRead.data[5], frameRead.data[6], frameRead.data[7]);
size_t payloadLen = sizeof(diagnostic_payload);
const le_result_t publishResult = mqtt_Publish(
MQTTSession,
newTopic,
diagnostic_payload,
payloadLen,
MQTT_QOS0_TRANSMIT_ONCE,
retain);
if (publishResult == LE_OK)
LE_INFO(
"Message published");
It works good, I see the message on the command prompt of the broker on Windows, but I can't understand why, some strange characters are added:
I wrote an app that brings this value on a graphic interface:
client = mqtt.Client("Prova")
m_decode = ''
def on_log(client, userdata, level, buf):
print("log: "+buf)
def on_connect(client, userdata, flags, rc):
if rc==0:
print("Connected OK")
connection_status.config(text="Connection status: CONNECTED")
else:
print("Bad connection Returned code = ", rc)
connection_status.config(text="Connection status: BAD CONNECTION with error" + rc)
def on_disconnect(client, userdata, flags, rc=0):
print("DisConnected result code "+str(rc))
connection_status.config(text="Connection status: DISCONNECTED" + rc)
def on_message(client, userdata, msg):
topic=msg.topic
global m_decode
m_decode=str(msg.payload.decode("utf-8"))
print("message received", m_decode)
testo_receive.configure(text=m_decode)
#testo_receive.config(text="Messaggio ricevuto on diag_response: " + (m_decode))
#Le righe sotto vanno aggiunte se vuoi memorizzare i dati su file di testo
file = open("documento_test_diagnosi.txt", 'a')
file.write(m_decode)
file.write("\n")
file.close()
#questa funzione, tramite il tasto send, invia un messaggio su un certo topic (in esempio, mangoh)
def publish_message(client, msg_entry):
msg=msg_entry.get()
msg_entry.delete('0', 'end')
it works for a normal string of char, but with the string I mentioned above it doesn't work, obtaining this error:
Could the cause of the issue be the codification? Can someone help me to understand where am I wrong?
Any help will be appreciated,
Kevin
One of the problem is
size_t payloadLen = sizeof(diagnostic_payload);
You are getting the size of the buffer, not the size of the string. Note: Python allow you to use any Unicode character, also \0
in text. This was used in past for padding, or just as "non-character" on terminal which specified speed, but data not ready.
So you send data with dirty trailing bytes, which python try to print, but because they are random numbers and not valid UTF-8 sequences, you have the error.