chenwen 2 лет назад
Родитель
Сommit
b22ae2c7a2

+ 24 - 1
src/main/java/com/hb/proj/gather/protocol/GatherRespParser.java

@@ -13,7 +13,7 @@ public class GatherRespParser {
 	private final static  Logger logger = LoggerFactory.getLogger(GatherRespParser.class);
 			
 	/**
-	 * 解析消息中的数据部分
+	 * 解析消息中的数据部分 每个数据项 4字节
 	 * @param byteBuf
 	 * @param  startIndex 数据区开始索引
 	 * @param  dataLen  数据区长度
@@ -31,5 +31,28 @@ public class GatherRespParser {
 		return rtns;
 		
 		
+	}
+	
+	
+	/**
+	 * 解析消息中的数据部分 每个数据项 2字节
+	 * @param byteBuf
+	 * @param startIndex
+	 * @param dataLen
+	 * @return
+	 */
+	public static List<Short> parseShort(ByteBuf byteBuf ,int startIndex,int dataLen) {
+		byteBuf.readerIndex(startIndex);
+		List<Short> rtns=new ArrayList<Short>();
+		while(true) {
+			rtns.add(byteBuf.readShort() ); //顺序读取,readIndex 自动后移
+			if(byteBuf.readerIndex()>=(startIndex+dataLen)) {
+				break;
+			}
+		}
+		logger.info("数据解析完:{}",rtns);
+		return rtns;
+		
+		
 	}
 }

+ 5 - 0
src/main/java/com/hb/proj/gather/protocol/ZLOpdProtCMDEnum.java

@@ -1,5 +1,10 @@
 package com.hb.proj.gather.protocol;
 
+/**
+ * 数据读取指令 枚举类
+ * @author cwen
+ *
+ */
 public enum ZLOpdProtCMDEnum {
 	
 	//20[应返回数据区字节数] 一次读取到油压、套压、回压、井口温度、载荷 01 03 01 2c 00 0a    校验位两字节         应返回字节数     每个数据项字节数  参数编码表

+ 1 - 16
src/main/java/com/hb/proj/gather/protocol/ZLOpdProtHandler.java

@@ -1,7 +1,5 @@
 package com.hb.proj.gather.protocol;
 
-import java.util.List;
-
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -58,9 +56,6 @@ public class ZLOpdProtHandler extends ChannelInboundHandlerAdapter {
 			return ;
 		}
 		
-		//byte[] allBytes=new byte[byteBuf.readableBytes()];
-		//byteBuf.readBytes(allBytes);
-		
 		
 		//开头两字节且不以0103开头,就认为是心跳数据-作为设备号,该方法并不可靠,有可能把采集的残包数据当作心跳
 		if(byteCount==2&&(!hexmsg.startsWith("0103"))) { 
@@ -82,19 +77,9 @@ public class ZLOpdProtHandler extends ChannelInboundHandlerAdapter {
 			}
 			
 			//读取头部+数据区
-			/*
-			byte[] headAndDatas=new byte[datalen+headBtyCount];
-			byteBuf.readerIndex(0);  //重置读索引至起始
-			byteBuf.readBytes(headAndDatas);
-			*/
 			byte[] headAndDatas=ByteBufUtil.getBytes(byteBuf,0,headBtyCount+datalen);
 			
 			//读取校验位
-			/*
-			byte[] crc16=new byte[crc16BtyCount];
-			byteBuf.readerIndex(datalen+headBtyCount); 
-			byteBuf.readBytes(crc16);*/
-			
 			byte[] crc16=ByteBufUtil.getBytes(byteBuf,headBtyCount+datalen,2);
 			
 			int calCrc16=Crc16Utils.getCRC(headAndDatas);
@@ -103,7 +88,7 @@ public class ZLOpdProtHandler extends ChannelInboundHandlerAdapter {
 			
 			if(ByteUtils.byte2ToIntHL(crc16)==calCrc16) {  //crc校验通过
 				
-				List<Float> smpdatas=GatherRespParser.parseFloat(byteBuf,headBtyCount,datalen);
+				GatherRespParser.parseShort(byteBuf,headBtyCount,datalen);
 				
 			}
 		}

+ 5 - 0
src/main/java/com/hb/proj/gather/server/MyChannelInitializer.java

@@ -5,6 +5,11 @@ import com.hb.proj.gather.protocol.ZLOpdProtHandler;
 import io.netty.channel.ChannelInitializer;
 import io.netty.channel.socket.SocketChannel;
 
+/**
+ * netty 通道消息处理器配置
+ * @author cwen
+ *
+ */
 public class MyChannelInitializer extends ChannelInitializer<SocketChannel> {
 
 	/*

+ 5 - 0
src/main/java/com/hb/proj/gather/server/NettyGatherServer.java

@@ -13,6 +13,11 @@ import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.channel.socket.nio.NioServerSocketChannel;
 import jakarta.annotation.PreDestroy;
 
+/**
+ * 采集服务程序 启动监听 收发数据
+ * @author cwen
+ *
+ */
 @Component
 public class NettyGatherServer {