Go Down

Topic: Serial.readBytes() (Read 1 time) previous topic - next topic

Osgeld

This is a problem thats been bugging me for a while, but now its flat out screwing me up

on the reference page for readBytes() it clearly states

Quote
buffer: the buffer to store the bytes in (char[] or byte[])


and if you do this

Code: [Select]

byte buffer[512];

void setup()
{
 Serial.begin(9600);
 Serial.readBytes(buffer, 512);
}
void loop(){}


the compile pukes out
Quote
sketch_dec09a.cpp: In function 'void setup()':
sketch_dec09a:5: error: invalid conversion from 'byte*' to 'char*'
sketch_dec09a:5: error: initializing argument 1 of 'size_t Stream::readBytes(char*, size_t)')


whats up with that? how would I get it to work with an array of bytes instead of chars?
thanks


http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

PaulS

The reason that the documentation mentions both byte and char is because they are the same size. The function is declared to take on, not overloaded to take either one.

So, if it expects char *, and you have byte *, lie to it. Tell it you have a char *. With a cast, of course.

Osgeld

I guess I could lie to it, it only happens in about a dozen places in my code, just kind of crappy that it says it will accept a byte when it wont .. might as well put boolean in there as well, since its 8 bits wide in avrgcc too (since we dont care to accurately document our functions around here)

ok thanks
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

retrolefty


I guess I could lie to it, it only happens in about a dozen places in my code, just kind of crappy that it says it will accept a byte when it wont .. might as well put boolean in there as well, since its 8 bits wide in avrgcc too (since we dont care to accurately document our functions around here)

ok thanks


I agree:
Quote

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


They should have just named it Serial.readChars(buffer, length) instead.
Stupid software guys are stupid.

Lefty

Go Up