This small sketch will not compile. Am I reading the reference wrong?
int numbytes;
byte InBytes[50] ;
char InChar[50];
void setup()
{
Serial.begin(115200);// initialize both serial ports:
Serial1.begin(115200);
}
void loop()
{
if (Serial1.available() )
{
numbytes = Serial1.readBytes(InBytes,50);
numbytes = Serial1.readBytes(InChar,50);
}
}
The error message is:
Test2.ino: In function 'void loop()':
Test2:14: error: invalid conversion from 'byte*' to 'char*'
Test2:14: error: initializing argument 1 of 'size_t Stream::readBytes(char*, size_t)'
The reference for StreamReadBytes says:
readBytes()
Description
readBytes() read characters from a stream into a buffer. The function terminates if the determined length has been read, or it times out (see setTimeout()).
readBytes() returns the number of characters placed in the buffer. A 0 means no valid data was found.
This function is part of the Stream class, and is called by any class that inherits from it (Wire, Serial, etc). See the Stream class main page for more information.
Syntax
stream.readBytes(buffer, length)
Parameters
stream : an instance of a class that inherits from Stream.
buffer: the buffer to store the bytes in (char[] or byte[])
length : the number of bytes to read (int)
Returns
byte
Yes, and no. The char and byte types are the same size, so they are sort of interchangeable. But, the method is defined as taking a char array. If you want to use a byte array, and I can't imagine why it matters, lie to the function:
I suspect that when the documentation was written, there are plans to overload the readBytes() method to accept either a char array or a byte array. But, plans change and documentation doesn't always keep up.
That makes a huge difference in the output from my sketch.
The char and byte types are the same size, so they are sort of interchangeable.
They do not have the same range, though. A byte has a range from 0 to 255. A char has a range from -128 to 127. If one type produces useful data, and the other does not, then it is not rocket science to figure out which type to use.
If that type causes issues with the readBytes() function, you can use a cast or not use the readBytes() function. After all, it is not doing anything that read() doesn't.
Your right of course but after years and years of programming Pascal and Delphi I am used to the idea that 'char' is short for character and therefore a signed character makes no sense at all (what would a negative letter 'A' be in the real world). So I prefer to use the byte type for the range 0..255 and its jarring to have to cast for it.
BTW - don't overstate rocket scientists. I used to be one... XD