Serial.readuntil() in arduino

Thread Starter

bhuvanesh

Joined Aug 10, 2013
268
Rich (BB code):
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  if(Serial.available())
  {
    char a=Serial.readBytes(char,6)
    Serial.println(a);
  }
my idea is to read upto 6 bytes so i used 6(char,6) and to store buffer in character so declared as char(char,6).serial.readuntil function return character stored in buffer so assigned that to char

but i am getting error:"primary expression missing before char"

maybe this due to my poor understanding of that function help me please
 

Thread Starter

bhuvanesh

Joined Aug 10, 2013
268
Rich (BB code):
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  if(Serial.available())
  {if(Serial.findUntil("like","do"))
  Serial.println("found");
  else
  Serial.println("not found");
  }}
input:like do like
actuall output:
found
not found
found

expected output:
found
not found

because in that bunch of stream after the terminator string "do"it should not read next coming string.am i right.
 

sirch2

Joined Jan 21, 2013
1,037
The loop() function is run repeatedly so presumably the the last "found" comes from the next time the loop runs. Your terminator really ought to be at the end of the input string.

I haven't given it much thought but I can't initially see why you get a "not found" at all since both the target and the terminator are in your input.

<Edit>
Have now read the readUntil docs and can now see that the first "found" comes from the first "like", the "not found" comes from the "do" and the final "found" comes from the final "like"


On your first post you cannot use "char" as a variable name in
Rich (BB code):
Serial.readBytes(char,6)
you need somehting like:
Rich (BB code):
char a[6];
Serial.readBytes(a,6)
 
Last edited:

Thread Starter

bhuvanesh

Joined Aug 10, 2013
268
Rich (BB code):
void setup()
{
  Serial.begin(9600);
}
void loop()
{char a[10];
  if(Serial.available())
  {
    char b=Serial.readBytes(a,10  );
    Serial.println(a);
  }
}

sorry about that thanks

and i am getting junk values like
input:asd
output:asd~ and some times output:asdr

what might be the reason for that
 

sirch2

Joined Jan 21, 2013
1,037
Rich (BB code):
void setup()

and i am getting junk values like
input:asd
output:asd~ and some times output:asdr

what might be the reason for that
Rich (BB code):
Noise? incorrect Baud rate? Line termination character (i.e. pressing enter)?
 

shteii01

Joined Feb 19, 2010
4,644
Rich (BB code):
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  if(Serial.available())
  {
    char a=Serial.readBytes(char,6)
    Serial.println(a);
  }
my idea is to read upto 6 bytes so i used 6(char,6) and to store buffer in character so declared as char(char,6).serial.readuntil function return character stored in buffer so assigned that to char

but i am getting error:"primary expression missing before char"

maybe this due to my poor understanding of that function help me please
Your buffer is defined incorrectly. You have buffer that contains 6 characters. But your storage is setup to hold only 1 character.

Step 1. Define the buffer.
char a[7];
You define the buffer to be array of characters. According to arduino the char array must have one extra cell. So. You will be storing 6 chars in 6 cells of the array and have one extra cell, that is why char array has 7 cells.

Step 2. Receive the characters.
Serial.readBytes(a[],6);
Receive 6 characters, store them in char array a[].
 
Top