admin管理员组文章数量:1406926
I have an OPCUA client written with node-opcua, that can successfully connect and read all datatypes. The problems begin when I need to write into a node. Few datatypes, like Int (all types) and Bool, are handled by the logic, others (like STRING and BYTE) won't work. The main logic to write into nodes is the following:
app.post("/api/write-node", ensureOpcuaSession, async (req, res) => {
const { nodeId, value, dataType = "String" } = req.body;
if (!nodeId) {
return res.status(400).json({ error: "nodeId is required" });
}
if (value === undefined) {
return res.status(400).json({ error: "value is required" });
}
try {
console.log(
`Writing to node ${nodeId}: value=${JSON.stringify(
value
)}, dataType=${dataType}`
);
const session = req.opcuaSession;
// Determine the actual data type to use
let opcuaDataType;
switch (dataType) {
case "Boolean":
opcuaDataType = DataType.Boolean;
break;
case "Byte":
opcuaDataType = DataType.Byte;
break;
case "SByte":
opcuaDataType = DataType.SByte;
break;
case "Int16":
opcuaDataType = DataType.Int16;
break;
case "UInt16":
opcuaDataType = DataType.UInt16;
break;
case "Int32":
opcuaDataType = DataType.Int32;
break;
case "UInt32":
opcuaDataType = DataType.UInt32;
break;
case "Int64":
opcuaDataType = DataType.Int64;
break;
case "UInt64":
opcuaDataType = DataType.UInt64;
break;
case "Float":
opcuaDataType = DataType.Float;
break;
case "Double":
opcuaDataType = DataType.Double;
break;
case "String":
opcuaDataType = DataType.String;
break;
case "DateTime":
opcuaDataType = DataType.DateTime;
break;
default:
opcuaDataType = DataType.String;
}
// Determine if it's an array
const isArray = Array.isArray(value);
const arrayType = isArray
? VariantArrayType.Array
: VariantArrayType.Scalar;
// Write the value to the node
const statusCode = await session.write({
nodeId,
attributeId: AttributeIds.Value,
value: {
value: {
dataType: opcuaDataType,
arrayType: arrayType,
value: value,
},
},
});
return res.json({
success: true,
nodeId,
statusCode: statusCode.toString(),
});
} catch (err) {
console.error(`Error writing to node ${nodeId}:`, err.message);
return res.status(500).json({ error: err.message });
}
});
I searched around, but I can't find any valuable information that can help in this case. I think the approach would be to define "custom" datatypes, but the documentation does not specify how to do that. Additionally, the OPCUA server does not expose anything in the Datatypes nodes. What's the best approach?
本文标签:
版权声明:本文标题:plc - Node OPCUA error when writing variable to siemens-powered OPCUA server (type is not defined) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744989226a2636279.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论