Skip to main content
formatted code
Source Link
jsotola
  • 1.6k
  • 2
  • 13
  • 22

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);
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.

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.

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.

Added code formatting and fixed grammar. Attempted to fix presentation/formatting
Source Link
Greenonline
  • 3.2k
  • 7
  • 37
  • 49

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. butHowever, it sendgets sent over serial port as AsciiASCII characters.

hereHere 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);

forFor example if I receive messageTemp = 2messageTemp = 2 and send it to variable cups_qtycups_qty and then send cups_qtycups_qty to serial via Serial.write(cups_qty); ISerial.write(cups_qty);, I receive the Equivalentequivalent character not number. example :

msg from MQTT >>> messageTemp = 2
cups_qty = messageTemp;
send over serial port >>> Serial.write(cups_qty);

what I see on serial RealTerm terminalFor example (Display As Ascii) >>> 2:

what I see on serial RealTerm terminal (Display As Hex) >>> 32

msg from MQTT          >>>   messageTemp = 2   
                             cups_qty = messageTemp;   
send over serial port  >>>   Serial.write(cups_qty);

whatWhat I see on serial RealTerm terminal (Display As int8) >>> 50

(Display As Ascii)  >>>   2

(Display As Hex)    >>>   32

(Display As int8)   >>>   50    

whatWhat I want to see on the terminal:

(Display As Ascii) >>> something unknown

(Display As Hex) >>> 02

(Display As int8) >>> 2

(Display As Ascii)  >>>   something unknown

(Display As Hex)    >>>   02   

(Display As int8)   >>>   2  

theThe confusing part is when I write something like : Serial.write(2) Serial.write(2), it works just as expected.

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. but it send 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. 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

what I see on serial RealTerm terminal (Display As Hex) >>> 32

what I see on serial RealTerm terminal (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

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.

Source Link

Send data with Serial.write() hex values

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. but it send 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. 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

what I see on serial RealTerm terminal (Display As Hex) >>> 32

what I see on serial RealTerm terminal (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