Parcourir la source

修改modbus浮点数转BigDecimal 精度丢失问题 + 一些注释的修改

xiuwei il y a 4 ans
Parent
commit
90a6419ec3

+ 3 - 3
pom.xml

@@ -6,7 +6,7 @@
 
     <groupId>wei.yigulu</groupId>
     <artifactId>protocol</artifactId>
-    <version>1.1.9</version>
+    <version>1.1.10</version>
     <packaging>pom</packaging>
     <modules>
         <module>protocol-core</module>
@@ -32,8 +32,8 @@
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
         <java.version>1.8</java.version>
-        <protocol.version>1.1.9</protocol.version>
-        <iec104.version>1.4.14</iec104.version>
+        <protocol.version>1.1.10</protocol.version>
+        <iec104.version>1.4.15</iec104.version>
         <modbus.version>1.2.4</modbus.version>
         <cdt.version>1.0.0</cdt.version>
     </properties>

+ 1 - 1
protocol-all/pom.xml

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

+ 1 - 1
protocol-cdt/pom.xml

@@ -5,7 +5,7 @@
     <parent>
         <artifactId>protocol</artifactId>
         <groupId>wei.yigulu</groupId>
-        <version>1.1.9</version>
+        <version>1.1.10</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>1.1.9</version>
+        <version>1.1.10</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
 

+ 5 - 0
protocol-core/src/main/java/wei/yigulu/netty/AbstractTcpMasterBuilder.java

@@ -134,6 +134,11 @@ public abstract class AbstractTcpMasterBuilder extends AbstractMasterBuilder {
 		return this.connectionListener;
 	}
 
+	/**
+	 * 获取或创建通道初始化
+	 *
+	 * @return {@link ProtocolChannelInitializer}
+	 */
 	@Override
 	protected abstract ProtocolChannelInitializer getOrCreateChannelInitializer();
 

+ 2 - 2
protocol-core/src/main/java/wei/yigulu/netty/AbstractTcpSlaverBuilder.java

