Searching Circular Buffer - Any Advice?

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 7.

Moderator: Benj

Post Reply
chipfryer27
Valued Contributor
Valued Contributor
Posts: 652
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 202 times
Contact:

Searching Circular Buffer - Any Advice?

Post by chipfryer27 »

Hi All

I'm just about to start a little project that will require the use of the RS232 component to receive data. I'm pretty confident of using an interrupt to signal when data is present, and from examples it seems I should be feeding this data into a Circular Buffer for processing. All good so far.

I have simulated a CB and populated it with random data and I've retrieved data too. I have also used the "Lookforvalue" function and I can determine if the buffer contains a specific value(s). This is all just "playtime" to familiarise myself with things before I start on the project.

I will be sending data to the RS232/CB and I can structure this data any way I wish. I imagine I will need a way to identify the start of my data followed by an "ID" and "Status", these could just be three bytes or the like (start,ID,status). The length of the "packet" won't change, just the second and third bytes (start would be a unique identifier and I do appreciate that I will probably end up using a more complex packet but that isn't my issue at present).

What would be the best way to search and extract this data from a CB? The "Lookforvalue" would only be able to identify non changing data such as the "start" byte and it doesn't provide any means to return found position within the CB to enable me to directly grab the following two bytes that I care about.

I can create a loop that extracts each byte from the buffer one byte at a time and I can test this byte to see if it is my unique "Start" value and if so go from there, but it seems a bit rough and ready. Has anyone got a better idea of searching the CB etc? Is there a way to first identify if and where in the buffer a target is located and then extract it?

I've looked through posts but I haven't quite seen anything similar so any pointers appreciated.

Thanks in advance.

mnf
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Searching Circular Buffer - Any Advice?

Post by mnf »

Hi,

Did you make any progress with this?

I think the approach needed may vary - how is data added to the buffer (on an interrupt or after a macro call for example). Is data before the start byte 'junk' - in which case reading bytes using GetByte and checking the value would be a valid approach..

Alternatively the 'LookforValue' or 'WaitForValue' may offer an inroad - it's a pity they just return true or false rather than the Index of the find.

The buffer is FCV_01211_CircularBuffer1__DATA and you could access it directly using C - (and search directly or using substr) - but it would be tricky (remember to wrap around at the end of the buffer). Using 'GetIndexedByte' in a loop would give an easier route into this!

Give us some more details of what you are searching for etc - and I'll try to produce a more definite code 'snippet' that searches for it!

Martin

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Searching Circular Buffer - Any Advice?

Post by Benj »

Hello,

Examples 1 and 2 on this page might help using the finite state machine approach.
https://www.matrixtsl.com/wikiv7/index. ... ateMachine

To improve the functionality you could add a checksum byte based on the variable data bytes and then check this is valid before performing the function.

The circular buffer is still useful as it can potentially buffer several commands until you get around to parsing through the incoming data. This is the basis of the Matrix AllCode functionality.

chipfryer27
Valued Contributor
Valued Contributor
Posts: 652
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 202 times
Contact:

Re: Searching Circular Buffer - Any Advice?

Post by chipfryer27 »

Hi Guys

Thanks for responding. Unfortunately I am traveling again and won't be able to actually do much until next month, other than look at examples and figure out best ways.

Martin, as you correctly guessed I don't care about any data until I reach my "start" byte, it can all be discarded. I have to admit though that I'm not brilliant in C which is why I and many others use Flowcode, but I appreciate the snippets. Very helpful in understanding things more.

Much as in one of the example files I looked at, I plan on using the Rx Interrupt to fill the buffer as necessary and then process once the incoming transmission is complete. I am not expecting a great deal of transmission and I will be transmitting at a very low speed so I am sure I can process before another transmission appears / buffer gets full etc. Once transmission stops I will probably use the "lookforvalue" as you suggest and if false flush the buffer. If true then I will proceed to process.

Benj, thanks for the links to the state machine and at first glance looks similar to what I was thinking. An identifier and a variable. I will look at this further and also the other example files.

At present I am only looking at a three-byte packet but this is highly likely to change as things progress. However if I get this searching correct now, it can easily scale up. First byte would identify the start of transmission, other than that it has no value to me, the next two bytes would mean something much like "L, 255" in Benj's state machine example. As the identifier won't change but the following two bytes will, I thought it best to first search the buffer to see if the identifier is there, if so then look through the buffer until I find my identifier byte, then I know the next two values are my "data", rather than look for every possible permutation of those two data bytes.

Thanks to both for your input, and from it I don't think I'm too far off track in my approach (unless of course, you know better, to quote "That's Life". I really am showing my age now <s>).

Regards

Post Reply