Trying to understand the SoftwareSerial.find() function

What I know (or think I know) is that find() is a function of Stream, and Serial inherits from Stream, and SoftwareSerial inherits from Serial.

Here are the definitions:


In the SoftwareSerial reference page, find() doesn't figure in the list:
image

I can't understand what the terms "stream" or "serial buffer" mean. How does find() "read" data. This data that's coming from the stream/serial buffer, where does it come from? How and why is it produced?

The syntax says:
image
How do I know which "target" to give to the find() function? When would I decide to use the find() function?


Specifically, what does this line mean?:
image

I have a code that instantiates a SoftwareSerial object then calls the find() function through this object:

SoftwareSerial Object(....);
if(Object.find("OK"))

I don't know why the target is "OK", I don't know what does it mean for the find("OK") to return a true? where does it find the target? and why is this a condition in the first place? I don't understand why SoftwareSerial.find() is used in the first place.

Maybe an example will help.

The following will wait until the user has typed the string hi in the serial console.

Serial.println("Please say something.");
while (not Serial.find("hi"));
Serial.println("Found \"hi\".");

If the user types hello (followed by an enter), nothing will happen. If something like oh hi there (followed by an enter) is typed, the program will respond and end.

Here is an interactive example to play with.

No. Stream inherits from Print. SoftwareSerial inherits from Stream.

'Serial' is the name of an object of a class. Which class it is depends on which Arduino board you're using. On an Uno, Serial is an object of the HardwareSerial class - which inherits from Stream. On a Teensy 3.2, Serial is an object of the usb_serial_class class - which also inherits from Stream.

2 Likes

@jfjlaros
So the find function reads the string that's been entered in the serial monitor, is that it?
Also, in your example, I don't understand why there's a while loop instead of an if condition; I mean, we want to print "Found hi" if the serial monitor receives the string "hi", aren't we?

1. This is the conceptual view (Fig-1) of the Serial Buffer.


Fugure-1:

2. Assume that the following Newline terminated string is entered in the InputBox of the Serial Monitor.

I am OK 23

3. The characters of the string of Step-2 arrive at the UNO one-by-one and gets saved in the FIFO type Buffer of Fig-1 on interrupt basis and in the background.

4. The user wants that the data (23) of interest is after the substring OK of the arrived message. The find("OK") method helps to detect the OK substring and then returns true. The following sketch demonstrates the use of the Serial.find("OK") method.

char myData[50];

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

void loop()
{
  byte n = Serial.available();
  if (n != 0 )
  {
    bool m = Serial.find("OK");
    if(m == true)
    {
      byte p = Serial.readBytesUntil('\n', myData, 50);
      myData[p] = '\0';
      Serial.println(myData);
    }
  }
}

Output:

 23

myData[p] = '\0';
What does this line do?

What is this: '\0'?

I have no idea.

If I say that '\0' refers to a non-printable character and its ASCII code is 0000000b (0x00 in hex base and 0 in decimal), can you tell the name of this character from the following Table (Fig-1)?


Figure-1:

It terminates the array of chars with a zero which denotes the end of the string for functions needing to use the string

Let's see.. The first column has three zeros, and the first line has four zeros, so that's seven zeros, but then I don't see the "b" in "0000000b"...

That is just about the worst ASCII table that I have ever seen. All the information is there but it is very confusing to read unless you know your way around ASCII and if you do then you probably don't need the table

Try this one

So it's the NUL character..

That Table is not my creation. This is compact and colored -- many people like it.

How do you say that -- it's a NUL character? The Table of post #12 shows eight 0s (00000000); whereas, the NUL character has seven 0s (0000000 = 7-bit ASCII code) in the Table of post #9. So, which one is a correct Table?

Um no idea? how is this gonna help me connect to my ESP8266?

You have initiated the queries which have relation with your thread.

They both are! The 8-bit table shows the first half of the ASCII table. The "characters" from 128-255 are what I think was called the extended character set. That includes additional foreign language letters, mathematical symbols, commercial/trade symbols and the box drawing symbols used in many MS-DOS based text "window" menu systems.

It's always a train wreck when someone who doesn't know what they're doing gets sucked into one of @GolamMostafa's pedantic rabbit holes.

2 Likes

@bukhari_abdallah
Do you know the meaning of "rabbit hole"?