OPC Studio User's Guide and Reference
Data Types in OPC Classic
Advanced Topics > Data Types > Data Types in OPC Classic

OPC Data Access specification is based on COM, and uses Windows VARIANT type (from COM Automation) for representing data values.

Note: Some OPC servers even use certain VARIANT types that are not officially supported by Microsoft.

Microsoft .NET has a different concept, and all data is represented using an Object type. Conversions between the two are available, but not always fully possible.

In addition, not everything that can be stored in an Object can later be processed by all .NET tools and languages. Microsoft has created so-called Common Language Specification (CLS), which has certain rules and restrictions that, if followed, guarantee cross-language compatibility.  Public QuickOPC.NET components (assemblies) are fully CLS compliant, and that includes the way the data types are converted to and from OPC types.

If you inspect the conversions, you will notice that most of them have to do with unsigned integer types (except Byte). This is because there are .NET languages that do not have such types, and cannot process them. A typical example of such example in frequent use is VB.NET.

QuickOPC.NET converts data from COM to .NET according to following table:

COM type (VARIANT) .NET type (Object)
VT_EMPTY System.Object (null reference)
VT_NULL System.DBNull (singleton class)
VT_I2 System.Int16
VT_I4 System.Int32
VT_R4 System.Single
VT_R8 System.Double
VT_CY System.Decimal
VT_DATE System.DateTime
VT_BSTR System.String
VT_DISPATCH System.Object (not tested)
VT_ERROR System.Int32
VT_BOOL System.Boolean
VT_VARIANT converted type of the target VARIANT
VT_DECIMAL System.Decimal
VT_I1 System.Int16
VT_UI1 System.Byte
VT_UI2 System.Int32
VT_UI4 System.Int64
VT_I8 System.Int64
VT_UI8 System.Decimal
VT_INT System.Int32
VT_UINT System.Int64
VT_ARRAY | vtElement System.Array of the converted vtElement type

Types that are highlighted do not convert from COM to their “natural” .NET counterparts, because the corresponding .NET type is not CLS compliant. Instead, a “wider” type that is CLS compliant is chosen.

Types not listed in the above table at all are not supported.

Strings are internally represented in Unicode wherever possible.

QuickOPC-COM is meant to be used from applications based on COM Automation, and in general, any valid VARIANT can be processed by such application. Some automation tools and programming languages, however, have restrictions on types of data they can process. If your tool does not support the data type that the OPC server is using, without QuickOPC, you would be out of luck.

In order to provide the ability to work with widest range of OPC servers and the data types they use, QuickOPC-COM converts some data types available in OPC. We have made a research into the data types supported by various tools, and QuickOPC-COM uses a subset of VarType types that is guaranteed to work in most tools that are in use today (one of the most restrictive languages appears to be VBScript).

Note that the QuickOPC-COM only converts the data that it passes to your application – either in output arguments of property accessors or methods, or input arguments in event notifications. In the opposite direction, i.e. for data that your application passes to QuickOPC-COM, we use very “relaxed” approach, and accept the widest range of possible data types.

QuickOPC-COM converts data from OPC Data Access according to following table:

VARTYPE in OPC Data Access VARTYPE In QuickOPC-COM
VT_EMPTY VT_EMPTY
VT_NULL VT_NULL
VT_I2 VT_I2
VT_I4 VT_I4
VT_R4 VT_R4
VT_R8 VT_R8
VT_CY VT_CY
VT_DATE VT_DATE
VT_BSTR VT_BSTR
VT_DISPATCH VT_DISPATCH
VT_ERROR VT_R8
VT_BOOL VT_BOOL
VT_VARIANT VT_VARIANT
VT_DECIMAL VT_DECIMAL
VT_I1 VT_I2
VT_UI1 VT_UI1
VT_UI2 VT_I4
VT_UI4 VT_R8
VT_I8 VT_R8 (may lose precision)
VT_UI8 VT_R8 (may lose precision)
VT_INT VT_I4
VT_UINT VT_R8
VT_ARRAY | vtElement VT_ARRAY | VT_VARIANT(vtElement) *(see note below)

Types that are highlighted are converted to a different data type. If a precise match does not exist, a “wider” type is chosen.

Types not listed in the above table at all are not supported.

Strings are internally represented in Unicode wherever possible.

If you want to determine the data type of a variable, do not use the type of the returned value for that. Instead, get and interpret its DataType property.

 

See Also

Examples - OPC Data Access