Print and Write data using 13 byte array

Hi, I am trying to send and print the 13 bytes of data using serial communication using Arduino Uno board, when I use print function the data is not matching with that of actual data present in the array, how to properly print the data present in the array in binary format with 13 bytes of data

byte data_out[] = {0b00000000,0b00000001,0b00000010,0b00000011,0b00000100,0b00000101,
                   0b00000110,0b00000111,0b00001000,0b00001001,0b00001010,0b00001011,
                   0b00001100,0b00001101};
int i;
void setup() {
  Serial.begin(9600);
}

void loop() {
  for (i=0; i < 13; i++) {
    Serial.write(data_out[i]);
    Serial.print(data_out[i], BIN);
   Serial.print("\n");
   //delay(100);
  }
//  Serial.println(data_out[i]);
}

-----------------------Update-----------------------
Thank you all for the responses, I have made changes in the program one with only using printing array and other with serial write and print function for the array, while using serial write and print function I get these extra characters between the data as shown below, can anyone help me with this

Printing the array

uint8_t a[13] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0X77, 0x88,
                 0x01, 0x02, 0x03, 0x04, 0x05};
int i;

void Print_Hexa(uint8_t num) {
  char Hex_Array[2];

  sprintf(Hex_Array, "%02X", num);
  Serial.print(Hex_Array);
}

void setup() {
  Serial.begin(9600);
}

void loop() {
  for (i = 0; i <= 12; i++) {
   // Serial.write(a[i]);
    Print_Hexa(a[i]);
  }
  Serial.println();
  delay(100);
}

Using the Serial write and print function for the array

uint8_t a[13] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0X77, 0x88,
                 0x01, 0x02, 0x03, 0x04, 0x05
                };
int i;

void Print_Hexa(uint8_t num) {
  char Hex_Array[2];

  sprintf(Hex_Array, "%02X", num);
  Serial.print(Hex_Array);
}

void setup() {
  Serial.begin(9600);
}

void loop() {
  for (i = 0; i <= 12; i++) {
    Serial.write(a[i]);
    Print_Hexa(a[i]);
  }
  Serial.println();
  delay(100);
}

Thank you for your response, I have added the delay at the end of the loop, it's still the same output

Why are you using Serial.write?

(There is a mismatch between the topic title and the content)

What about the output is incorrect? Note that the ascii characters generated from the Serial.write are going to be control characters, some will be unprintable, others are things like tab, backspace, linefeed, carriage return, etc, which will affect the output in the serial monitor.

@anon73444976
I'm trying to send a known 13 bytes of data and receive the same across the destination

@david_2018
The output is incorrect which is not matching with the input data, as I'm sending only numerical data in binary format

I think you misunderstand ASCII control characters.

No, the output buffer will fill, and sending will block as it empties.
What point are you trying to make?

Let us try to show on Serial Monitor all the bits (00000000) of the 1st elemnt of your given array.

1. Giving print() command:

Serial.print(data_out[0]);  //Serial.print(data_out[0], DEC);     shows: 0

print() command/method shows (on Serial Monitor) the decimal digits (base is DEC) for the value of data_out[0], which is 0. So, we see 0 on the Serial Monitor. Not that the Serial Monitor does not show the "leadings zeros". In fact, the process is:

Serial.print(data_out[0]);  //default base is DEC ; data_out[0] = 0b0000000 = 0x00 = 0
==> Serial.write(0x30);      //0x30 is the ASCII code of '0', which is asserted on Serial Monitor 

2. Giving write() command:

Serial.write(data_out[0]);   //Serial.write(0b00000000); or Serial.write(0x00);

write() command/method puts 0x00 (the content of data_out[0] in binary) onto the Serial Monitor. Let us remember that Serial Monitor is an ASCII/Text Monitor which means that it expects ASCII code at its input and then it will show the corresponding charcater. If we look at the ASCII Code Table (Fig-1), it is seen that the ASCII Code 0x00 is for a non-printable charcater (called NUL). So, we see nothing on the Serial Monitor.


Figure-1:

3. To see all the 8-bit of data_out[0] on the Serial Monitor, we may execute the following codes:

for(int i=7;i>=0; i--)
  {
     Serial.print(bitRead(data_out[0], i), DEC);  //shows: 00000000
  }

Note that the Print functions do not produce a leading zero.

1 Like

I have learnt it now! (where is the old karma?)

You have 14 bytes of data and you are sending the first 13 bytes.

Since you are sending the binary bytes and printing the binary values to the same output device you will get the two interspersed. Perhaps you wanted to write them all and then print the values:

byte data_out[14] =
{
  0b00000000, 0b00000001, 0b00000010, 0b00000011,
  0b00000100, 0b00000101, 0b00000110, 0b00000111,
  0b00001000, 0b00001001, 0b00001010, 0b00001011,
  0b00001100, 0b00001101
};

void setup()
{
  Serial.begin(115200);
  delay(200); // Give Serial Monitor time to connect
}

void loop()
{
  for (int i = 0; i < 14; i++)
  {
    Serial.write(data_out[i]);
  }
  Serial.println();

  for (int i = 0; i < 14; i++)
  {
    Serial.println(data_out[i], BIN);
  }

  delay(1000);
}

Of course, the control characters don't show up:

a	


0
1
10
11
100
101
110
111
1000
1001
1010
1011
1100
1101

If you want to see how each control character looks:

  for (int i = 0; i < 14; i++)
  {
    Serial.print((int)data_out[i]);
    Serial.print(": \"");
    Serial.write(data_out[i]);
    Serial.println("\"");
  }

Most show up as nothing or a single space.
7 is the 'Bell' (Alert?) character
9 is the 'Tab' (\t) character.
10 is the 'Newline' (\n) character
13 is the 'Return' (\r) character

0: ""
1: ""
2: ""
3: ""
4: ""
5: ""
6: ""
7: "a"
8: ""
9: "	"
10: "
"
11: ""
12: ""
13: "
"

What were you expecting them to look like?

@anon73444976
I have made changes in the program from binary to hexa, could share the details for the ASCII control characters

@GolamMostafa
Thank you for your response, I have changed the program from binary to hexa format, I'm having issues while printing the array with serial.write function, this issue doesn't occur when only print function is used, can you assist me in removing these extra characters when printing the data

@johnwasser
Thank you for your response, I wanted to print the data in one sequence order with no spaces, for which I made changes in the program and also using hexa decimal instead of binary system, while printing the array with serial write function I get these unwanted characters between data, which doesn't occur when only print function is used, I don't require these unwanted characters printing in the serial monitor with the main data

So you wanted to send:
0123456789ABCD
or:
000102030405060708090A0B0C0D

Why didn't you say so?

@johnwasser
I wanted to send in the format:
0123456789ABCD

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