Byte array not working with in Serial.readBytes()

I am trying to use a byte array as a buffer in Serial.readBytes (Arduino IDE 1.0.5-r2):

byte serialData[10];
int nBytes = 0;

nBytes = Serial.readBytes(serialData,10);

but it fails with the following error message:

 error: invalid conversion from 'byte*' to 'char*'
error: initializing argument 1 of 'size_t Stream::readBytes(char*, size_t)'

...even though the Serial.readBytes reference page states:

Serial.readBytes(buffer, length)
buffer: the buffer to store the bytes in (char[] or byte[])
length : the number of bytes to read (int)

What am I missing?

Hi Zoran

You can cast the pointer from byte pointer to char pointer:

nBytes = Serial.readBytes((char*)serialData,10);

But I appreciate that doesn't answer your question on why the reference page talks about byte[ ].

Regards

Ray

That is an Arduino foul up on the documentation I think, but it is still correct... sort of,

IDE 1.0.x Stream.h
https://github.com/arduino/Arduino/blob/master/hardware/arduino/cores/arduino/Stream.h#L76

IDE 1.5.x

You can see in the 1.5.x versions, the uint8_t methods have been added. ( uint8_t is byte, or unsigned char ), and you can see the 1.0.x versions only support char.

It only wraps the old function and does the cast for you. Might as well keep your code portable and add in the cast posted by Ray yourself.

Thanks guys. Works for me.

incorrect

nBytes = Serial.readBytes(DATA,10);

correct

Serial.readBytes(DATA,10);

data save in DATA>>>> then , what is nBytes >>> O_o

md3848:
nBytes = Serial.readBytes(DATA,10);
data save in DATA>>>> then , what is nBytes >>> O_o

From the code itself:

//returns the number of characters placed in the buffer (0 means no valid data found)