Serial.readuntil() in arduino

Discussion in 'Embedded Systems and Microcontrollers' started by bhuvanesh, Jan 2, 2014.

  1. bhuvanesh

    Thread Starter Member

    Aug 10, 2013
    268
    2
    Code ( (Unknown Language)):
    1. void setup()
    2. {
    3.   Serial.begin(9600);
    4. }
    5. void loop()
    6. {
    7.   if(Serial.available())
    8.   {
    9.     char a=Serial.readBytes(char,6)
    10.     Serial.println(a);
    11.   }
    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
     
  2. bhuvanesh

    Thread Starter Member

    Aug 10, 2013
    268
    2
    Code ( (Unknown Language)):
    1. void setup()
    2. {
    3.   Serial.begin(9600);
    4. }
    5. void loop()
    6. {
    7.   if(Serial.available())
    8.   {if(Serial.findUntil("like","do"))
    9.   Serial.println("found");
    10.   else
    11.   Serial.println("not found");
    12.   }}
    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.
     
  3. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    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
    Code ( (Unknown Language)):
    1. Serial.readBytes(char,6)
    you need somehting like:
    Code ( (Unknown Language)):
    1. char a[6];
    2. Serial.readBytes(a,6)
     
    Last edited: Jan 2, 2014
    bhuvanesh likes this.
  4. bhuvanesh

    Thread Starter Member

    Aug 10, 2013
    268
    2
    Code ( (Unknown Language)):
    1. void setup()
    2. {
    3.   Serial.begin(9600);
    4. }
    5. void loop()
    6. {char a[10];
    7.   if(Serial.available())
    8.   {
    9.     char b=Serial.readBytes(a,10  );
    10.     Serial.println(a);
    11.   }
    12. }

    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
     
  5. DerStrom8

    Well-Known Member

    Feb 20, 2011
    2,428
    1,328
    How many times do you need to ask the same question? I think there are at least three threads here! You only need one.
     
    bhuvanesh likes this.
  6. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    Code ( (Unknown Language)):
    1.  
    2.  
    3. Noise? incorrect Baud rate? Line termination character (i.e. pressing enter)?
     
  7. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,399
    497
    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[].
     
    bhuvanesh likes this.
Loading...