plc - Node OPCUA error when writing variable to siemens-powered OPCUA server (type is not defined) - Stack Overflow

I have an OPCUA client written with node-opcua, that can successfully connect and read all datatypes. T

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?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744989227a4604798.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信