Browse Source

修改104 发送命令的bug

weiyigulu 3 years ago
parent
commit
3fc583b949

+ 4 - 4
pom.xml

@@ -6,7 +6,7 @@
 
     <groupId>wei.yigulu</groupId>
     <artifactId>protocol</artifactId>
-    <version>2.2.11</version>
+    <version>2.2.12</version>
     <packaging>pom</packaging>
     <modules>
         <module>protocol-core</module>
@@ -32,9 +32,9 @@
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
         <java.version>1.8</java.version>
-        <protocol.version>2.2.11</protocol.version>
-        <iec104.version>2.4.16</iec104.version>
-        <modbus.version>2.2.4</modbus.version>
+        <protocol.version>2.2.12</protocol.version>
+        <iec104.version>2.4.17</iec104.version>
+        <modbus.version>2.2.5</modbus.version>
         <cdt.version>1.0.0</cdt.version>
     </properties>
     <distributionManagement>

+ 1 - 1
protocol-all/pom.xml

@@ -6,7 +6,7 @@
     <parent>
         <artifactId>protocol</artifactId>
         <groupId>wei.yigulu</groupId>
-        <version>2.2.11</version>
+        <version>2.2.12</version>
     </parent>
 
 

+ 1 - 1
protocol-cdt/pom.xml

@@ -5,7 +5,7 @@
     <parent>
         <artifactId>protocol</artifactId>
         <groupId>wei.yigulu</groupId>
-        <version>2.2.11</version>
+        <version>2.2.12</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
 

+ 1 - 1
protocol-core/pom.xml

@@ -5,7 +5,7 @@
     <parent>
         <artifactId>protocol</artifactId>
         <groupId>wei.yigulu</groupId>
-        <version>2.2.11</version>
+        <version>2.2.12</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
 

+ 1 - 1
protocol-iec104/pom.xml

@@ -5,7 +5,7 @@
     <parent>
         <artifactId>protocol</artifactId>
         <groupId>wei.yigulu</groupId>
-        <version>2.2.11</version>
+        <version>2.2.12</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
 

+ 1 - 1
protocol-iec104/src/main/java/wei/yigulu/iec104/asdudataframe/ShortFloatCommand.java

