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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - dirgo

在Oracle中,授予用户角色和权限 Linux下查看当前哪些端口在监听状态,哪些端口在连接状态 mobaxterm常用配置 Windows下udp工具 Oracle表空间用户授权创建dblink等操作 用nmap扫描找出某个网段下空闲的ip脚本 Oracle 19c 常用运维 SQL Linux下设置CDB/PDB 环境的Oracle19c开机启动 Oracle是 CDB/PDB 环境下,让PDB在数据库启动后自动打开 怎样禁止dbeaver点击导航中数据库自动切换sql编辑器所属的数据库 Oracle 19c占内存高的解决方法 sudo -i -u zhangsan 与su - zhangsan区别,在现代 Linux 系统中,推荐使用 sudo 进行权限切换 Linux 的目录结构英文全称(及可能的命名背景)和更详细的说明 利用 Logback 的热加载特性,安全的迁移日志,改变日志保存路径 Linux中查询进程内存占用 iotDB调整内存占用及注意事项 centos7.9安装minio RELEASE.2025-04-22T22-12-26Z centos7.9编译安装nginx 1.28.1 linux(centos7.9)编译安装redis7.2.4 UFW 防火墙常用命令速查表
Eclipse Milo 处理PLC"字(Word)"类型,最直接和正确的做法是使用其内置的 UShort 类型
dirgo · 2026-01-08 · via 博客园 - dirgo

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

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

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

📝 详细写入步骤

在实际代码中,向一个 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 类型数据点的标准方式