How to read structured datatype from PLC?|OPC UA Standard|Forum|OPC Foundation

Avatar
Search
Forum Scope


Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
Lost password?
sp_Feed sp_PrintTopic sp_TopicIcon
How to read structured datatype from PLC?
Avatar
Alexsandr Soul
Member
Members
Forum Posts: 3
Member Since:
12/04/2022
sp_UserOfflineSmall Offline
1
09/09/2024 - 04:51
sp_Permalink sp_Print sp_EditHistory

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();
}
}

Avatar
Randy Armstrong
Admin
Forum Posts: 1564
Member Since:
05/30/2017
sp_UserOfflineSmall Offline
2
09/09/2024 - 07:41
sp_Permalink sp_Print

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.

Avatar
Alexsandr Soul
Member
Members
Forum Posts: 3
Member Since:
12/04/2022
sp_UserOfflineSmall Offline
3
09/09/2024 - 22:28
sp_Permalink sp_Print

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

Avatar
Randy Armstrong
Admin
Forum Posts: 1564
Member Since:
05/30/2017
sp_UserOfflineSmall Offline
4
09/12/2024 - 12:46
sp_Permalink sp_Print

Then you should be able to read the DiagnosticCounts and you will get a ExtensionObject that contains the Diagnostics[Structure] in the Body.

Avatar
Alexsandr Soul
Member
Members
Forum Posts: 3
Member Since:
12/04/2022
sp_UserOfflineSmall Offline
5
09/15/2024 - 23:00
sp_Permalink sp_Print

Randy Armstrong said
Then you should be able to read the DiagnosticCounts and you will get a ExtensionObject that contains the Diagnostics[Structure] in the Body.

  

Is there some example of implementation in C#?

Avatar
Randy Armstrong
Admin
Forum Posts: 1564
Member Since:
05/30/2017
sp_UserOfflineSmall Offline
Forum Timezone: America/Phoenix
Most Users Ever Online: 510
Currently Online:
Guest(s) 20
Currently Browsing this Page:
1 Guest(s)
Top Posters:
Forum Stats:
Groups: 2
Forums: 10
Topics: 1434
Posts: 4854