@@ -65,7 +65,7 @@ public class ShortFloatCommand extends AbstractDataFrameType {
 	 * @param val     值
 	 */
 	public ShortFloatCommand(Integer address, Float val) {
-		new ShortFloatCommand(address, val, null);
+		 this(address, val, null);
 	}
 
 

+ 5 - 13
protocol-iec104/src/main/java/wei/yigulu/iec104/util/CommandWaiter.java

@@ -4,6 +4,7 @@ import io.netty.channel.ChannelId;
 import lombok.Data;
 import wei.yigulu.iec104.apdumodel.Apdu;
 import wei.yigulu.iec104.asdudataframe.typemodel.IecDataInterface;
+import wei.yigulu.iec104.exception.Iec104Exception;
 
 /**
  * 命令等待者
@@ -46,15 +47,16 @@ public class CommandWaiter {
 		this.dataAddress = dataAddress;
 	}
 
-	public synchronized IecDataInterface get() {
+	public synchronized IecDataInterface get() throws Iec104Exception {
 		if (this.data == null) {
 			try {
 				this.wait(5000);
+				return this.data;
 			} catch (InterruptedException e) {
 				e.printStackTrace();
 			}
 		}
-		return this.data;
+		throw new Iec104Exception("控制命令无响应");
 	}
 
 	public synchronized void set(IecDataInterface data) {
@@ -67,9 +69,7 @@ public class CommandWaiter {
 	public boolean equals(Object o) {
 		if (this == o) return true;
 		if (o == null || getClass() != o.getClass()) return false;
-
 		CommandWaiter that = (CommandWaiter) o;
-
 		if (!channelId.equals(that.channelId)) return false;
 		if (!sourceAddress.equals(that.sourceAddress)) return false;
 		if (!commonAddress.equals(that.commonAddress)) return false;
@@ -77,13 +77,5 @@ public class CommandWaiter {
 		return dataAddress.equals(that.dataAddress);
 	}
 
-	@Override
-	public int hashCode() {
-		int result = channelId.hashCode();
-		result = 31 * result + sourceAddress.hashCode();
-		result = 31 * result + commonAddress.hashCode();
-		result = 31 * result + typeId.hashCode();
-		result = 31 * result + dataAddress.hashCode();
-		return result;
-	}
+
 }

+ 12 - 4
protocol-iec104/src/main/java/wei/yigulu/iec104/util/SendCommandHelper.java

@@ -25,6 +25,7 @@ public class SendCommandHelper {
 
 	public static boolean sendShortCommand(AbstractMasterBuilder masterBuilder, Integer sourceAddress, Integer commonAddress, Integer dataAddress, Float value) throws Exception {
 		ShortFloatCommand command = new ShortFloatCommand(dataAddress, value);
+		System.out.println(command.toString());
 		Apdu apdu = new Apdu();
 		Asdu asdu = command.generateBack();
 		asdu.setCommonAddress(commonAddress);
@@ -32,10 +33,18 @@ public class SendCommandHelper {
 		asdu.getCot().setNot(6);
 		apdu.setAsdu(asdu);
 		SendAndReceiveNumUtil.sendIFrame(apdu, masterBuilder.getFuture().channel(), masterBuilder.getLog());
-		CommandWaiter commandWaiter = new CommandWaiter(masterBuilder.getFuture().channel().id(), apdu, 0);
+		CommandWaiter commandWaiter = new CommandWaiter(masterBuilder.getFuture().channel().id(), apdu, dataAddress);
 		commandWaiters.add(commandWaiter);
-		IecDataInterface data = commandWaiter.get();
-		if (value.equals(data.getIecValue())) {
+		IecDataInterface data;
+		try {
+			data = commandWaiter.get();
+		}catch (Exception e){
+			throw e;
+		}finally {
+			commandWaiters.remove(commandWaiter);
+		}
+
+		if (data!=null && value.equals(data.getIecValue())) {
 			return true;
 		} else {
 			return false;
@@ -45,7 +54,6 @@ public class SendCommandHelper {
 
 	public static void setIecValue(CommandWaiter commandWaiter) {
 		int i = commandWaiters.indexOf(commandWaiter);
-		System.out.println(i);
 		if (i != -1) {
 			commandWaiters.get(i).set(commandWaiter.getData());
 		} else {

+ 31 - 0
protocol-iec104/src/test/java/SendCommand.java

@@ -0,0 +1,31 @@
+import wei.yigulu.iec104.nettyconfig.Iec104MasterBuilder;
+import wei.yigulu.iec104.util.SendCommandHelper;
+
+import java.util.Random;
+
+/**
+ * @author: xiuwei
+ * @version:
+ */
+public class SendCommand {
+
+	public static void main(String[] args) throws Exception {
+		sendAp();
+	}
+
+	public static void sendAp() throws Exception {
+		Random random = new Random();
+		Iec104MasterBuilder builder = new Iec104MasterBuilder("127.0.0.1", 2404);
+		builder.createByUnBlock();
+		while (true) {
+			Thread.sleep(30000L);
+			try {
+				SendCommandHelper.sendShortCommand(builder, 0, 0, 1, (float) (40 + 10 * random.nextDouble()));
+			} catch (Exception e) {
+				e.printStackTrace();
+			}
+		}
+	}
+
+
+}

+ 1 - 1
protocol-modbus/pom.xml

@@ -5,7 +5,7 @@
     <parent>
         <artifactId>protocol</artifactId>
         <groupId>wei.yigulu</groupId>
-        <version>2.2.11</version>
+        <version>2.2.12</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <version>${modbus.version}</version>

+ 1 - 1
protocol-modbus/src/main/java/wei/yigulu/modbus/domain/confirm/TcpModbusConfirm.java

@@ -54,7 +54,7 @@ public class TcpModbusConfirm extends AbstractModbusConfirm {
 	@Override
 	public TcpModbusConfirm decode(ByteBuffer byteBuf) throws ModbusException {
 		if (byteBuf.remaining() != 12) {
-			throw new ModbusException("该帧非数据请求帧");
+			throw new ModbusException("该帧非命令确认帧");
 		}
 		this.tcpExtraCode.decode(byteBuf);
 		this.setLength(new P_AB().decode(byteBuf).getValue().intValue());