How to make Serial.readBytes() read bytes and not characters

I've been reading in the help section that it looks like Serial.readBytes() can read in bytes or characters

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

However when I try to read in -

byte length;
byte sEventBuffer[2];
length = Serial.readBytes(sEventBuffer,2);

I'm getting an invalid conversion from byte* to char* error

A byte and a char are the exact same thing - just one is signed and one is unsigned. The data held within them are exactly the same.

You can cast between one and the other at will.

length = Serial.readBytes((char *)sEventBuffer,2);

great thank you very much

majenko: A byte and a char are the exact same thing - just one is signed and one is unsigned. The data held within them are exactly the same.

You can cast between one and the other at will.

length = Serial.readBytes((char *)sEventBuffer,2);

I am having the same problem as the original poster but do not understand the (char *) portion of the answer. What does that mean?

Its C's arcane syntax for type-casting. For instance:

char x = (char) 0xFF ;

(Since char is signed, 0xFF is out of range, but the cast tells the compiler to not care).

int x = (int) 4.5 ;

Here the cast does more, it makes the compiler do a dataconversion by performing truncate() on the float value and taking the integer result. (I think).

In many situations the compiler automatically converts types, but when it doesn't a cast can force it to either convert or stop complaining. In particular casting pointer- and reference- typed expressions enables the data stored at that address to be treated as if of a different type.

Got it, thanks.

I modified my code - I am just learning how to communicate with Betwino and this is a test program to figure that out. Here is the code

// Test reading a file using the Betwino RFLIN command //
char text;
char hold;

void setup() {
  Serial.begin(9600); 
  }

void loop(){
     Serial.println("#S|TEXTEST|[1]#"); //send Betwino command
     text = Serial.readBytes((char*)hold, 7);  //get TEXTEST characters
     Serial.println(text);  /*output the result - I know that Betwino will not
     recognize it, I just want to see what it is */
    }

The file TEXTEST calls up a text file containing the 7 letters ABCDEFG. Betwino receives the command OK and outputs the characters. However, the variable text does not have any of them - it comes up with some cryptic ASCII character - can't really make out which one it is but that does not matter.

What am I doing wrong here?

MarkT: (Since char is signed, 0xFF is out of range, but the cast tells the compiler to not care).

That's not quite what the cast does, is it?

char text;
char hold;

...
void loop(){
     Serial.println("#S|TEXTEST|[1]#"); //send Betwino command
     text = Serial.readBytes((char*)hold, 7);  //get TEXTEST characters
     ...
}

You’re trying to stuff 7 bytes of data into a single char. That’s not going to work.

Arrch: You're trying to stuff 7 bytes of data into a single char. That's not going to work.

Does it even compile? I'm struggling to see how you could meaningfully cast a char to a char*.