Why is the OPC Read block in the OPC Toolbox unable to read a Boolean 1?

2 views (last 30 days)
The OPC Read block is not able to read a Boolean 1. It reads it as a value of -1.
Even though I have found a workaround in the Unary Minus block, I would like to know why the OPC Read block does not support reading a Boolean variable.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
The OPC specification has a note about Boolean values:
"Note that the internal storage type of VT_BOOL is ‘short’, which must have the values VARIANT_TRUE (0xFFFF – i.e., ‘-1’ when stored in a ‘short’) and VARIANT_FALSE (0). When converting TO bool, any nonzero value converts to VARIANT_TRUE. Converting FROM bool to any signed numeric type, VARIANT_TRUE converts to ‘-1’ or ‘-1.0’. For unsigned types it converts to the maximum value. The recommended OPC standard for conversion of bool to BSTR is '0' or '-1', rather than ‘True' or 'False'..."
So, you should get a -1 if using any of the signed data types for the OPC Read block. This is the behavior for doubles and for int8. For uint8 data types, the behavior is to output the maximum value (255) as dictated by the OPC specification.
Using a unary minus block will work, but if you want a boolean, insert a Data Type Conversion block, and set the data type to "boolean".

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!