@@ -81,9 +81,9 @@ public abstract class AbstractTcpSlaverBuilder extends BaseProtocolBuilder {
 		log.info("Slaver端启动成功;端口" + port);
 		// 关闭服务器通道
 		cf.channel().closeFuture().sync();
-		// 释放线程池资源
+	// 释放线程池资源
 		group.shutdownGracefully().sync();
-	}
+}
 
 
 	/**

+ 1 - 1
protocol-iec104/pom.xml

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

+ 6 - 5
protocol-iec104/src/main/java/wei/yigulu/iec104/nettyconfig/Iec104SlaverBuilder.java

@@ -20,6 +20,7 @@ import wei.yigulu.netty.ProtocolChannelInitializer;
 public class Iec104SlaverBuilder extends AbstractTcpSlaverBuilder {
 
 
+
 	public Iec104SlaverBuilder(int port) {
 		super(port);
 	}
@@ -27,11 +28,11 @@ public class Iec104SlaverBuilder extends AbstractTcpSlaverBuilder {
 	@Override
 	protected ProtocolChannelInitializer getOrCreateChannelInitializer() {
 		return new ProtocolChannelInitializer<SocketChannel>(this) {
-			@Override
-			protected void initChannel(SocketChannel ch) throws Exception {
-				ch.pipeline().addLast(new AllCustomDelimiterHandler());
-				ch.pipeline().addLast(new Slave104Handle((Iec104SlaverBuilder) builder));
-			}
+				@Override
+				protected void initChannel(SocketChannel ch) throws Exception {
+					ch.pipeline().addLast(new AllCustomDelimiterHandler());
+					ch.pipeline().addLast(new Slave104Handle((Iec104SlaverBuilder) builder));
+				}
 
 		};
 	}

+ 1 - 2
protocol-iec104/src/main/java/wei/yigulu/iec104/nettyconfig/Master104Handle.java

@@ -59,6 +59,7 @@ public class Master104Handle extends SimpleChannelInboundHandler<ByteBuf> {
 	private Class<? extends Apdu> apduClass = Apdu.class;
 
 
+
 	@Override
 	public void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
 		//收数据
@@ -68,9 +69,7 @@ public class Master104Handle extends SimpleChannelInboundHandler<ByteBuf> {
 		if (apdu.getApciType() == Apdu.ApciType.I_FORMAT) {
 			this.testNum = 0;
 		}
-
 		apdu.answer();
-
 	}
 
 

+ 1 - 1
protocol-iec104/src/main/java/wei/yigulu/iec104/nettyconfig/Slave104Handle.java

@@ -57,7 +57,7 @@ public class Slave104Handle extends SimpleChannelInboundHandler<ByteBuf> {
 	public void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
 		//收数据
 		log.debug("----------------------------------------------------------------------------------");
-		log.debug(DataConvertor.ByteBuf2String( msg));
+		log.debug("re <= "+DataConvertor.ByteBuf2String( msg));
 		Apdu apdu = apduClass.newInstance().setChannel(ctx.channel()).setIec104Builder(slaverBuilder).setLog(slaverBuilder.getLog()).loadByteBuf(msg);
 		apdu.answer();
 	}

+ 2 - 3
protocol-iec104/src/test/java/MasterTest.java

@@ -16,9 +16,8 @@ public class MasterTest {
 
 	public static void main(String[] args) throws Exception {
 		ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.ADVANCED);
-		Iec104MasterBuilder masterBuilder = new Iec104MasterBuilder("192.168.9.112", 2404);
-		masterBuilder.setSelfIp("192.168.9.156");
-		masterBuilder.setSelfPort(28732);
+		Iec104HSMasterBuilder masterBuilder = new Iec104HSMasterBuilder("127.0.0.1", 2404);
+
 		masterBuilder.createByUnBlock();
 
 	/*	//创建总召唤类型I帧

+ 19 - 8
protocol-iec104/src/test/java/NettyClient.java

@@ -1,8 +1,10 @@
+import io.netty.bootstrap.Bootstrap;
 import io.netty.bootstrap.ServerBootstrap;
 import io.netty.channel.*;
 import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.channel.socket.SocketChannel;
 import io.netty.channel.socket.nio.NioServerSocketChannel;
+import io.netty.channel.socket.nio.NioSocketChannel;
 import io.netty.handler.codec.string.StringDecoder;
 import io.netty.handler.codec.string.StringEncoder;
 
@@ -15,20 +17,18 @@ import io.netty.handler.codec.string.StringEncoder;
  **/
 public class NettyClient {
 	public static void main(String[] args) {
-
 		EventLoopGroup group = new NioEventLoopGroup();
 		try {
-			ServerBootstrap sb = new ServerBootstrap();
+			Bootstrap sb = new Bootstrap();
 			// 绑定线程池
 			sb.group(group)
 					// 指定使用的channel
-					.channel(NioServerSocketChannel.class)
+					.channel(NioSocketChannel.class)
 					// 绑定监听端口
-					.localAddress(9001)
 					// 绑定客户端连接时候触发操作
-					.childHandler(new ChannelInitializer<SocketChannel>() {
+					.handler(new ChannelInitializer<NioSocketChannel>() {
 						@Override
-						protected void initChannel(SocketChannel ch) throws Exception {
+						protected void initChannel(NioSocketChannel ch) throws Exception {
 							ch.pipeline().addLast(new StringDecoder());
 							//字符串编码器
 							ch.pipeline().addLast(new StringEncoder());
@@ -37,9 +37,20 @@ public class NettyClient {
 						}
 					});
 			// 服务器异步创建绑定
-			ChannelFuture cf = sb.bind().sync();
+			ChannelFuture cf = sb.bind("127.0.0.1",2404);
+			System.out.println("1"+cf.isSuccess());
+			cf.sync();
+			System.out.println("2"+cf.isSuccess());
+
+			System.out.println("4"+cf.isDone());
+			System.out.println(cf.getClass().getSimpleName());
 			// 关闭服务器通道
-			cf.channel().closeFuture().sync();
+			ChannelFuture ff = cf.channel().closeFuture();
+			System.out.println(ff.getClass().getSimpleName());
+			System.out.println(ff.sync());
+			System.out.println("is=="+ (cf==ff));
+			System.out.println(cf.isDone());
+			System.out.println("3"+cf.isSuccess());
 
 		} catch (InterruptedException e) {
 			e.printStackTrace();

+ 0 - 3
protocol-iec104/src/test/java/SlaveTest.java

@@ -10,9 +10,6 @@ public class SlaveTest {
 
 	public static void main(String[] args) throws Exception {
 		Iec104SlaverBuilder slaverBuilder = new Iec104SlaverBuilder(2404);
-
-
-
 		slaverBuilder.getConnectFilterManager().appendFilter((c)->{
 			if(slaverBuilder.getChannels().size()>=1){
 				return -1;

+ 1 - 1
protocol-modbus/pom.xml

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

+ 4 - 4
protocol-modbus/src/main/java/wei/yigulu/modbus/domain/datatype/numeric/ABCD.java

@@ -29,15 +29,15 @@ public class ABCD extends NumericModbusData {
 
 	@Override
 	public ABCD decode(byte[] bytes, int offset) {
-		this.value = BigDecimal.valueOf(Float.intBitsToFloat((bytes[offset * 2] & 0xff) << 24 | ((bytes[offset * 2 + 1] & 0xff) << 16)
-				| ((bytes[offset * 2 + 2] & 0xff) << 8) | ((bytes[offset * 2 + 3] & 0xff))));
+		this.value = new BigDecimal(Float.toString(Float.intBitsToFloat((bytes[offset * 2] & 0xff) << 24 | ((bytes[offset * 2 + 1] & 0xff) << 16)
+				| ((bytes[offset * 2 + 2] & 0xff) << 8) | ((bytes[offset * 2 + 3] & 0xff)))));
 		return this;
 	}
 
 	@Override
 	public ABCD decode(ByteBuffer byteBuf) {
-		this.value = BigDecimal.valueOf(Float.intBitsToFloat((byteBuf.get() & 0xff) << 24 | ((byteBuf.get() & 0xff) << 16)
-				| ((byteBuf.get() & 0xff) << 8) | ((byteBuf.get() & 0xff))));
+		this.value = new BigDecimal(Float.toString(Float.intBitsToFloat((byteBuf.get() & 0xff) << 24 | ((byteBuf.get() & 0xff) << 16)
+				| ((byteBuf.get() & 0xff) << 8) | ((byteBuf.get() & 0xff)))));
 		return this;
 	}
 

+ 4 - 4
protocol-modbus/src/main/java/wei/yigulu/modbus/domain/datatype/numeric/BADC.java

@@ -29,15 +29,15 @@ public class BADC extends NumericModbusData {
 
 	@Override
 	public BADC decode(byte[] bytes, int offset) {
-		this.value = BigDecimal.valueOf(Float.intBitsToFloat( ((bytes[offset * 2 + 0] & 0xff) << 16) | ((bytes[offset * 2 + 1] & 0xff) << 24)|(bytes[offset * 2+2] & 0xff) | ((bytes[offset * 2 + 3] & 0xff) << 8)
-				));
+		this.value =new BigDecimal(Float.toString(Float.intBitsToFloat( ((bytes[offset * 2 + 0] & 0xff) << 16) | ((bytes[offset * 2 + 1] & 0xff) << 24)|(bytes[offset * 2+2] & 0xff) | ((bytes[offset * 2 + 3] & 0xff) << 8)
+				)));
 		return this;
 	}
 
 	@Override
 	public BADC decode(ByteBuffer byteBuf) {
-		this.value = BigDecimal.valueOf(Float.intBitsToFloat(
-				((byteBuf.get() & 0xff) << 16) | ((byteBuf.get() & 0xff) << 24)|(byteBuf.get() & 0xff) | ((byteBuf.get() & 0xff) << 8)));
+		this.value = new BigDecimal(Float.toString(Float.intBitsToFloat(
+				((byteBuf.get() & 0xff) << 16) | ((byteBuf.get() & 0xff) << 24)|(byteBuf.get() & 0xff) | ((byteBuf.get() & 0xff) << 8))));
 		return this;
 	}
 

+ 4 - 4
protocol-modbus/src/main/java/wei/yigulu/modbus/domain/datatype/numeric/CDAB.java

@@ -29,15 +29,15 @@ public class CDAB extends NumericModbusData {
 
 	@Override
 	public CDAB decode(byte[] bytes, int offset) {
-		this.value = BigDecimal.valueOf(Float.intBitsToFloat((bytes[offset * 2] & 0xff) << 8 | ((bytes[offset * 2 + 1] & 0xff))
-				| ((bytes[offset * 2 + 2] & 0xff) << 24) | ((bytes[offset * 2 + 3] & 0xff) << 16)));
+		this.value =new BigDecimal(Float.toString(Float.intBitsToFloat((bytes[offset * 2] & 0xff) << 8 | ((bytes[offset * 2 + 1] & 0xff))
+				| ((bytes[offset * 2 + 2] & 0xff) << 24) | ((bytes[offset * 2 + 3] & 0xff) << 16))));
 		return this;
 	}
 
 	@Override
 	public CDAB decode(ByteBuffer byteBuf) {
-		this.value = BigDecimal.valueOf(Float.intBitsToFloat((byteBuf.get() & 0xff) << 8 | ((byteBuf.get() & 0xff))
-				| ((byteBuf.get() & 0xff) << 24) | ((byteBuf.get() & 0xff) << 16)));
+		this.value = new BigDecimal(Float.toString(Float.intBitsToFloat((byteBuf.get() & 0xff) << 8 | ((byteBuf.get() & 0xff))
+				| ((byteBuf.get() & 0xff) << 24) | ((byteBuf.get() & 0xff) << 16))));
 		return this;
 	}
 

+ 4 - 4
protocol-modbus/src/main/java/wei/yigulu/modbus/domain/datatype/numeric/DCBA.java

@@ -29,15 +29,15 @@ public class DCBA extends NumericModbusData {
 
 	@Override
 	public DCBA decode(byte[] bytes, int offset) {
-		this.value = BigDecimal.valueOf(Float.intBitsToFloat((bytes[offset * 2] & 0xff) | ((bytes[offset * 2 + 1] & 0xff) << 8)
-				| ((bytes[offset * 2 + 2] & 0xff) << 16) | ((bytes[offset * 2 + 3] & 0xff) << 24)));
+		this.value = new BigDecimal(Float.toString(Float.intBitsToFloat((bytes[offset * 2] & 0xff) | ((bytes[offset * 2 + 1] & 0xff) << 8)
+				| ((bytes[offset * 2 + 2] & 0xff) << 16) | ((bytes[offset * 2 + 3] & 0xff) << 24))));
 		return this;
 	}
 
 	@Override
 	public DCBA decode(ByteBuffer byteBuf) {
-		this.value = BigDecimal.valueOf(Float.intBitsToFloat((byteBuf.get() & 0xff) | ((byteBuf.get() & 0xff) << 8)
-				| ((byteBuf.get() & 0xff) << 16) | ((byteBuf.get() & 0xff) << 24)));
+		this.value = new BigDecimal(Float.toString(Float.intBitsToFloat((byteBuf.get() & 0xff) | ((byteBuf.get() & 0xff) << 8)
+				| ((byteBuf.get() & 0xff) << 16) | ((byteBuf.get() & 0xff) << 24))));
 		return this;
 	}
 

+ 2 - 0
protocol-modbus/src/main/java/wei/yigulu/modbus/netty/ModbusMasterBuilderInterface.java

@@ -13,9 +13,11 @@ public interface ModbusMasterBuilderInterface {
 
 
 	/**
+	 * 获取或创建同步等候室
 	 * null则创建,有则获取获取EventLoopGroup 用与bootstrap的绑定
 	 *
 	 * @return or create work group
+	 * @throws ModbusException modbus例外
 	 */
 	SynchronousWaitingRoom getOrCreateSynchronousWaitingRoom() throws ModbusException;
 

+ 15 - 2
protocol-modbus/src/test/java/Test.java

@@ -1,3 +1,4 @@
+import wei.yigulu.modbus.domain.datatype.numeric.*;
 import wei.yigulu.modbus.netty.ModbusTcpMasterBuilder;
 
 /**
@@ -6,11 +7,23 @@ import wei.yigulu.modbus.netty.ModbusTcpMasterBuilder;
  */
 public class Test {
 	public static void main(String[] args) throws InterruptedException {
-		ModbusTcpMasterBuilder master = new ModbusTcpMasterBuilder("127.0.0.1", 5002);
+	/*	ModbusTcpMasterBuilder master = new ModbusTcpMasterBuilder("127.0.0.1", 5002);
 		master.createByUnBlock();
 		Thread.sleep(30000L);
 		System.out.println("重启");
 		master.stop();
-		master.createByUnBlock();
+		master.createByUnBlock();*/
+	//43 b0 ca ed
+		byte[] bs=new byte[]{(byte)0x43,(byte)0xb0,(byte)0xca,(byte)0xed};
+		System.out.println("ABCD:"+new ABCD().decode(bs,0).getValue());
+		System.out.println("CDAB:"+new CDAB().decode(bs,0).getValue());
+		System.out.println("DCBA:"+new DCBA().decode(bs,0).getValue());
+		System.out.println("BADC:"+new BADC().decode(bs,0).getValue());
+		System.out.println("P_ABCD:"+new P_ABCD().decode(bs,0).getValue());
+		System.out.println("P_CDAB:"+new P_CDAB().decode(bs,0).getValue());
+		System.out.println("AB:"+new PM_AB().decode(bs,1).getValue());
+		System.out.println("BA:"+new PM_BA().decode(bs,1).getValue());
+
+
 	}
 }

+ 3 - 3
protocol-modbus/src/test/java/TestRtuMaster.java

@@ -19,7 +19,7 @@ import java.util.Map;
 @Slf4j
 public class TestRtuMaster {
 	public static void main(String[] args) throws InterruptedException, ModbusException {
-		ModbusRtuMasterBuilder master = new ModbusRtuMasterBuilder("COM2");
+		ModbusRtuMasterBuilder master = new ModbusRtuMasterBuilder("COM1");
 		master.setBaudRate(9600);
 		master.createByUnBlock();
 /*
@@ -31,8 +31,8 @@ public class TestRtuMaster {
 		master3.createByUnBlock();*/
 		Thread.sleep(5000L);
 		Map<Integer, ModbusDataTypeEnum> map = new HashMap<>();
-		for (int i = 0; i < 43; i+=1) {
-			map.put(i , ModbusDataTypeEnum.PM_AB);
+		for (int i = 0; i < 60; i+=2) {
+			map.put(i , ModbusDataTypeEnum.CDAB);
 		}
 		List<Obj4RequestRegister> ll = ModbusRequestDataUtils.splitModbusRequest(map, 1, FunctionCode.READ_HOLDING_REGISTERS);