Commit d4f1904f authored by wanghao's avatar wanghao

1 各种modbus读取 写入 时间测试。

parent c0abff62
......@@ -11,6 +11,7 @@ import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.ip.IpParameters;
import com.serotonin.modbus4j.locator.BaseLocator;
import com.serotonin.modbus4j.msg.*;
import com.zehong.system.domain.PalletDeviceBinding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -837,19 +838,46 @@ public class Modbus4jUtils {
// System.out.println("v042:" + v042);
// // 批量读取
// 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);
boolean[] booleans = readDiscreteInputs(modbusMaster, 1, 0, 2);
// 读取 第1个 pcba 板子的数据
modbusMaster = getMaster("192.168.2.1", 501);
int[] ints = readDeviceRegisters(modbusMaster, 1);
// 输出结果
for (int i = 0; i < booleans.length; i++) {
System.out.println("离散输入 " + (0 + i) + ": " + booleans[i]);
// 4. 条件写入时间
if (ints[1] == 1 || ints[1] == 3 || ints[1] == 4) {
// 重用之前的master连接进行写操作
modbusMaster = getMaster("192.168.2.1", 501);
writeCurrentTimeToDevice(master, 1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (modbusMaster != null) {
modbusMaster.destroy();
System.out.println("Modbus连接已关闭");
......@@ -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);
}
}
/**
* 读取设备寄存器(线程安全版)
......
......@@ -123,7 +123,7 @@ public class DeviceCommunicationJob implements Job {
// 并行处理该端口的所有设备
List<CompletableFuture<Boolean>> deviceFutures = deviceIds.stream()
.map(deviceId -> processDeviceWithWrite(ip, port, deviceId, storeyIdStr, errorCount))
.map(deviceId -> processDeviceWithWrite(ip, port, deviceId, errorCount))
.collect(Collectors.toList());
try {
......@@ -155,13 +155,12 @@ public class DeviceCommunicationJob implements Job {
/**
* 处理单个设备(读取 + 条件写入)
*/
private CompletableFuture<Boolean> processDeviceWithWrite(String ip, int port, int deviceId,
String storeyIdStr, AtomicInteger errorCount) {
private CompletableFuture<Boolean> processDeviceWithWrite(String ip, int port, int deviceId, AtomicInteger errorCount) {
return CompletableFuture.supplyAsync(() -> {
ModbusMaster master = null;
try {
// 1. 读取设备数据
int[] result = readDeviceWithRetry(ip, port, deviceId, storeyIdStr);
int[] result = readDeviceWithRetry(ip, port, deviceId);
// 2. 查询设备绑定信息
PalletDeviceBinding binding = palletDeviceBindingMapper.selectByTrayIdAndIndex(ip, deviceId);
......@@ -179,7 +178,7 @@ public class DeviceCommunicationJob implements Job {
if (result[1] == 1 || result[1] == 3 || result[1] == 4) {
// 重用之前的master连接进行写操作
master = createModbusMaster(ip, port);
writeCurrentTimeToDevice(master, deviceId, binding, ip, port);
writeCurrentTimeToDevice(master, deviceId, binding);
}
// 5. 更新数据库
......@@ -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;
for (int retry = 0; retry <= CUSTOM_RETRY_TIMES; retry++) {
......@@ -262,7 +261,7 @@ public class DeviceCommunicationJob implements Job {
* 写入当前时间到设备
*/
private void writeCurrentTimeToDevice(ModbusMaster master, int deviceId,
PalletDeviceBinding binding, String ip, int port) {
PalletDeviceBinding binding) {
try {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment