Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi everyone!
I am reading integer value from PLC(you can think from file which periodically updated ) in every 5 seconds(or by a click a button) and displaying on the screen and use that value to do some processes. I have a problem that, if the user whom can directly control the PLC machine mistakenly reset(zeroing) the PLC value. How can I backup the last read value or How can I make user to select last read value among last read values list. What is the best practice to store last 10 or 20 values of the property. Can yo give me some advices ??

Best Regards
Posted
Comments
PIEBALDconsult 25-Jan-16 12:06pm    
In a stack maybe?
Sergey Alexandrovich Kryukov 25-Jan-16 12:06pm    
Is that your property or the property you can redefine? Then it would not be a problem. "Property" is a key here.
What is your problem, the storage or capturing the event of setting the value?
—SA
Ermany 25-Jan-16 14:47pm    
Hi Sergey !
Actually both of them is my problem but I will try to handle the storage problem by the help of OriginalGriff1 or ppolymorphe solutions I will try both of them. What would you suggest me to setting the value ?
BillWoodruff 25-Jan-16 15:52pm    
Interesting question.

"the user whom can directly control the PLC machine mistakenly reset(zeroing) the PLC value"

How do you detect this ? After the user resets it ?
Ermany 26-Jan-16 3:41am    
Actually user wanted me to do this. They said some times mistakenly I initialize the PLC value.For easy use at least not on the HMI screen but on your desktop application I want to see last read value(also user can write that value or can select from the last read vales list) + PLC current value. byt the way I am reading length of a stripe for quality control.I am not planning to write values to PLC.

The Queue<T> class would be a natural fit here, if it would only automatically dequeue when it reached it's limit!
But...It's easy to do:
C#
public class FixedQueue<T> : Queue<T>
    {
    private int _fixedSize = int.MinValue;
    public int FixedSize
        {
        get { return _fixedSize; }
        set
            {
            while (value < _fixedSize  && Count > value)
                {
                Dequeue();
                }
            _fixedSize = value;
            }
        }
    public FixedQueue(int fixedSize)
        : base(fixedSize)
        {
        _fixedSize = fixedSize;
        }
    public new void Enqueue(T item)
        {
        if (Count >= FixedSize)
            {
            Dequeue();
            }
        base.Enqueue(item);
        }
    }
 
Share this answer
 
Comments
Ermany 25-Jan-16 14:56pm    
Thank you so much for your detailed answer !
Best Regards
BillWoodruff 25-Jan-16 15:54pm    
I'm curious: in your code the 'setter of the Public 'FixedSize Property is never called; in what circumstances would the programmer set it ?

If I assume the programmer would set it to down-size the Queue, would it be necessary to call 'TrimExcess after the Queue had been trimmed ?
OriginalGriff 25-Jan-16 15:56pm    
If he wanted to change the number of elements it stored.
It's unlikely, but...
BillWoodruff 25-Jan-16 16:39pm    
Hi, Just added to my query a question about the possible requirement for a call to 'TrimExcess.

I note the MSDN docs on 'Queue constructed with a 'Size value are a little less than "transparent:"

"If the size of the collection can be estimated, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the Queue<t>."

cheers, your perennial (in fact, antiquated) student, Bill
OriginalGriff 25-Jan-16 17:06pm    
Yes, a Queue is implemented in the same way as a list: it's internally an array, and when it expands it's copied to a new array. Setting the initial size defines the original array dimension to avoid the overhead of repeated allocate-copy operations.
There's a description of it as a list here:
http://www.codeproject.com/Articles/870013/List-T-Is-it-really-as-efficient-as-you-probably-t
The Queue class avoids the Insert/Delete problems because it ask works from indexes and you can only Queue and Dequeue from the head and tail, so there's no shuffling of elements involved.
I would use an array to store the previous values.
In order to reduce The amount of work to manage the array, I would use it like a circular buffer, the advantage is that it avoid moving the data as the oldest value is replaced by a new one.
Circular buffer - Wikipedia, the free encyclopedia[^]
 
Share this answer
 
Comments
Ermany 25-Jan-16 14:54pm    
Thank you mate I will read about it and if I can do I will adapt it to my project
BillWoodruff 25-Jan-16 16:00pm    
fyi: a Queue is a data-structure manages an Array.

MSDN: "implements a generic queue as a circular array."
Ermany 26-Jan-16 3:51am    
Thanks for your interest.
I am using the code of Rob Blackbourn's "Circular Buffer in C#" post. Here is the link http://geekswithblogs.net/blackrob/archive/2014/09/01/circular-buffer-in-c.aspx
Best regards

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900