Professional OPC
Development Tools

logos

How to get the OPC data type of the returned value (tag)?

More
29 Nov 2011 12:49 #667 by support
Note that starting with Version 5.12, there will be extension methods that allow getting property values already casted to proper datatype. This will allow making code like yours be written in a shorter way.

Please Log in or Create an account to join the conversation.

More
10 Oct 2011 04:02 #526 by julian
Thanks for your help! Got it all working!

Please Log in or Create an account to join the conversation.

More
09 Oct 2011 10:56 #525 by support
I have prepared two examples
The first example obtains the data type through OPC property:
// This example shows how to obtain a data type of an OPC item.

using System;
using OpcLabs.EasyOpc;
using OpcLabs.EasyOpc.DataAccess;

.....
var easyDAClient = new EasyDAClient();

// Get the value of DataType property; it is a 16-bit signed integer
var dataType = (short)easyDAClient.GetPropertyValue("", "OPCLabs.KitServer.2", "Simulation.Random",
DAPropertyId.DataType);
// Convert the data type to VarType
var varType = (VarType)dataType;

// Display the obtained data type
Console.WriteLine("DataType: {0}", dataType); // Display data type as numerical value
Console.WriteLine("VarType: {0}", varType); // Display data type symbolically

// Code below illustrates how decisions can be made based on type
switch (varType)
{
case VarType.R8:
Console.WriteLine("The data type is VarType.R8, as we expected.");
break;

// other cases may come here ...

default:
Console.WriteLine("The data type is not as we expected!");
break;
}
.....
The second example reads an item and interprets the data type of the item value:
// This example shows how to read a single item and obtains a type code of the received value.

using System;
using OpcLabs.EasyOpc.DataAccess;

.....
var easyDAClient = new EasyDAClient();

DAVtq vtq = easyDAClient.ReadItem("", "OPCLabs.KitServer.2", "Simulation.Random");

TypeCode typeCode = Type.GetTypeCode(vtq.Value.GetType());

Console.WriteLine("TypeCode: {0}", typeCode);
.....

Please Log in or Create an account to join the conversation.

More
07 Oct 2011 00:54 #521 by julian
I've tried what you've suggested. By using different methods I get:"object" or "VarType" or "1" or "OpcLabs.EasyOpc.VarType"OpcLabs.EasyOpc.DataAccess.DAItemDescriptor item = itemIdArray[index];item.RequestedDataType.GetType();returns "VarType"Type.GetTypeCode(tag.GetType()).ToString();returns "Object"item1.RequestedDataType.GetType().ToString();returns "OpcLabs.EasyOpc.VarType"

Please Log in or Create an account to join the conversation.

More
06 Oct 2011 19:50 #520 by support
If you want to get that information before you actually operate invoke some reading or writing on the tag, you can use the GetPropertyValue method, passing in DAPropertyId.DataType. You will get back one of the constants defined in VarType type.
If you have read the value already and want to figure out what it is, there are multiple ways how to do that. You can e.g. do .GetType() on the value and then compare it to a specific type, such as typeof(int), or do Type.GetTypeCode(value.GetType()), and then compare it to one of the TypeCode enumeration members (ms-help://MS.MSDNQTR.v90.en/fxref_mscorlib/html/ec83d2c5-2704-dbbc-4fdb-ac5759b7940e.htm ). You can also use the 'as' operator to try cast the value to what your code actually needs.

Please Log in or Create an account to join the conversation.

More
06 Oct 2011 14:58 #519 by julian
How to get the OPC data type of the returned value (tag)? For Example: DWord, String, Date, Boolean etc.Thanks for your help.

Please Log in or Create an account to join the conversation.

Moderators: support
Time to create page: 0.258 seconds

      

 Recommend this on Google