Go Down

Topic: Peek more than one character (HardwareSerial) (Read 19 times) previous topic - next topic


I have seen another post like this but it did not quite address the problem. I have created a library to insert in other peoples code. At the top of their loop, they will call a function from my library.

My function needs to look at what is in the serial buffer to see if "#Reset#" or "#Reload#" or any other predefined string is present. If the string is there, get it off the buffer and call the appropriate function in my library or perhaps return something so they can select their own function to call. If the strings are not present, leave everything in the buffer and simply return so the rest of their program can read the buffer and do whatever with it as if my code was not even there. Although I am looking for a string in the serial buffer, the rest of the time the data could be binary or whatever.

The main thing here is that unless the string is in the serial buffer, their code should behave like my function was never called. Also, I do not want to require the user to modify their existing library files or change their code because "Serial" has changed. They should basically be able to add my library and put my function call at the top of their loop. I don't mind if they have to add a little something at the top of setup or something as well. I also don't mind requiring them to have a newer version of Arduino.

I could see passing an array into "peek" so it could be filled with what is in the buffer and return the number of bytes copied (up to a size limit passed in). Or maybe another peek function that moves the pointer each time you call it with a way to reset it. I've even thought about somehow reassigning "Serial" to be a new HardwareSerial class with an extension that provides my new peek function. I have not got that to work besides I suspect serialEvent would be affected.

Any suggestions would be appreciated. In the event that a developer who submits changes to the Arduino library is reading this, the idea of passing an array into "peek" seems like a good solution if nobody else has a better idea that does not involve changing the library. The problem I see with a moving peek is that you would also need a moving available. You can't return a -1 if the last byte in the buffer is reached because -1 is a perfectly valid number if the data is binary.



My sense is that you are trying to detect the data at the wrong point in the process. Why not read all the incoming data into a buffer and then look for particular sequences of characters at your leisure. That is the underlying concept in Serial Input Basics

The HardwareSerial buffer can only hold 64 bytes and the sort of sequences you are looking for take up a lot of that - especially if data continues to arrive while you are checking for a sequence.

What is the purpose of the library you are developing?

Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up