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
2a225540
Commit
2a225540
authored
Dec 25, 2025
by
wanghao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1 换了新程序后 写时间 失效问题测试
parent
f8be6274
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
56 additions
and
2 deletions
+56
-2
Modbus4jUtils.java
...ain/java/com/zehong/system/modbus/util/Modbus4jUtils.java
+56
-2
No files found.
zhmes-agecal-system/src/main/java/com/zehong/system/modbus/util/Modbus4jUtils.java
View file @
2a225540
...
...
@@ -650,6 +650,60 @@ public class Modbus4jUtils {
log
.
info
(
"写入成功,响应确认"
);
return
true
;
}
/**
* 使用功能码16写入多个保持寄存器(更稳定,支持批量)
*/
public
static
boolean
writeRegistersMultiple
(
ModbusMaster
master
,
int
slaveId
,
int
startOffset
,
short
[]
values
)
throws
ModbusTransportException
,
ModbusInitException
{
log
.
info
(
"发送批量写入请求:从站={}, 起始地址={}, 数量={}, 值={}"
,
slaveId
,
startOffset
,
values
.
length
,
Arrays
.
toString
(
values
));
WriteRegistersRequest
request
=
new
WriteRegistersRequest
(
slaveId
,
startOffset
,
values
);
WriteRegistersResponse
response
=
null
;
synchronized
(
master
)
{
response
=
(
WriteRegistersResponse
)
master
.
send
(
request
);
}
if
(
response
==
null
)
{
log
.
error
(
"未收到 Modbus 响应"
);
return
false
;
}
if
(
response
.
isException
())
{
log
.
error
(
"Modbus 异常响应: {}"
,
response
.
getExceptionMessage
());
return
false
;
}
log
.
info
(
"批量写入成功,起始地址={}, 写入数量={}"
,
startOffset
,
values
.
length
);
return
true
;
}
/**
* 写入时间(使用功能码16一次性写入所有时间寄存器)
*/
public
static
boolean
writeTimeRegisters
(
ModbusMaster
master
,
int
deviceId
)
{
try
{
Calendar
cal
=
Calendar
.
getInstance
();
short
[]
timeValues
=
{
(
short
)
cal
.
get
(
Calendar
.
YEAR
),
(
short
)
(
cal
.
get
(
Calendar
.
MONTH
)
+
1
),
(
short
)
cal
.
get
(
Calendar
.
DATE
),
(
short
)
cal
.
get
(
Calendar
.
HOUR_OF_DAY
),
(
short
)
cal
.
get
(
Calendar
.
MINUTE
)
};
// 一次性写入所有时间寄存器
return
writeRegistersMultiple
(
master
,
deviceId
,
4
,
timeValues
);
}
catch
(
Exception
e
)
{
log
.
error
(
"批量写入时间失败"
,
e
);
return
false
;
}
}
/***
* 写[03 Holding Register(4x)] 写一个 function ID = 6
*
...
...
@@ -796,7 +850,8 @@ public class Modbus4jUtils {
try
{
// 读取 第1个 pcba 板子的数据
modbusMaster
=
createModbusMaster
(
"192.168.2.1"
,
501
);
writeCurrentTimeToDeviceEnhanced
(
modbusMaster
,
1
);
// writeCurrentTimeToDeviceEnhanced(modbusMaster, 1);
writeTimeRegisters
(
modbusMaster
,
1
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
...
...
@@ -957,7 +1012,6 @@ public class Modbus4jUtils {
allSuccess
&=
writeAndVerifyField
(
master
,
deviceId
,
5
,
(
short
)
month
,
"月份"
,
month
);
Thread
.
sleep
(
200
);
// 写入并验证日期
allSuccess
&=
writeAndVerifyField
(
master
,
deviceId
,
6
,
(
short
)
day
,
"日期"
,
day
);
Thread
.
sleep
(
200
);
...
...
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