Go Down

Topic: I can't use readbytes() to byte* buffer (Read 732 times) previous topic - next topic

deLucast

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

  }
}

and sketch can not compile
Code: [Select]
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?

MarkT

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

deLucast

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

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

michinyon

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: [Select]

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


It may, or may not work.


MarkT

The reference page is buggy - they should always provide tested example code!!
[ I won't respond to messages, use the forum please ]

deLucast

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 :(
Code: [Select]
 if( Serial.available() > 1){
   recData[0] = Serial.read();
   recData[1] = Serial.read();

AWOL

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

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy