DLL版本信息 (VersionInfo)
VersionInfo 提供 DLL 库的版本信息和元数据查询,用于诊断、日志记录和兼容性检查。
通过 com.darra.ethercat.statics.VersionInfo 静态工具类访问。
版本信息属性
getDllVersionInfo
public static DarraCore.DllVersionInfo getDllVersionInfo()
获取完整的 DLL 版本信息结构。
返回值:
DarraCore.DllVersionInfo— 包含版本号、构建日期等完整信息,失败返回null
相关结构:
public class DllVersionInfo {
public short Major; // 主版本号
public short Minor; // 次版本号
public short Patch; // 修订号
public short Build; // 构建号
public byte[] BuildDate; // 构建日期原始字节(32 字节缓冲区,UTF-8)
public String getBuildDateString(); // 解码构建日期字符串(ISO 8601 格式)
}
示例:
DarraCore.DllVersionInfo info = VersionInfo.getDllVersionInfo();
if (info != null) {
System.out.printf("DLL版本: %d.%d.%d.%d%n",
info.Major, info.Minor, info.Patch, info.Build);
System.out.println("构建日期: " + info.getBuildDateString());
}
getDllVersion
public static String getDllVersion()
获取 DLL 版本号字符串,格式为 X.X.X.X。
返回值:
String— 版本号(例如:2.5.0.0),失败返回"unknown"
示例:
String version = VersionInfo.getDllVersion();
System.out.println("当前DLL版本: " + version);
// 版本兼容性检查
if (version.startsWith("2.5")) {
System.out.println("使用 v2.5 系列");
}
getBuildDate
public static String getBuildDate()
获取 DLL 编译日期。
返回值:
String— 构建日期(ISO 8601格式)
示例:
String buildDate = VersionInfo.getBuildDate();
System.out.println("DLL构建于: " + buildDate);
getSerialNumber
public static String getSerialNumber()
获取 DLL 的唯一识别码 / 设备序列号。
返回值:
String— 识别码字符串
示例:
String serial = VersionInfo.getSerialNumber();
System.out.println("DLL序列号: " + serial);
// 用于唯一标识 DLL 版本
String uniqueId = VersionInfo.getDllVersion() + "-" + serial;
完整示例
应用程序日志
在日志中记录 DLL 版本信息:
import com.darra.ethercat.statics.VersionInfo;
public class Main {
public static void main(String[] args) {
// 启动时记录版本信息
System.out.println("=== Darra EtherCAT Master ===");
System.out.println("DLL版本: " + VersionInfo.getDllVersion());
System.out.println("构建日期: " + VersionInfo.getBuildDate());
System.out.println("序列号: " + VersionInfo.getSerialNumber());
System.out.println("============================");
// 业务逻辑...
}
}
版本兼容性检查
确保应用程序与 DLL 版本兼容:
public class VersionChecker {
public static boolean checkCompatibility() {
String version = VersionInfo.getDllVersion();
if ("unknown".equals(version)) {
System.out.println("错误: 无法获取DLL版本信息");
return false;
}
String[] parts = version.split("\\.");
int major = Integer.parseInt(parts[0]);
int minor = Integer.parseInt(parts[1]);
// 要求 v2.5 或更高版本
if (major < 2 || (major == 2 && minor < 5)) {
System.out.println("错误: DLL版本过低(" + version + "),需要 v2.5+");
return false;
}
System.out.println("DLL版本兼容: " + version);
return true;
}
}
诊断信息收集
生成诊断报告:
public class DiagnosticsReporter {
public static String generateReport() {
StringBuilder sb = new StringBuilder();
sb.append("=== 系统诊断报告 ===\n\n");
// DLL 版本信息
sb.append("DLL信息:\n");
sb.append(" 版本: ").append(VersionInfo.getDllVersion()).append("\n");
sb.append(" 构建: ").append(VersionInfo.getBuildDate()).append("\n");
sb.append(" 序列号: ").append(VersionInfo.getSerialNumber()).append("\n\n");
// 系统信息
sb.append(" Java版本: ").append(System.getProperty("java.version")).append("\n");
sb.append(" 操作系统: ").append(System.getProperty("os.name")).append("\n");
return sb.toString();
}
}
唯一标识符生成
为日志文件或会话生成唯一标识符:
import java.text.SimpleDateFormat;
import java.util.Date;
public class SessionManager {
public static String createSessionId() {
String versionId = VersionInfo.getSerialNumber();
String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
return "SESSION_" + versionId + "_" + timestamp;
}
}
应用启动检查
import com.darra.ethercat.master.EtherCATMaster;
import com.darra.ethercat.statics.VersionInfo;
public class Program {
public static void main(String[] args) {
try {
// 显示版本信息
printVersionInfo();
// 兼容性检查
if (!checkMinimumVersion(2, 5)) {
System.out.println("程序需要 DLL v2.5 或更高版本");
return;
}
// 启动主站
EtherCATMaster master = EtherCATMaster.create();
// ...
} catch (Exception ex) {
logError(ex);
}
}
static void printVersionInfo() {
System.out.println("==============================");
System.out.println(" Darra EtherCAT Master");
System.out.println("------------------------------");
System.out.printf(" 版本: %-20s%n", VersionInfo.getDllVersion());
System.out.printf(" 构建: %-20s%n", VersionInfo.getBuildDate());
System.out.printf(" 序列: %-20s%n", VersionInfo.getSerialNumber());
System.out.println("==============================");
}
static boolean checkMinimumVersion(int minMajor, int minMinor) {
String[] parts = VersionInfo.getDllVersion().split("\\.");
int major = Integer.parseInt(parts[0]);
int minor = Integer.parseInt(parts[1]);
return (major > minMajor) || (major == minMajor && minor >= minMinor);
}
static void logError(Exception ex) {
// 写入错误日志(含 DLL 版本与序列号便于追溯)
System.err.println("DLL v" + VersionInfo.getDllVersion()
+ " serial=" + VersionInfo.getSerialNumber());
ex.printStackTrace();
}
}
注意事项
版本管理
在生产环境中,建议记录 DLL 版本信息到应用程序日志,便于问题追溯和版本管理。
序列号用途
getSerialNumber() 是 DLL 版本唯一标识码,主要用于快速区分不同构建版本,不应用于安全验证。