View Single Post
Old March 14th, 2019, 04:45 PM   #5
Lifetime Supporting Member

ASF is offline
Join Date: Jun 2012
Location: Australia
Posts: 2,884
Interesting problem!

Are the strings coming in a regular, repeatable, consistent intervals? Or could you have ten strings in a five-second period, and then only two strings in the next five second period?

If they're regular, it's a bit simpler, but I'm going to guess they're probably not - and even if they are, there's likely no way of guaranteeing that it will always be that way. So it makes sense to assume the worst and program accordingly.

As Ken said, we'll need to know what PLC/platform you're using to do this, but here's how I'd approach it in a Logix PLC. For another platform, the same basic premise should hold true, but the instructions and terminology will be a little different.

First things first, you'll need an array of strings to store your incoming strings. Work out the absolute maximum number of strings you can possibly receive in any 15-second period. Then double it, and make an array that size. If your strings are a fixed length, you can make a custom string data type with a shorter length to save memory (a string by default reserves space for 82 characters).

Then you'd use an FFL and FFU to load them in and out of the array. Adding them in is easy: each time you see a new string, you use an FSC to compare it to the existing array strings, then an FFL to add it into the array.

Unloading from the array is a bit trickier. Normally when you're doing things like this, you're checking against the last 15 strings, not all the strings in the last 15 seconds. If you were doing the former, it's a piece of cake - each time you get a new string, you do a compare, unload the last (15th) string from the array, and add the new one in. But here, you're going to have to have some way of timestamping the values, and periodically checking to see if any have exceeded the 15-second "lifetime" and thus need removing.

There are several ways you could do this. You could use the T_clock and T_add add-on instructions from the RA sample library, or you could use an array of timers to match your array of strings. You could streamline somewhat by creating a UDT consisting of a string and a timer, and then creating an array of that UDT. In any case, indirect addressing and loops are your friend here. Remember, you don't need to check every single string in your array to see if it's "expired" - start your check with the oldest one in the array, and as soon as you find one that hasn't expired, you know that none of the remaining ones will have either.

Hopefully that gets a few ideas bouncing around!
  Reply With Quote