Serial data confusion

Hello, I am thoroughly confused by serial.print and serial.write.
I have transmit code that sends out via serial.print(loadTX_Decode) , which is defined as a byte (not int, char, unsigned or long...etc). When defined as byte the contents are readable, when defined as char it is weird tiny square symbol in serial monitor.

The receiver code examples i see online receive chars. If I send < 31 > for example, is that sent as:
1byte = <, 1byte = 3 , 1byte=1 and 1byte = >

I am trying to avoid mismatch between serial sent out and receiver code examples receiving chars. Any insight would be appreciated.

char myCharacter = ‘A’;
byte myLetter = 65;

Serial.print(‘A’); //prints the ASCII character A

Serial.print(myCharacter); //prints the ASCII character A

Serial.write(65) //prints the ASCII character A

Serial.write(myLetter) //prints the ASCII character A

1 Like

Are you asking, "how do I send and receive bytes, instead of strings (arrays of char)"? The simplified answer is "with read() and write()".

Yes, and how do I send and receive from Arduino to Arduino the same format, such that the content isn't misinterpreted. serial monitor only sends and receives ASCII. I guess I want to be able to develop code and test via serial monitor, but also want the contents to be interpreted by the other Arduino correctly.

One, or many bytes in a message?

Raw binary values include non-printable characters so you can't view it in a terminal program or print it.

My TX code is sending B00011111 - 1byte. When sent to serial monitor it is converted to "31" ASCII.
is that conversion to ASCII happening in serial monitor, or at the time it is transmitting out?

The standard advice for newbies is to use ASCII format and follow this tutorial: https://forum.arduino.cc/t/serial-input-basics-updated/382007

1 Like

You can't "convert" from 0b00110001 to 0x31 to "1". They are all the same thing. See reply #2.

1 Like

The following exercise might help you to understand between print() and write() methods.

1. Given the following:

byte y1 = 0x41;
char y2 = 0x41;

2. Now execute the following codes on the variables of Step-1 and observe the results on the Serial Monitor.

Serial.println(y1);  //shows: 65
Serial.println(y2);  //shows: A

3. Why are the outputs different in Step-2 while the contents of the variables (y1, y2) are same?
Ans:
The data types are checked by the print() method.

Serial.println(y1); is not executed; rather it is broken down to two write() methods as shown below, which are executed.

==> Serial.println(0x41, DEC);   //not executed
==> Serial.println(65, DEC);        //not executed
==> Serial.write(0x36);    //executed and 6 appears on SM
==> Serial.write(0x35);   // executed and 5 appears on SM

Serial.prinln(y2); is not executed; rather, it is broken down to the following write() method which is executed.

==> Serial.write(0x41); //executed and A appears on SM

4. Now execute the following codes on the variables of Step-1 and observe the results on the Serial Monitor.

Serial.write(y1);  //shows: A
Serial.write(y2);  //shows: A

5. Why are the outputs same while the data types are different?
Ans:
Serial.write() method sends the 8-bit content of the variables to the Serial Monitor, which are 0x41. The Serial Monitor is an ASCII-type Monitor and 0x41 is the ASCII code for the character A; as a result, A appears on SM.

6. You can execute the following code to verify the above proposition:

int y = 0x4241;
Serial.print(y);  
==> Serial.write(0x41);  //shows: A
2 Likes

The Print class has multiple overloads for the print() function. How the data is displayed depends on the datatype passed to print(). See the prototypes in Print.h:

    size_t print(const __FlashStringHelper *);
    size_t print(const String &);
    size_t print(const char[]);
    size_t print(char);
    size_t print(unsigned char, int = DEC);
    size_t print(int, int = DEC);
    size_t print(unsigned int, int = DEC);
    size_t print(long, int = DEC);
    size_t print(unsigned long, int = DEC);
    size_t print(double, int = 2);
    size_t print(const Printable&);

    size_t println(const __FlashStringHelper *);
    size_t println(const String &s);
    size_t println(const char[]);
    size_t println(char);
    size_t println(unsigned char, int = DEC);
    size_t println(int, int = DEC);
    size_t println(unsigned int, int = DEC);
    size_t println(long, int = DEC);
    size_t println(unsigned long, int = DEC);
    size_t println(double, int = 2);
    size_t println(const Printable&);
    size_t println(void);
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.