Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to read the value from PLC and display it in a form whenever the PLC Tag value changes.

There will list of tags which I need to monitor. Whenever a TAG value changes i need to call a function(different functions for each tag).

This is what I have done so far for capturing the Tag value change..

After connecting to PLC, i ll create the LIST of tags.

Read TAG values in timer.

While reading i ll check with OLDVALUES tag, if there is any change in value I ll raise an event.

This is working fine with 4 or 5 tags. When the Tag count is more, say 100, some of the Tag change events are not firing..

This is what so far I have done..

C#
public delegate void DataChangedEventHandler(string TagName, string NewValue);
   private Timer tmr = new Timer();

   public event DataChangedEventHandler OnDataChanged;
   private void StartTimer(DataTable dt)
   {
       AddTagList(dt);
       SetInitialVales();
       tmr.Tick += timerticks;
       tmr.Interval = 250;
       tmr.Enabled = true;
       tmr.Start();
   }
   private void StopTimer()
   {
       tmr.Enabled = false;
       tmr.Stop();
   }


I ll add the list of tags..
C#
private List<string> TagValues = new List<string>();
   private List<string> oldValues = new List<string>();
   private List<string> newValues = new List<string>();
   private void AddTagList(DataTable dt)
   {
       int ILoop = 0;

       foreach (DataRow row in dt.Rows)
       {
           TagValues.Add((string)row["Path"]);
           ILoop = ILoop + 1;
       }
   }


To set the initial values of the Tags.
C#
private void SetInitialVales()
    {
        int iLoop = 0;
        foreach (string vals in TagValues)
        {
            var rd = ReadTag(vals);
            oldValues.Add(rd.ToString());
            newValues.Add(rd.ToString());
            iLoop = iLoop + 1;
        }
        //newValues = oldValues
    }


and the main datachange part.
C#
private void timerticks(object sender, EventArgs eventArgs)
 {
     int iLoop = 0;
     foreach (string vals in TagValues)
     {
         oldValues[iLoop] = ReadTag(vals).ToString();
         if (oldValues[iLoop] != newValues[iLoop])
         {
             newValues[iLoop] = oldValues[iLoop];
             if (OnDataChanged != null)
             {
                 OnDataChanged(vals, newValues[iLoop]);
             }
         }
         iLoop = iLoop + 1;
     }
 }


My Queries:

1.What will happen if a event is raised while already raised event is still in progress(sub procedure is not completed)?? Is because of this reason I am missing some datachange events??

2.How to raise a raise a event automatically whenever the member of LIST value changes??

3.Any other better method to handle the timer-read-raiseevent?
Posted

1 solution

As a side note you may want to mention what type of PLC you are using (Siemens S7?) and what you are using to connect to it. You could have a direct Ethernet connection, a profibus or....

Anyway; the issue you are seeing is not new and i know i have encountered this approx 7 years ago. Now i don't know what the cause (or actually the real solution was) but i do know we could only get it working by having a fully configured OPC server as provided from the manufacturer on the PC that did the polling for us. The OPC Server in turn was doing the direct communication to the PLC (that was the magic part). The OPC client was monitoring the changes of the server and gave us the updates at a pre-configured rate.

Not the solution i wanted but sometimes it just needed to work yesterday.

Hope this helps.

Cheers,

AT
 
Share this answer
 
Comments
meetarun007 2-Apr-12 10:24am    
@Addy Tas : I am using Mitsubishi PLC and MXComponents(.NET version). There is no inbuilt Datachange event in MX. So i tried to create my own with timer, which works with small no of tags. but when it comes to more no of tags some events misses out. so only..
Addy Tas 2-Apr-12 13:34pm    
Well it seems we're down to offering suggestions... When there is no data changed event all you are left with is indeed polling. What may be a good suggestion is to stop the timer on the start of ontick and start it again when the ontick is about to end, that way you'll never be running the ontick multiple times on the same resource.
Hope this suggestion is working for you.

Cheers, AT

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