12/04/2022
Hi!
I have a solution in C#, PLC Mitsubishi FX5U and Melfosoft MX OPCUA Server.
I can read data from PLCs of standard types, but I have a question:
How do I read the whole structure from a PLC? How to read an array from a structure in PLC? How to write structure data to PLC?
Here is an example of how I read data from PLC -
1. Cration subscription:
private void ReadSubscription(Session session, Subscription subscription)
{
if (session == null || session.Connected == false) return;try
{
session.AddSubscription(subscription);
subscription.Create();MonitoredItem LiveCntItem = new MonitoredItem(subscription.DefaultItem);
LiveCntItem.StartNodeId = new NodeId("ns=4;s=Address Space.FX5U.Data.LiveCnt");
LiveCntItem.AttributeId = Attributes.Value;
LiveCntItem.DisplayName = "LiveCnt";
LiveCntItem.SamplingInterval = 1000;
LiveCntItem.QueueSize = 10;
LiveCntItem.DiscardOldest = true;
LiveCntItem.Notification += OnMonitoredItemNotification;
subscription.AddItem(LiveCntItem);MonitoredItem SystemRunningItem = new MonitoredItem(subscription.DefaultItem);
SystemRunningItem.StartNodeId = new NodeId("ns=4;s=Address Space.FX5U.Data.SystemRunning");
SystemRunningItem.AttributeId = Attributes.Value;
SystemRunningItem.DisplayName = "SystemRunning";
SystemRunningItem.SamplingInterval = 1000;
SystemRunningItem.QueueSize = 10;
SystemRunningItem.DiscardOldest = true;
SystemRunningItem.Notification += OnMonitoredItemNotification;
subscription.AddItem(SystemRunningItem);MonitoredItem Result1Item = new MonitoredItem(subscription.DefaultItem);
Result1Item.StartNodeId = new NodeId("ns=4;s=Address Space.FX5U.Data.Result1");
Result1Item.AttributeId = Attributes.Value;
Result1Item.DisplayName = "Result1;
Result1Item.SamplingInterval = 1000;
Result1Item.QueueSize = 10;
Result1Item.DiscardOldest = true;
Result1Item.Notification += OnMonitoredItemNotification;
subscription.AddItem(Result1Item);subscription.ApplyChanges();
}
catch (Exception ex)
{
Log.Information(ex.ToString());
Disconnect();
}
2. Monitor and event that data has been updated:
void OnMonitoredItemNotification(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
{
try
{
MonitoredItemNotification change = e.NotificationValue as MonitoredItemNotification;
if (change == null) return;List changes = new List();
changes.Add(change);foreach (var item in changes)
{
switch (monitoredItem.DisplayName)
{
case "LiveCnt":
_data.LiveCnt = (short)item.Value.Value;
break;
case "SystemRunning":
_data.SystemRunning = (bool)item.Value.Value;
break;
case "Result1":
_data.Result1 = (short)item.Value.Value;
break;
}
}DataUpdated?.Invoke();
}
catch (Exception ex)
{
Log.Information(ex.ToString());
Disconnect();
}
}
12/04/2022
Randy Armstrong said
If you see multiple Variables when you are browsing then you must subscribe to each Variable individually.If there is a single Variable with a Structure value then you can read the entire Structure as once.
What you can do depends on the functionality provided by the Server.
Can you tell me how?
For example: I have a Diagnostics structure that stores several integer variables:
Diagnostics[Structure]
1. ProductCount [UINT]
2. ProductGoodCount [UINT]
3. ProductBadCount [UINT]
4. PusherCount [UINT]
In the PLC/OPC Server it appears as - DiagnosticCounts with data type Diagnostis
1 Guest(s)