2

I need to print mentioned below byte array on the serial monitor screen of Arduino IDE

char b[]= {'0x7E', '0x00', '0x1C','0x90', '0x00', '0x13', '0XA2', '0x00', '0x41', '0x58', '0x1C', '0xCB', '0xFF', '0xFE', '0xC1', '0x7C', '0x00','0x0F', '0x00', '0x01', '0x00', '0x00', '0x37', '0x46', '0x46', '0x46', '0x00', '0x00', '0x00', '0xE7' };
void setup() {
Serial.begin(9600);
}

void loop() {
Serial.println(b[]);
delay(500);
}

But whenever I am trying a print, the error problem was there which was not resolved which is

C:\Users\Misha\Desktop\test\Test_2\Test_2.ino: In function 'void loop()':

Test_2:8:18: error: expected primary-expression before ']' token

 Serial.println(b[]);

                  ^

exit status 1
expected primary-expression before ']' token

Any suggestions on this will be great help.

4
  • what is b[]? array variable is simply b.
    – Juraj
    Jan 22, 2019 at 7:59
  • Hey, Thanks for the suggestion whenever I have used Serial.println(b) also but its printing only the LSB value, I am not able to print the MSB values.
    – varul jain
    Jan 22, 2019 at 8:05
  • of course. you can't print an array with print and invalid syntax will not help with it
    – Juraj
    Jan 22, 2019 at 8:08
  • What MSB values are you on about? You just have a list of 8 bit values. There is nothing "MSB" or "LSB" about them. I think you need to go back and rethink what it is you are doing and asking and get the two to match up.
    – Majenko
    Jan 22, 2019 at 10:59

1 Answer 1

4

You can’t print an array in that way.

If you want to print an array of hexadecimal value (with two digits), you have to use sprintf function and change declaration of b array.

uint8_t b[]= {0x7E, 0x00, 0x1C, 0x90, 0x00, 0x13, 0XA2, 0x00, 
              0x41, 0x58, 0x1C, 0xCB, 0xFF, 0xFE, 0xC1, 0x7C, 
              0x00, 0x0F, 0x00, 0x01, 0x00, 0x00, 0x37, 0x46, 
              0x46, 0x46, 0x00, 0x00, 0x00, 0xE7};
int i;

void printHex(uint8_t num) {
  char hexCar[2];

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

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

void loop() {
  for(i=0; i<sizeof(b); i++){
    printHex(b[i]);
 }

 Serial.println();
 delay(500);
}

The output produced is:

7E001C900013A20041581CCBFFFEC17C000F0001000037464646000000E7 7E001C900013A20041581CCBFFFEC17C000F0001000037464646000000E7 7E001C900013A20041581CCBFFFEC17C000F0001000037464646000000E7

6
  • Similar in this case It only displays the LSB values but no MSB values were displayed in serial monitor screen
    – varul jain
    Jan 22, 2019 at 8:06
  • What do you want to print? ASCII character or the hex value? In serial monitor Do you want this: 7E 00 ... ?
    – leoc7
    Jan 22, 2019 at 8:33
  • @varuljain I updated the answer.
    – leoc7
    Jan 22, 2019 at 9:14
  • Btw the buffer is too small to contain two characters and '\0' character.
    – KIIV
    Jan 22, 2019 at 9:29
  • I am anxious to understand HOW to use the Serial Monitor
    – martymarty
    Jan 8, 2020 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.