Pages: [1]   Go Down
Author Topic: I can't use readbytes() to byte* buffer  (Read 522 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is my code:
Code:
byte recData[4];
void setup()
{
  Serial.begin(9600);
}
 
void loop()
{
  if( Serial.available() > 0){
    Serial.readBytes(recData, 2);

  }
}
and sketch can not compile
Code:
In function 'void loop()':
:25: error: invalid conversion from 'unsigned char*' to 'char*'
:25: error: initializing argument 1 of 'size_t Stream::readBytes(char*, size_t)'
Am I wrong or something?
Logged

0
Offline Offline
Shannon Member
****
Karma: 207
Posts: 12200
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

byte and char are not the same type.
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank for ur fast answer, but in the reference page of Arduino said that we can use both type of variable??
Code:
Syntax

Serial.readBytes(buffer, length)

Parameters

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

Returns

byte
Logged

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3031
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well you are both right,  and wrong.

It should work,  but it doesn't.

Just humour it,   and make the array an array of char.

Or you could try putting a cast on the argument of the function, like this

Code:
Serial.readBytes( (char*)recData, 2);

It may, or may not work.

Logged

0
Offline Offline
Shannon Member
****
Karma: 207
Posts: 12200
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The reference page is buggy - they should always provide tested example code!!
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Lol thank you!
I think that so ridiculous when "readbyte()" cannot read Byte. Because I have to work with range 0 to 255.
And michinyon's solution may work but forcing to char* will change the value in range -128 to 127.
I ve just figued out myseft this way, but little bit quite not fun smiley-sad
Code:
 if( Serial.available() > 1){
    recData[0] = Serial.read();
    recData[1] = Serial.read();
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26344
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
And michinyon's solution may work but forcing to char* will change the value in range -128 to 127.
It doesn't change any values. The binary value remains the same, but it is up to you how to interpret it.

0xff == 25510 == -1
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: