Unable to write to tag with node-opcua (BadTypeMismatch)

1.6k Views Asked by At

I'm using node-opcua to write a boolean value to set a reset tag. Here's my code:

    var nodesToWrite = new Array();
    nodesToWrite.push({
        nodeId: 'ns=2;s=Paint.PLC.Reset_Auto_Blocked_Time',
        attributeId: opcua.AttributeIds.Value,
        indexRange: null,
        value: {
            value: {
                dataType: opcua.DataType.Boolean,
                value: true
            }
        }
    });
    self.uaSession.write(nodesToWrite, function (err, statusCode, diagnosticInfo) {
        if (!err) {
            console.log(" write ok");
            console.log(statusCode);
            console.log(diagnosticInfo);
        } else {
            console.log(" write err = ", err);
        }
   })

It doesn't actually call "err" because the console logs this:

[{ [Number: 2155085824
   value: 2155085824,
   description: 'The value supplied for the attribute is not of the same type as the attribute\'s value.',
name: 'BadTypeMidmatch' }]
[]

However, that is clearly an error and the write is never completed. The tag is set in KEPServer as a boolean and works fine. I'm not sure why it's saying it's a mismatch. Any help?

3

There are 3 best solutions below

0
On

Just use "Boolean" in value.value.dataType

Exemple:

   nodesToWrite.push({
        nodeId: 'ns=2;s=Paint.PLC.Reset_Auto_Blocked_Time',
        attributeId: opcua.AttributeIds.Value,
        indexRange: null,
        value: {
            value: {
                dataType: "Boolean",
                value: true
            }
        }
    });
0
On

There is an option to read builtInType of the nodes

const nodeToRead = {
   nodeId: 'ns=2;s=Paint.PLC.Reset_Auto_Blocked_Time',
   attributeId: opcua.AttributeIds.DataType,
};

const dataValue = await self.uaSession.read(nodeToRead);
// check IdentifierType(Numeric) and identifierNumeric(1 to 25) of dataType 
// E.g: Boolean - 1, String - 12, Int16 - 4 , etc


// Fill nodesToWrite depending on the builtInType 
const nodesToWrite = [];
nodesToWrite.push({
    nodeId: 'ns=2;s=Paint.PLC.Reset_Auto_Blocked_Time',
    attributeId: opcua.AttributeIds.Value,
    indexRange: null,
    // check builtInType-type and fill nodesToWrite                 
});

await self.uaSession.write(nodesToWrite);
0
On

Looks like opcua.DataType.Boolean is not the expected type.

I would perform a read of the variable first to validate what dataType is set:

var nodeToRead = {
    nodeId: 'ns=2;s=Paint.PLC.Reset_Auto_Blocked_Time',
    attributeId: opcua.AttributeIds.Value,
};
self.uaSession.read(nodeToRead , function (err, dataValue) {
    if (!err) {
        console.log(" read ok");
        console.log(dataValue.toString());
    } else {
        console.log(" read err = ", err);
    }
});