Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
Z
zhmes-agecal
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
耿迪迪
zhmes-agecal
Commits
f8be6274
Commit
f8be6274
authored
Dec 25, 2025
by
wanghao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1 换了新程序后 写时间 失效问题测试
parent
9c454bf3
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
138 additions
and
1 deletion
+138
-1
Modbus4jUtils.java
...ain/java/com/zehong/system/modbus/util/Modbus4jUtils.java
+138
-1
No files found.
zhmes-agecal-system/src/main/java/com/zehong/system/modbus/util/Modbus4jUtils.java
View file @
f8be6274
...
...
@@ -796,7 +796,7 @@ public class Modbus4jUtils {
try
{
// 读取 第1个 pcba 板子的数据
modbusMaster
=
createModbusMaster
(
"192.168.2.1"
,
501
);
writeCurrentTimeToDevice
(
modbusMaster
,
1
);
writeCurrentTimeToDevice
Enhanced
(
modbusMaster
,
1
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
...
...
@@ -929,4 +929,141 @@ public class Modbus4jUtils {
return
unsignedValues
;
}
/**
* 增强版:写入并实时验证每个字段
*/
public
static
void
writeCurrentTimeToDeviceEnhanced
(
ModbusMaster
master
,
int
deviceId
)
{
try
{
Calendar
cal
=
Calendar
.
getInstance
();
int
year
=
cal
.
get
(
Calendar
.
YEAR
);
int
month
=
cal
.
get
(
Calendar
.
MONTH
)
+
1
;
int
day
=
cal
.
get
(
Calendar
.
DATE
);
int
hour
=
cal
.
get
(
Calendar
.
HOUR_OF_DAY
);
int
minute
=
cal
.
get
(
Calendar
.
MINUTE
);
log
.
info
(
"开始设置设备{}时间: {}-{}-{} {}:{}"
,
deviceId
,
year
,
month
,
day
,
hour
,
minute
);
// 逐个写入并验证
boolean
allSuccess
=
true
;
// 写入并验证年份
allSuccess
&=
writeAndVerifyField
(
master
,
deviceId
,
4
,
(
short
)
year
,
"年份"
,
year
);
Thread
.
sleep
(
200
);
// 写入并验证月份
allSuccess
&=
writeAndVerifyField
(
master
,
deviceId
,
5
,
(
short
)
month
,
"月份"
,
month
);
Thread
.
sleep
(
200
);
// 写入并验证日期
allSuccess
&=
writeAndVerifyField
(
master
,
deviceId
,
6
,
(
short
)
day
,
"日期"
,
day
);
Thread
.
sleep
(
200
);
// 写入并验证小时
allSuccess
&=
writeAndVerifyField
(
master
,
deviceId
,
7
,
(
short
)
hour
,
"小时"
,
hour
);
Thread
.
sleep
(
200
);
// 写入并验证分钟
allSuccess
&=
writeAndVerifyField
(
master
,
deviceId
,
8
,
(
short
)
minute
,
"分钟"
,
minute
);
Thread
.
sleep
(
200
);
if
(
allSuccess
)
{
// 最终整体验证
Thread
.
sleep
(
500
);
int
[]
ints
=
readDeviceRegisters
(
master
,
deviceId
);
if
(
verifyTimeRegisters
(
ints
,
year
,
month
,
day
,
hour
,
minute
))
{
log
.
info
(
"设备{}时间设置全部完成且验证通过"
,
deviceId
);
}
else
{
log
.
warn
(
"设备{}时间设置完成,但最终验证失败"
,
deviceId
);
}
}
else
{
log
.
error
(
"设备{}时间设置过程中出现失败"
,
deviceId
);
}
}
catch
(
Exception
e
)
{
log
.
error
(
"设备{}时间写入异常"
,
deviceId
,
e
);
}
}
/**
* 写入并验证单个字段
*/
private
static
boolean
writeAndVerifyField
(
ModbusMaster
master
,
int
deviceId
,
int
address
,
short
value
,
String
fieldName
,
int
expectedValue
)
{
try
{
log
.
info
(
"开始写入{}: {} (地址={})"
,
fieldName
,
expectedValue
,
address
);
// 写入
boolean
success
=
Modbus4jUtils
.
writeRegister
(
master
,
deviceId
,
address
,
value
);
if
(!
success
)
{
log
.
error
(
"写入{}失败"
,
fieldName
);
return
false
;
}
// 等待设备处理
Thread
.
sleep
(
100
);
// 读取验证
int
[]
ints
=
readDeviceRegisters
(
master
,
deviceId
);
if
(
ints
.
length
>
address
&&
ints
[
address
]
==
expectedValue
)
{
log
.
info
(
"{}验证通过,写入值: {}"
,
fieldName
,
expectedValue
);
return
true
;
}
else
{
log
.
warn
(
"{}验证失败,期望: {},实际: {}"
,
fieldName
,
expectedValue
,
ints
.
length
>
address
?
ints
[
address
]
:
"N/A"
);
return
false
;
}
}
catch
(
Exception
e
)
{
log
.
error
(
"写入{}时发生异常"
,
fieldName
,
e
);
return
false
;
}
}
/**
* 验证时间寄存器
*/
private
static
boolean
verifyTimeRegisters
(
int
[]
registers
,
int
year
,
int
month
,
int
day
,
int
hour
,
int
minute
)
{
if
(
registers
.
length
<
9
)
{
log
.
warn
(
"寄存器数量不足,无法验证时间"
);
return
false
;
}
boolean
verified
=
true
;
if
(
registers
[
4
]
!=
year
)
{
log
.
warn
(
"年份不匹配: 期望={}, 实际={}"
,
year
,
registers
[
4
]);
verified
=
false
;
}
if
(
registers
[
5
]
!=
month
)
{
log
.
warn
(
"月份不匹配: 期望={}, 实际={}"
,
month
,
registers
[
5
]);
verified
=
false
;
}
if
(
registers
[
6
]
!=
day
)
{
log
.
warn
(
"日期不匹配: 期望={}, 实际={}"
,
day
,
registers
[
6
]);
verified
=
false
;
}
if
(
registers
[
7
]
!=
hour
)
{
log
.
warn
(
"小时不匹配: 期望={}, 实际={}"
,
hour
,
registers
[
7
]);
verified
=
false
;
}
if
(
registers
[
8
]
!=
minute
)
{
log
.
warn
(
"分钟不匹配: 期望={}, 实际={}"
,
minute
,
registers
[
8
]);
verified
=
false
;
}
return
verified
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment