惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
J
Java Code Geeks
IT之家
IT之家
Last Week in AI
Last Week in AI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
B
Blog RSS Feed
P
Proofpoint News Feed
博客园 - Franky
MongoDB | Blog
MongoDB | Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 【当耐特】
Microsoft Security Blog
Microsoft Security Blog
I
InfoQ
B
Blog
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
博客园 - 聂微东
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
U
Unit 42
aimingoo的专栏
aimingoo的专栏

博客园 - dirgo

springboot里的application.yml文件的logging:下的配置 Flutter的SDK国内下载方法及AndroidStudio配置打包方法 minio数据迁移方法 [转]flutter入门 Elasticsearch介绍以及在spring项目里的使用方法 moxa NPort 5100 Linux real tty驱动安装 VirtualBox 的网络模式 Oracle关联表删除数据和更新数据 logback-spring.xml引入了base.xml日志文件默认生成在哪里? application.yml中Logback相关配置 org/springframework/boot/logging/logback/defaults.xml的内容和作用 logback-spring.xml 中<include resource="org/springframework/boot/logging/logback/base.xml"/>作用 Spring Boot 项目 Logback 日志框架配置文件logback-spring.xml详解 Oracle DataBaseLink 提示密码过期 梅特勒托-托利多衡器串口连续输出数据格式的说明 utf-8编码详解 mybaits updateWrapper更新报错:无效的列类型: 1111 @RequestPart和@RequestParam有什么区别 如何避免Mass Assignment漏洞? 关于vxe-table v4 导出大数据量excel浏览器卡死问题的解决 开源的Linux换源和安装docker脚本 wsl常用命令 使用网页静默打印 在Oracle中,授予用户角色和权限 Linux下查看当前哪些端口在监听状态,哪些端口在连接状态 mobaxterm常用配置 Windows下udp工具 Oracle表空间用户授权创建dblink等操作 用nmap扫描找出某个网段下空闲的ip脚本 Oracle 19c 常用运维 SQL
Eclipse Milo 处理PLC&quot;字(Word)&quot;类型,最直接和正确...
dirgo · 2026-01-08 · via 博客园 - dirgo

在 Eclipse Milo 这个 OPC UA 库中,向一个 UInt16 类型的数据点写入数据,最直接和正确的做法是使用其内置的 UShort 类型。

为了方便你快速理解和上手,我整理了下面的核心信息表:

操作环节 OPC UA 类型 Milo 中的对应类型 核心写入方法/类
写入 UInt16 UShort new Variant(new UShort(value))Unsigned.ushort(value)

📝 详细写入步骤

在实际代码中,向一个 UInt16 节点写入值通常包含以下步骤:

  1. 创建 UShort 对象:将你的整数值(如 500)包装成 UShort 对象。
  2. 封装为 Variant:OPC UA 通信使用 Variant 作为通用数据容器。
  3. 构造 DataValue:将 Variant 封装进 DataValue,并可以设置时间戳、状态码等。
  4. 执行写入:通过节点的 writeValue 方法写入。

以下是一个完整的代码示例,展示了如何向节点 ns=2;s="YourUInt16NodeId" 写入值 500

import org.eclipse.milo.opcua.stack.core.types.builtin.*;
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort;
import org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode;
import java.util.concurrent.ExecutionException;

// ... 省略客户端连接等初始化代码

// 1. 指定要写入的节点
NodeId nodeId = new NodeId(2, "YourUInt16NodeId");
UaVariableNode node = client.getAddressSpace().createVariableNode(nodeId);

// 2. 创建要写入的值(例如 500),并包装为 UShort
// 方法一:直接构造 UShort
// UShort uShortValue = new UShort(500);
// 方法二:使用 Unsigned 工具类(效果相同)
 UShort uShortValue = Unsigned.ushort(500);

// 3. 将值封装进 Variant 和 DataValue
Variant variant = new Variant(uShortValue);
DataValue dataValue = new DataValue(variant, null, null);

// 4. 执行写入操作
try {
    StatusCode status = node.writeValue(dataValue).get();
    if (status.isGood()) {
        System.out.println("写入 UInt16 成功。");
    } else {
        System.out.println("写入失败,状态码: " + status);
    }
} catch (InterruptedException | ExecutionException e) {
    e.printStackTrace();
}

🔍 重要说明与建议

  • 为什么是 UShort? 由于 Java 语言本身不支持无符号整数类型,Milo 库提供了 UShortUIntegerULong 等类来完整表示 OPC UA 的无符号类型,并处理相关的取值范围和运算,直接使用这些类是最规范的方式。
  • 读取时的处理:当你从该节点读取数据时,返回的 Variant 中的值也是 UShort 对象。你可以通过调用 UShorttoInt()toLong() 等方法将其转换为标准的 Java 类型进行计算或展示。
  • 不要直接使用基础类型:避免试图直接用 Java 的 intshort 进行强转,这会导致 ClassCastException。如果确实需要基于 int 值处理,正确做法是先将 int 转为 UShort 再写入,或从 UShort 转回 int

总结来说,使用 UShort 是 Milo 中处理 UInt16 类型数据点的标准方式