Pages: [1]   Go Down
Author Topic: Serial.readBytesUntil - will not accept a BYTE buffer  (Read 1831 times)
0 Members and 1 Guest are viewing this topic.
UK
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
I've been working on reading incoming serial data.
My approach is to read packets into a local buffer.
This is working so long as my local buffer is of data type CHAR.

However, if I try to declare my buffer array as a BYTE, I get a compliation error:
SerialInvest_1.ino : In function 'void loop()':
SerialInvest_1.ino : 34: error: invalid conversion from 'byte*' to 'char*'
SerialInvest_1.ino : 34: error: initializing argument 2 of 'size_t Stream::readBytesUntil(char, char*, size_t)'


Yet, accroding to the Arduino documentation, the buffer 'should' be legitimate as a BYTE:
[http://arduino.cc/en/Serial/ReadBytesUntil]
Quote
Syntax
Serial.readBytesUntil(character, buffer, length)

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


Can anyone throw any insight/guidance on this please ?
I know a work around would be just to -127 from the values being sent .... but i'd prefer a clean solution than a workaround if possible.

Many Thanks
Simon

Code below shows the successful implimentation (it compiles)
However, changing either array (trash or buffer) to a BYTE data type, casuses compilation to fail
Code:
void loop() {
  char trash[64];      // local buffer to collect any erroneous data before SOP
  char buffer[2];      // local buffer to collect Packet data
  int flag = 0;          // miscellaneous int for debug purpose

  if(Serial.available()==0){                      // Do nothing if there is no data to read
    goto esc;
  }

  memset(buffer,0,sizeof(buffer));          // Ensure no old data remains from previous loop
  memset(trash,0,sizeof(trash));

  flag = Serial.readBytesUntil('>', trash, 64);  // get to start of packet (SOP)  ::  NB: Termination Char is not put into local buffer(trash), but IS removed from serial buffer.
  flag = Serial.readBytesUntil('<', buffer, 2);  // read until end of packet (EOP)

  while(Serial.available()){ // clear serial buffer of any remaining values
    Serial.read();
  }                   

esc:
  delay(20);   
}
Logged

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

Code:
  if(Serial.available()==0){                      // Do nothing if there is no data to read
    goto esc;
  }
If there is serial data to read, do it. Get rid of the stupid goto and label.

Code:
  char trash[64];      // local buffer to collect any erroneous data before SOP
A hoarder, huh? Why do you need to save trash?

char and byte are the same size. The Serial methods make no attempt to interpret the data. So, if the function is defined to take an array of chars, and you are sending an array of bytes (containing values in the 0 to 255 range), create a byte array and use a cast in the call to readBytesUntil().
Logged

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

Thanks Paul
(I think) ...


Quote
use a cast in the call to readBytesUntil()

Would you mind illustrating just how I do that ?
Unless I am misinterpreting what you are saying...I'm at a loss to see !?

On a more fundamental point:
 you say  'function is defined to take an array of chars'        ..... and this is the whole issue.
According to the Arduino documentation (previosuly quoted), it implies that the function is defined to take an array of chars OR bytes.

As to the other points,
What is the difference between :

 If(Serial.read()>0){ << do stuff >> }

and

 if(Serial.available()==0){goto esc; }
<< do stuff >>
esc:

They do exaclty the same thing ?



Hoarding ?
I want to hard this data to study why there is any trash in the first place.
It will be removed when I have finished my 'learning'.

Logged

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

Quote
Would you mind illustrating just how I do that ?
Code:
byte trash[64];
Serial.readBytesUntil('<', (char *)trash, 64);

Quote
According to the Arduino documentation (previosuly quoted), it implies that the function is defined to take an array of chars OR bytes.
Bitch about this in the Website and Forum section, not here. All that means to me is that I can use either a byte array or a char array to hold the data. It does not tell me that the method is overloaded to accept either type, although I can see how you would get that impression.

Quote
What is the difference between :

 If(Serial.read()>0){ << do stuff >> }

and

 if(Serial.available()==0){goto esc; }
<< do stuff >>
esc:

They do exaclty the same thing ?
They do, except that one does it in a way that looks like you know what you are doing, and the other looks a workaround for faulty logic.
Logged

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

Paul,

Wonderful, thank you so much, your code works and I can now get my full range smiley
I am fairly sure that I would not be able to do that in some other languages, so it has quite suprised me and I will look into the inner workings after this !

As for the 'goto lable' business :  personal preference.
I am less concerned with what it 'looks like' to others, than how it 'feels' for me whilst I am coding.
Bracketed sections are essential in many situations, but sometomes I feel they just add clutter.
But that's just me.



Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11158
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

According to the Arduino documentation (previosuly quoted), it implies that the function is defined to take an array of chars OR bytes.

This is a problem with the Arduino documentation in general. It describes the API textually but does not tell you the actual signature of the method(s).
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: