FSC Question

ASF

Lifetime Supporting Member
Join Date
Jun 2012
Location
Australia
Posts
3,921
Received this question in my inbox and posting it here so that others can also chip in:
I need to track data in a 5380 and then need it recallable from the HMI by searching by date. My thought was to use the FSC to search through the array that I will create where all of this data will be stored, elements within the array will be a UDT I’ll create for this specific project. There will be more then three elements within the UDT but for this question let’s just say I’ll have DINTs of, Day, Month, Year. My challenge that I’m mentally wrestling with here is that if the end user puts in the day, month and year from the HMI then wants to search the array using an FSC, am I able to use 3 different variables within the expression for searching the array? And if so, I actually would need to use those 3 variables and they would need to match 3 separate tags within the UDT. I hope that makes sense.

With that said, I wonder if it’s simpler for me to take the date information, which will be in DINT data type, then go DINT to STRING, then concatenate the STRINGS so when I need to search the array, it will be searching for one STRING tag that matches and not 3 separate DINT tags

It's been a while since I used the FSC now, but from memory you search argument can only check one variable. But you can check one variable and then manually check the others. Assume a UDT with members Year, Month, Day and Data. Assume an array of this UDT called MyUdtArray which contains your data. Assume an FSC with a Control tag called MyFSC. Assume a single instance of your UDT called SearchDate which contains the date you want to search for. Your expression would look something like:

Code:
MyUdtArray.Year[MyFSC.POS] = SearchDate.Year

Then when the .FD (found) bit comes on, you manually check to see if the month and day also match, and if not, clear the .IN (inhibit) and .FD (found) bits and let the FSC continue along to the next index.

There are all sorts of ways you could "enhance" this. You could:

- Concatenate the date into a string as you suggest and store that in the UDT each time you add an entry, then do the same with the operator-entered date
- Use math on the operator-entered date to turn it into an integer that LOOKS like a string representation of the date:
Code:
SearchValue = (SearchDate.Year * 10000) + (SearchDate.Month * 100) + SearchDate.Day
...then use the FSC expression:
Code:
(MyUdtArray[MyFSC.POS].Year * 10000) + (MyUdtArray[MyFSC.POS].Month * 100) + MyUdtArray[MyFSC.POS].Day = SearchValue
...so today's date (24th of March 2023) becomes the integer 20,230,324 and then the FSC does the same math on each member of your UDT array to see if it gets the same result

I'm sure others will have more ideas - plenty of ways to remove the hide from this feline.
 
I've used the approach suggested by ASF. My only add is that I would create another UDT element with the encoded date to avoid repeatedly calculating it in the FSC expression. It is also implied that the encoded date and search value be DINT data types, and this is essential to the approach.
 
I've used the approach suggested by ASF. My only add is that I would create another UDT element with the encoded date to avoid repeatedly calculating it in the FSC expression. It is also implied that the encoded date and search value be DINT data types, and this is essential to the approach.

So you would add another element within the UDT that would always have the calculation completed for the year, day and month so that the FSC expression would be simplified to something like...

Code:
(MyUdtArray[MyFSC.POS].DINTDate = SearchValue


...then use the FSC expression:
Code:
(MyUdtArray[MyFSC.POS].Year * 10000) + (MyUdtArray[MyFSC.POS].Month * 100) + MyUdtArray[MyFSC.POS].Day = SearchValue
...so today's date (24th of March 2023) becomes the integer 20,230,324 and then the FSC does the same math on each member of your UDT array to see if it gets the same result
 
So you would add another element within the UDT that would always have the calculation completed for the year, day and month so that the FSC expression would be simplified to something like...

Code:
(MyUdtArray[MyFSC.POS].DINTDate = SearchValue

Exactly. The trade-off being that slightly more memory is required, and, more importantly, the encoded DINTDate is updated if/when the date components are changed.

The other thing I will mention is to read up on the FSC instruction. It can be a little tricky to use, especially when you have more than one matching search item in the collection. Also, the RES (reset) instruction can be helpful when you are starting a new search.
 
Exactly. The trade-off being that slightly more memory is required, and, more importantly, the encoded DINTDate is updated if/when the date components are changed.

The other thing I will mention is to read up on the FSC instruction. It can be a little tricky to use, especially when you have more than one matching search item in the collection. Also, the RES (reset) instruction can be helpful when you are starting a new search.

Would you have a reason for doing it this way versus concatenating the strings and searching the array with the FSC that way? I assume the main reason would be the DINT method would take less memory, but I’m curious if there is another reason that I’m overlooking.
 
If you go the AB website and the Sample Code Library and search "Time and Date Math"
they have AOI's that do math on the Wall Clock values. This would allow you to calculate the number of days between two dates. If you have the oldest data in Array 0 and you enter your data daily. Then the number of days between Lookup date and Array 0 will be your array number to display. They call it T_DIFF.

Once your UDT array is Full then you'll need to COP Array1 to Array 0 for length to shift data up 1 position to always keep the oldest data in Array 0 and you have to shift your entry point for saved data every day even if that day is 0's.

simple example would be Array 0 = January 1st. you're looking for data from January 10th. 10 - 1 = 9 and since January 1st is in array 0 then the 10th is in Array 9. The AOI would calculate that difference up to +/-58 million years. At least that's what they said.
 
Last edited:
Another alternative is to use Structured Text. It's made for looping and it makes for easier to understand code for this sort of situation that is more portable across different PLC platforms. FSC seems to me to be a difficult to use brand-specific instruction. It and other similar instructions appear to be an awkward approach to doing something that ladder is not designed for. I avoid them as much as possible.

1131 has different languages for a reason.
 
It is also implied that the encoded date and search value be DINT data types, and this is essential to the approach.

Would you have a reason for doing it this way versus concatenating the strings and searching the array with the FSC that way?
I suspect (but this is just a guess) that Mispeld is (correctly) recalling that previous controllers can't do string compares; that is, you can't use an EQU operation on a string on 5370/5570 controllers or earlier. By extension, you also can't compare strings in an FSC expression.

However you're using a 5380 controller which can do string compares. Although, that does raise the question - can it do them inside an FSC expression? I've never tried! I have use strings in EQU instructions on those controllers, but not an FSC.
 

Similar Topics

Hello Please Help, I want to use a FSC instruction that will scan 20 DINT arrays. If the arrays are greater than 1000 display the value. How do I...
Replies
4
Views
1,763
Maybe this is just not possible, or maybe I am doing something wrong. Background; I have a data array of over 1500 products. For sorting I...
Replies
6
Views
759
I am trying to use the FSC to count how many numbers in my real array are less than a certain value but not sure why I cant for the life of me get...
Replies
11
Views
382
I have a FSC instruction that won't enable. I check tags and data type and can't find the problem. I'm trying to pull index numbers for data...
Replies
4
Views
731
I received the following message via PM, and am posting here publicly to help others. ============================================ I had a...
Replies
10
Views
1,019
Back
Top Bottom