I'm using ESP32-C3 as hardware and Arduino IDE as my software core.
I receive a data from MQTT server and want to send it to another board using serial port as a hex number. However, it gets sent over serial port as ASCII characters.
Here is the code
unsigned char messageTemp; //data received from MQTT server
unsigned char cups_qty; //data to be transferred to serial port
cups_qty = messageTemp;
Serial.write(cups_qty);
For example if I receive messageTemp = 2
and send it to variable cups_qty
and then send cups_qty
to serial via Serial.write(cups_qty);
, I receive the equivalent character not number.
For example :
msg from MQTT >>> messageTemp = 2
cups_qty = messageTemp;
send over serial port >>> Serial.write(cups_qty);
What I see on serial RealTerm terminal
(Display As Ascii) >>> 2
(Display As Hex) >>> 32
(Display As int8) >>> 50
What I want to see on the terminal:
(Display As Ascii) >>> something unknown
(Display As Hex) >>> 02
(Display As int8) >>> 2
The confusing part is when I write something like Serial.write(2)
, it works just as expected.
1 Answer 1
You wrote:
if I receive
messageTemp = 2
and send it to variablecups_qty
and then sendcups_qty
to serial viaSerial.write(cups_qty);
, I receive the equivalent character not number.
This is incorrect. If you Serial.write(2)
you will receive, at the
other end, a byte with the numeric value 2. Try it!
The function Serial.write()
transmits binary numbers as-is, without
attempting any conversion. The following lines:
Serial.write('2');
Serial.write(0x32);
Serial.write(50);
Serial.print(2);
all do the same thing: transmit a byte with the numeric value 50. The
three expressions '2'
, 0x32
and 50
are just three ways of writing
the same number. Note that, unlike Serial.write()
, Serial.print()
formats the numbers in ASCII before transmitting.
It looks like your MQTT server sent you a number in ASCII, which you
probably did not expect. If you know for sure that this is a single
ASCII digit, you can convert it to binary by subtracting the ASCII code
for '0'
, namely 48:
Serial.write(messageTemp - '0'); // send messageTemp in binary
-
thank you Edgar for your answer. I did what you have suggested >>> Serial.write(messageTemp - '0'); and it works but only when the received data is just one digit. for example the MQTT server sends 4 payloads>> 2, 80, 4, 5 and after conversion I receive the following over serial port in HEX format >> 0x02 , 0X38, 0x04, 0X05 the data 80 converted to 0x38 which is ASCII format of digit 8! what do you suggest? the incoming data could be 1 , 2 or 3 digitsEric Matevosian– Eric Matevosian2024年06月25日 07:30:49 +00:00Commented Jun 25, 2024 at 7:30
-
for example I want to receive the data 80 as >> 0X50 or the data 90 as >> 0X5AEric Matevosian– Eric Matevosian2024年06月25日 07:44:28 +00:00Commented Jun 25, 2024 at 7:44
-
@EricMatevosian: You want to read a number that is formatted as a text string. This is called parsing. Search the Web for "Arduino parse number".Edgar Bonet– Edgar Bonet2024年06月25日 09:26:33 +00:00Commented Jun 25, 2024 at 9:26
The confusing part is when I write something like Serial.write(2), it works just as expected.
... you are doingSerial.write('2')
.... the two are not the same ... your question. is not about Arduino ... it is about C++ languageSerial.print(cups_qty, HEX);
what data you actually received.