Documentation Center

  • Trial Software
  • Product Updates

Work with Different Data Types

Conversion Between MATLAB Data Types and COM Variant Data Types

The OPC Data Access Standard uses the Microsoft® COM Specification for communication between the OPC server and OPC client. A significant amount of the data exchanged between the OPC server and the client is the value from a server item or the value that a client wants to write to a server item. The Microsoft COM Specification uses Microsoft Variants to send different data types between the client and server. This section discusses how OPC Toolbox™ software converts MATLAB® data types to COM Variants when writing values, and COM Variants to MATLAB data types when reading values.

OPC servers require all values to be written to server items in COM Variant format. The server also provides the toolbox with COM Variants when an item's Value property is read or returned by the server. The toolbox automatically converts between the COM Variant type and MATLAB data types according to the table shown below.

Table 8-1. Conversion from MATLAB Data Type to COM Variant Data Type

MATLAB Data Type

OPC Server Data Type (COM Variant Type)

Remarks

double

VT_R8

single

VT_R4

char

VT_BSTR

logical

VT_BOOL

uint8

VT_UI1

uint16

VT_UI2

uint32

VT_UI4

uint64

VT_UI8

int8

VT_I1

int16

VT_I2

int32

VT_I4

int64

VT_I8

function_handle

N/A

Not allowed

cell

N/A

Not allowed

struct

N/A

Not allowed

object

N/A

Not allowed

N/A

VT_DISPATCH

Not allowed

N/A

VT_BYREF

Not allowed

double

VT_EMPTY

Returns the empty matrix ([])

Conversion of Values Written to an OPC Server

When you write values to the OPC server using the write or writeasync function, you can provide any MATLAB data for the write operation. When you write data to an OPC server, the following data conversions take place:

  1. OPC Toolbox software converts the value into the equivalent COM Variant according to Table 8-1. If any disallowed data type is encountered (for example, if you attempt to write a MATLAB structure), an error will be generated.

  2. The COM Variant is sent to the OPC server.

  3. The OPC server will attempt to convert the COM Variant to the server item's canonical data type, using COM Variant conversion rules. If the conversion fails, the server will return an error.

Conversion of Values Read from an OPC Server

When an OPC server returns values for a server item to MATLAB, the OPC server will first convert the value to the COM Variant equivalent of the data type specified by the daitem object's DataType property. If the conversion fails, an error message is returned with the value. When OPC Toolbox software receives the value, the COM Variant is converted to the equivalent MATLAB data type according to Table 8-1.

Handling Arrays for Item Values

The OPC Specification supports arrays of values being written to a server item, and read from a server item. However, a specific server item may not accept an array of values. The behavior of the server in that case is server-dependent. For example, one server may use only the first value of the array. Another server may return an error when attempting to write an array of values to a server item that only supports a scalar value. OPC Toolbox software is not able to determine if a server item accepts only scalar values.

For all of the data types listed in Table 8-1 that can be converted between MATLAB and a COM Variant, scalar and array data are permitted by the toolbox. However, the OPC Specification supports only one-dimensional arrays of data. Higher dimension MATLAB arrays are flattened into a one-dimensional vector when writing data to the OPC server.

Was this topic helpful?