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?

本文标签: