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.
1 Answer 1
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
-
Similar in this case It only displays the LSB values but no MSB values were displayed in serial monitor screenvarul jain– varul jain2019年01月22日 08:06:29 +00:00Commented 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– leoc72019年01月22日 08:33:42 +00:00Commented Jan 22, 2019 at 8:33
-
@varuljain I updated the answer.leoc7– leoc72019年01月22日 09:14:41 +00:00Commented Jan 22, 2019 at 9:14
-
Btw the buffer is too small to contain two characters and '0円' character.KIIV– KIIV2019年01月22日 09:29:14 +00:00Commented Jan 22, 2019 at 9:29
-
I am anxious to understand HOW to use the Serial Monitormartymarty– martymarty2020年01月08日 18:44:11 +00:00Commented Jan 8, 2020 at 18:44
b[]
? array variable is simplyb
.Serial.println(b)
also but its printing only the LSB value, I am not able to print the MSB values.print
and invalid syntax will not help with it