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
d4f1904f
Commit
d4f1904f
authored
Oct 27, 2025
by
wanghao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1 各种modbus读取 写入 时间测试。
parent
c0abff62
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
69 additions
and
14 deletions
+69
-14
Modbus4jUtils.java
...ain/java/com/zehong/system/modbus/util/Modbus4jUtils.java
+63
-7
DeviceCommunicationJob.java
...n/java/com/zehong/system/task/DeviceCommunicationJob.java
+6
-7
No files found.
zhmes-agecal-system/src/main/java/com/zehong/system/modbus/util/Modbus4jUtils.java
View file @
d4f1904f
...
@@ -11,6 +11,7 @@ import com.serotonin.modbus4j.exception.ModbusTransportException;
...
@@ -11,6 +11,7 @@ import com.serotonin.modbus4j.exception.ModbusTransportException;
import
com.serotonin.modbus4j.ip.IpParameters
;
import
com.serotonin.modbus4j.ip.IpParameters
;
import
com.serotonin.modbus4j.locator.BaseLocator
;
import
com.serotonin.modbus4j.locator.BaseLocator
;
import
com.serotonin.modbus4j.msg.*
;
import
com.serotonin.modbus4j.msg.*
;
import
com.zehong.system.domain.PalletDeviceBinding
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
...
@@ -837,19 +838,46 @@ public class Modbus4jUtils {
...
@@ -837,19 +838,46 @@ public class Modbus4jUtils {
// System.out.println("v042:" + v042);
// System.out.println("v042:" + v042);
// // 批量读取
// // 批量读取
// batchRead();
// batchRead();
// Map<Integer, Object> result = new HashMap<>();
// for (Integer registerOffset : registerOffsets) {
// Boolean aBoolean = Modbus4jUtils.readCoilStatus(master, 1, registerOffset);
// result.put(registerOffset, aBoolean);
// }
// return result;
// master = Modbus4jUtils.getMaster(fip, fport);
// Boolean aBoolean = Modbus4jUtils.readCoilStatus(master, 1, registerOffset);
// 01 测试 读取 设备上有没有东西的 功能的时间
// registerOffset = 0; 说明是 第一层 有没有 托盘
modbusMaster
=
getMaster
(
"192.168.2.1"
,
504
);
Boolean
aBoolean
=
Modbus4jUtils
.
readCoilStatus
(
master
,
1
,
0
);
// 测试offset的这一层 是否有电
modbusMaster
=
getMaster
(
"192.168.2.2"
,
504
);
Boolean
hasElectricity
=
Modbus4jUtils
.
readCoilStatus
(
master
,
1
,
0
);
// 给这层上电
if
(!
hasElectricity
)
{
modbusMaster
=
getMaster
(
"192.168.2.2"
,
502
);
Modbus4jUtils
.
writeCoil
(
master
,
1
,
0
,
true
);
}
modbusMaster
=
getMaster
(
"192.168.2.11"
,
502
);
// 读取 第1个 pcba 板子的数据
boolean
[]
booleans
=
readDiscreteInputs
(
modbusMaster
,
1
,
0
,
2
);
modbusMaster
=
getMaster
(
"192.168.2.1"
,
501
);
int
[]
ints
=
readDeviceRegisters
(
modbusMaster
,
1
);
// 输出结果
// 4. 条件写入时间
for
(
int
i
=
0
;
i
<
booleans
.
length
;
i
++)
{
if
(
ints
[
1
]
==
1
||
ints
[
1
]
==
3
||
ints
[
1
]
==
4
)
{
System
.
out
.
println
(
"离散输入 "
+
(
0
+
i
)
+
": "
+
booleans
[
i
]);
// 重用之前的master连接进行写操作
modbusMaster
=
getMaster
(
"192.168.2.1"
,
501
);
writeCurrentTimeToDevice
(
master
,
1
);
}
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
}
finally
{
}
finally
{
if
(
modbusMaster
!=
null
)
{
if
(
modbusMaster
!=
null
)
{
modbusMaster
.
destroy
();
modbusMaster
.
destroy
();
System
.
out
.
println
(
"Modbus连接已关闭"
);
System
.
out
.
println
(
"Modbus连接已关闭"
);
...
@@ -862,7 +890,35 @@ public class Modbus4jUtils {
...
@@ -862,7 +890,35 @@ public class Modbus4jUtils {
}
}
}
}
}
}
/**
* 写入当前时间到设备
*/
public
static
void
writeCurrentTimeToDevice
(
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
);
// 写入时间寄存器
boolean
success
;
success
=
Modbus4jUtils
.
writeRegister
(
master
,
deviceId
,
4
,
(
short
)
year
);
success
&=
Modbus4jUtils
.
writeRegister
(
master
,
deviceId
,
5
,
(
short
)
month
);
success
&=
Modbus4jUtils
.
writeRegister
(
master
,
deviceId
,
6
,
(
short
)
day
);
success
&=
Modbus4jUtils
.
writeRegister
(
master
,
deviceId
,
7
,
(
short
)
hour
);
success
&=
Modbus4jUtils
.
writeRegister
(
master
,
deviceId
,
8
,
(
short
)
minute
);
if
(
success
)
{
log
.
debug
(
"设备{}时间写入成功"
,
deviceId
);
}
else
{
log
.
error
(
"设备{}时间写入失败"
,
deviceId
);
}
}
catch
(
Exception
e
)
{
log
.
error
(
"设备{}时间写入异常"
,
deviceId
,
e
);
}
}
/**
/**
* 读取设备寄存器(线程安全版)
* 读取设备寄存器(线程安全版)
...
...
zhmes-agecal-system/src/main/java/com/zehong/system/task/DeviceCommunicationJob.java
View file @
d4f1904f
...
@@ -123,7 +123,7 @@ public class DeviceCommunicationJob implements Job {
...
@@ -123,7 +123,7 @@ public class DeviceCommunicationJob implements Job {
// 并行处理该端口的所有设备
// 并行处理该端口的所有设备
List
<
CompletableFuture
<
Boolean
>>
deviceFutures
=
deviceIds
.
stream
()
List
<
CompletableFuture
<
Boolean
>>
deviceFutures
=
deviceIds
.
stream
()
.
map
(
deviceId
->
processDeviceWithWrite
(
ip
,
port
,
deviceId
,
storeyIdStr
,
errorCount
))
.
map
(
deviceId
->
processDeviceWithWrite
(
ip
,
port
,
deviceId
,
errorCount
))
.
collect
(
Collectors
.
toList
());
.
collect
(
Collectors
.
toList
());
try
{
try
{
...
@@ -155,13 +155,12 @@ public class DeviceCommunicationJob implements Job {
...
@@ -155,13 +155,12 @@ public class DeviceCommunicationJob implements Job {
/**
/**
* 处理单个设备(读取 + 条件写入)
* 处理单个设备(读取 + 条件写入)
*/
*/
private
CompletableFuture
<
Boolean
>
processDeviceWithWrite
(
String
ip
,
int
port
,
int
deviceId
,
private
CompletableFuture
<
Boolean
>
processDeviceWithWrite
(
String
ip
,
int
port
,
int
deviceId
,
AtomicInteger
errorCount
)
{
String
storeyIdStr
,
AtomicInteger
errorCount
)
{
return
CompletableFuture
.
supplyAsync
(()
->
{
return
CompletableFuture
.
supplyAsync
(()
->
{
ModbusMaster
master
=
null
;
ModbusMaster
master
=
null
;
try
{
try
{
// 1. 读取设备数据
// 1. 读取设备数据
int
[]
result
=
readDeviceWithRetry
(
ip
,
port
,
deviceId
,
storeyIdStr
);
int
[]
result
=
readDeviceWithRetry
(
ip
,
port
,
deviceId
);
// 2. 查询设备绑定信息
// 2. 查询设备绑定信息
PalletDeviceBinding
binding
=
palletDeviceBindingMapper
.
selectByTrayIdAndIndex
(
ip
,
deviceId
);
PalletDeviceBinding
binding
=
palletDeviceBindingMapper
.
selectByTrayIdAndIndex
(
ip
,
deviceId
);
...
@@ -179,7 +178,7 @@ public class DeviceCommunicationJob implements Job {
...
@@ -179,7 +178,7 @@ public class DeviceCommunicationJob implements Job {
if
(
result
[
1
]
==
1
||
result
[
1
]
==
3
||
result
[
1
]
==
4
)
{
if
(
result
[
1
]
==
1
||
result
[
1
]
==
3
||
result
[
1
]
==
4
)
{
// 重用之前的master连接进行写操作
// 重用之前的master连接进行写操作
master
=
createModbusMaster
(
ip
,
port
);
master
=
createModbusMaster
(
ip
,
port
);
writeCurrentTimeToDevice
(
master
,
deviceId
,
binding
,
ip
,
port
);
writeCurrentTimeToDevice
(
master
,
deviceId
,
binding
);
}
}
// 5. 更新数据库
// 5. 更新数据库
...
@@ -227,7 +226,7 @@ public class DeviceCommunicationJob implements Job {
...
@@ -227,7 +226,7 @@ public class DeviceCommunicationJob implements Job {
/**
/**
* 带重试的设备读取
* 带重试的设备读取
*/
*/
private
int
[]
readDeviceWithRetry
(
String
ip
,
int
port
,
int
deviceId
,
String
storeyIdStr
)
{
private
int
[]
readDeviceWithRetry
(
String
ip
,
int
port
,
int
deviceId
)
{
ModbusMaster
master
=
null
;
ModbusMaster
master
=
null
;
for
(
int
retry
=
0
;
retry
<=
CUSTOM_RETRY_TIMES
;
retry
++)
{
for
(
int
retry
=
0
;
retry
<=
CUSTOM_RETRY_TIMES
;
retry
++)
{
...
@@ -262,7 +261,7 @@ public class DeviceCommunicationJob implements Job {
...
@@ -262,7 +261,7 @@ public class DeviceCommunicationJob implements Job {
* 写入当前时间到设备
* 写入当前时间到设备
*/
*/
private
void
writeCurrentTimeToDevice
(
ModbusMaster
master
,
int
deviceId
,
private
void
writeCurrentTimeToDevice
(
ModbusMaster
master
,
int
deviceId
,
PalletDeviceBinding
binding
,
String
ip
,
int
port
)
{
PalletDeviceBinding
binding
)
{
try
{
try
{
Calendar
cal
=
Calendar
.
getInstance
();
Calendar
cal
=
Calendar
.
getInstance
();
int
year
=
cal
.
get
(
Calendar
.
YEAR
);
int
year
=
cal
.
get
(
Calendar
.
YEAR
);
...
...
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