Pages: [1]   Go Down
Author Topic: Serial.readBytes()  (Read 952 times)
0 Members and 1 Guest are viewing this topic.
SE USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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


Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

SE USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17261
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: