| | |
| | | * @param param |
| | | * @return |
| | | */ |
| | | String gateControl(ApiInoutData param); |
| | | String gateControl(ApiInoutData param, InoutData data); |
| | | |
| | | |
| | | /** |
| | |
| | | * @param param |
| | | * @return |
| | | */ |
| | | String lightControl(ApiInoutData param); |
| | | String lightControl(ApiInoutData param, InoutData data); |
| | | |
| | | |
| | | /** |
| | |
| | | TCP_BHZH_GRAIN_FM_V1("TCP_BHZH_GRAIN_FM_V1", "馿µ·æºè½æ 线主æºçV1"), |
| | | TCP_BHZH_GRAIN_V2("TCP_BHZH_GRAIN_V2", "馿µ·æºè½æçº¿çV2"), |
| | | TCP_BHZH_VERB_V2("TCP_BHZH_VERB_V2", "馿µ·æ§å¶ææºè½æçº¿çV2"), |
| | | TCP_BHZH_INOTU_V1("TCP_BHZH_INOTU_V1", "馿µ·æºè½åºå
¥åºæ§å¶å¨åè®®"), |
| | | TCP_BHZH_INOUT_V1("TCP_BHZH_INOUT_V1", "馿µ·æºè½åºå
¥åºæ§å¶å¨åè®®"), |
| | | FZZY_OPENAPI_HTTP("FZZY_OPENAPI_HTTP", "飿£è´è¿API-HTTPåè®®"); |
| | | private String code; |
| | | private String msg; |
| | |
| | | <artifactId>igds-core</artifactId> |
| | | <version>${igds.version}</version> |
| | | </dependency> |
| | | <!-- å¼å
¥è½èå
--> |
| | | <dependency> |
| | | <groupId>com.ld.igds</groupId> |
| | | <artifactId>igds-es</artifactId> |
| | | <version>${igds.version}</version> |
| | | </dependency> |
| | | |
| | | <!-- å¼å
¥IOå
--> |
| | | <dependency> |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ld.igds.protocol.bhzn.inout; |
| | | |
| | | import com.ld.igds.inout.ApiInoutService; |
| | | import com.ld.igds.inout.dto.ApiInoutData; |
| | | import com.ld.igds.inout.dto.InoutData; |
| | | import com.ld.igds.io.constant.ProtocolEnum; |
| | | import com.ld.igds.protocol.bhzn.inout.client.BHZNClientEngine; |
| | | import com.ld.igds.protocol.bhzn.utils.CRC16; |
| | | import com.ld.igds.util.BytesUtil; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.io.UnsupportedEncodingException; |
| | | |
| | | /** |
| | | * @Desc: å°ç£
主æ§åè®®å®ç°ï¼åºå
¥åºç¸å
³æ¥å£ |
| | | */ |
| | | @Slf4j |
| | | @Component |
| | | public class BHZNWeightServiceImpl implements ApiInoutService { |
| | | |
| | | // @Autowired |
| | | // private Inoutservice inoutservice; |
| | | |
| | | @Override |
| | | public String getProtocol() { |
| | | return ProtocolEnum.TCP_BHZH_INOUT_V1.getCode(); |
| | | } |
| | | |
| | | /** |
| | | * @param param |
| | | * @return |
| | | */ |
| | | @Override |
| | | public ApiInoutData getPlateNum(ApiInoutData param) { |
| | | return param; |
| | | } |
| | | |
| | | /** |
| | | * ä¸éè¦å®ç° |
| | | * |
| | | * @param param |
| | | * @return |
| | | */ |
| | | @Override |
| | | public String initLpr(ApiInoutData param) { |
| | | return "å½åæ¨¡å¼æ éåå§å车çè¯å«"; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * ææ¶ä¸å®ç° |
| | | * |
| | | * @param param deviceIdåactionCode ä¸å¯ä¸ºç©º |
| | | * @return |
| | | */ |
| | | @Override |
| | | public String gateControl(ApiInoutData param, InoutData data) { |
| | | try{ |
| | | // ReqGate reqGate = new ReqGate(); |
| | | BHZNClientEngine test = new BHZNClientEngine( |
| | | param.getIp(), param.getPort()); |
| | | test.start(); |
| | | String msg = "<BHZN>{\"cmd\":226,\"content\":{" + |
| | | "\"value\":[1,1]," + |
| | | "\"LEDValue\":[1,1]" + |
| | | "},\"orderId\":\"5009\",\"result\":\"\",\"sign\":\"0000\",\"stNum\":1,\"version\":\"V1.0000000\"}<END>"; |
| | | |
| | | // Thread.sleep(3000L); |
| | | |
| | | test.send(BytesUtil.hexStrToBytes(msg)); |
| | | }catch (Exception e){ |
| | | log.error(e.getMessage(),e); |
| | | return "ERROR"; |
| | | } |
| | | |
| | | return "SUCCESS"; |
| | | } |
| | | |
| | | @Override |
| | | public String lightControl(ApiInoutData param, InoutData data) { |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public String noticeRadio(ApiInoutData param, InoutData data) { |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public String noticeLed(ApiInoutData param, InoutData data) { |
| | | // todo |
| | | |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * å¢å ç½åå --è°ç¨å线ç¨å®ç° |
| | | * |
| | | * @param param deviceIdåactionCode ä¸å¯ä¸ºç©º |
| | | * @return |
| | | */ |
| | | public String addWhitePlate(ApiInoutData param) { |
| | | |
| | | return "SUCCESS"; |
| | | } |
| | | |
| | | /** |
| | | * å é¤ç½åå---è°ç¨å线ç¨å®ç° |
| | | * |
| | | * @param param |
| | | * @return |
| | | */ |
| | | public String delWhitePlate(ApiInoutData param) { |
| | | |
| | | return "SUCCESS"; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @param param |
| | | * @return |
| | | */ |
| | | @Override |
| | | public ApiInoutData getIcCardNum(ApiInoutData param) { |
| | | return param; |
| | | } |
| | | |
| | | |
| | | |
| | | public static void main(String[] args) { |
| | | try{ |
| | | String str1 = "许彬",str2="粤A123456",str3 = "3å·ä»" ,str4 = "å°éº¦",str5="286000",str6 = "30000",str7 ="åæ ¼"; |
| | | String test = "æç±ä¸èªè½¯ä»¶"; |
| | | //78340100290000000000000000 1400 0B00 01 02 00 01 0C 00 |
| | | |
| | | String lengh = BytesUtil.tran_LH(BytesUtil.intToHexStr(8+test.length())); |
| | | String msg = "78340100290000000000000000 " + lengh+ "0B00010200010C00"; |
| | | msg = msg + convertStr(test); |
| | | msg = msg.toUpperCase(); |
| | | |
| | | // String t = "78 34 01 00 29 BC FD 00 00 00 00 00 00 14 00 01 00 01 02 06 01 0C 00 CE D2 B0 AE D6 D0 BA BD C8 ED BC FE"; |
| | | // t = t.replaceAll(" ",""); |
| | | String check = BytesUtil.tran_LH(BytesUtil.intToHexStr(CRC16.calcCrc16(BytesUtil.hexStrToBytes(msg)))); |
| | | System.out.println(check); |
| | | |
| | | msg+=check; |
| | | msg += "A5"; |
| | | |
| | | System.out.println(msg); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | //hexToBytesæ¹æ³ï¼åå
è¿å¶å符串转byte[]ï¼ç½ä¸å¾å¤ |
| | | //getRepair0æ¹æ³ï¼è¡¥0çï¼å¾ç®åï¼èªå·±å®ç°å§ |
| | | /** |
| | | * |
| | | * new StringBuffer(str).reverse() |
| | | * 转askç |
| | | * @param msg |
| | | * @return |
| | | * @throws UnsupportedEncodingException |
| | | */ |
| | | public static String convertStr(String msg) throws Exception { |
| | | //å
æå符串ægb2312转æbyteæ°ç» |
| | | byte[] bytes = msg.getBytes("gb2312"); |
| | | StringBuilder gbString = new StringBuilder(); |
| | | |
| | | for (byte b : bytes) |
| | | { |
| | | // åç¨Integerä¸çæ¹æ³ï¼ææ¯ä¸ªbyteè½¬æ¢æ16è¿å¶è¾åº |
| | | String temp = Integer.toHexString(b); |
| | | //夿è¿è¡æªå |
| | | if(temp.length()>=8){ |
| | | temp = temp.substring(6, 8); |
| | | } |
| | | gbString.append(temp); |
| | | } |
| | | return gbString.toString(); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ld.igds.protocol.bhzn.inout.client; |
| | | |
| | | import com.ld.igds.util.BytesUtil; |
| | | import com.ld.io.api.InvokeResult; |
| | | import io.netty.bootstrap.Bootstrap; |
| | | import io.netty.buffer.Unpooled; |
| | | import io.netty.channel.*; |
| | | import io.netty.channel.oio.OioEventLoopGroup; |
| | | import io.netty.channel.socket.SocketChannel; |
| | | import io.netty.channel.socket.oio.OioSocketChannel; |
| | | import io.netty.handler.codec.bytes.ByteArrayDecoder; |
| | | import io.netty.handler.codec.bytes.ByteArrayEncoder; |
| | | |
| | | /** |
| | | * åºå
¥åºæ§å¶å¨ æå¡ï¼å¹³å°ä½ä¸ºå®¢æ·ç«¯ä¸»å¨è¿æ¥ |
| | | * |
| | | * @author vicne |
| | | * @description: |
| | | * @version: |
| | | */ |
| | | public class BHZNClientEngine implements Runnable { |
| | | |
| | | private String host; |
| | | private int port; |
| | | // public ChannelFuture channelFuture; |
| | | public Channel channel; |
| | | |
| | | public BHZNClientEngine(String host, int port) { |
| | | this.host = host; |
| | | this.port = port; |
| | | } |
| | | public void start() { |
| | | Thread thread = new Thread(this); |
| | | thread.start(); |
| | | } |
| | | |
| | | @Override |
| | | public void run() { |
| | | try { |
| | | startRun(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | public void startRun() throws Exception { |
| | | EventLoopGroup group = new OioEventLoopGroup(); |
| | | Bootstrap b = new Bootstrap(); |
| | | // b.option(ChannelOption.SO_KEEPALIVE, true); |
| | | b.group(group).channel(OioSocketChannel.class) |
| | | .handler(new ChannelInitializer<SocketChannel>() { |
| | | @Override |
| | | protected void initChannel(SocketChannel ch) |
| | | throws Exception { |
| | | ChannelPipeline p = ch.pipeline(); |
| | | // å符串解ç å ç¼ç |
| | | p.addLast("decoder", new ByteArrayDecoder()); |
| | | p.addLast("encoder", new ByteArrayEncoder()); |
| | | // èªå·±çé»è¾Handler |
| | | p.addLast("handler", new ClientHandler()); |
| | | } |
| | | }); |
| | | |
| | | // åèµ·å¼æ¥è¿æ¥è¯·æ±ï¼ç»å®è¿æ¥ç«¯å£åhostä¿¡æ¯ |
| | | ChannelFuture channelFuture = b.connect(host, port); |
| | | this.channel = channelFuture.channel(); |
| | | // channelFuture.channel().closeFuture().sync(); |
| | | |
| | | channelFuture.addListener(new ChannelFutureListener() { |
| | | @Override |
| | | public void operationComplete(ChannelFuture arg0) throws Exception { |
| | | if (channelFuture.isSuccess()) { |
| | | System.out.println("è¿æ¥æå¡å¨æå"); |
| | | } else { |
| | | System.out.println("è¿æ¥æå¡å¨å¤±è´¥"); |
| | | channelFuture.cause().printStackTrace(); |
| | | group.shutdownGracefully(); // å
³é线ç¨ç» |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | public InvokeResult send(byte[] array) throws InterruptedException { |
| | | if (null == channel) { |
| | | return InvokeResult.SOCKET_NOT_CREATE; |
| | | } |
| | | if (!channel.isActive()) { |
| | | return InvokeResult.CHANNEL_CLOSED; |
| | | } |
| | | channel.writeAndFlush(Unpooled.copiedBuffer(array)).sync(); |
| | | |
| | | return InvokeResult.SUCCESS; |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | try { |
| | | |
| | | String msg = "000000000006010600070012"; |
| | | BHZNClientEngine test = new BHZNClientEngine( |
| | | "192.168.1.5", 502); |
| | | |
| | | test.start(); |
| | | |
| | | msg = "000000000006010300000007"; |
| | | |
| | | // Thread.sleep(3000L); |
| | | |
| | | test.send(BytesUtil.hexStrToBytes(msg)); |
| | | |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | public Channel getChannel() { |
| | | return channel; |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ld.igds.protocol.bhzn.inout.client; |
| | | |
| | | import com.ld.igds.util.BytesUtil; |
| | | import io.netty.channel.ChannelHandlerContext; |
| | | import io.netty.channel.SimpleChannelInboundHandler; |
| | | import io.netty.util.internal.logging.InternalLogger; |
| | | import io.netty.util.internal.logging.InternalLoggerFactory; |
| | | |
| | | import java.net.InetSocketAddress; |
| | | |
| | | /** |
| | | * Handles a client-side channel. |
| | | */ |
| | | |
| | | public class ClientHandler extends SimpleChannelInboundHandler<Object> { |
| | | private final InternalLogger log = InternalLoggerFactory.getInstance(this |
| | | .getClass()); |
| | | @Override |
| | | public void channelActive(ChannelHandlerContext ctx) { |
| | | InetSocketAddress insocket = (InetSocketAddress) ctx.channel() |
| | | .remoteAddress(); |
| | | log.info("åºå
¥åºæ§å¶å¨æåè¿æ¥ï¼IP={},port={}", insocket.getAddress() |
| | | .getHostAddress(), insocket.getPort()); |
| | | } |
| | | |
| | | @Override |
| | | public void channelInactive(ChannelHandlerContext ctx) throws Exception { |
| | | super.channelInactive(ctx); |
| | | |
| | | InetSocketAddress insocket = (InetSocketAddress) ctx.channel() |
| | | .remoteAddress(); |
| | | |
| | | log.info("åºå
¥åºæ§å¶å¨æå¼è¿æ¥ï¼IP={},port={}", insocket.getAddress(),insocket.getPort()); |
| | | } |
| | | |
| | | @Override |
| | | public void channelRead0(ChannelHandlerContext ctx, Object msg) |
| | | throws Exception { |
| | | byte[] bytes = (byte[]) msg; |
| | | |
| | | log.info("åºå
¥åºæ§å¶å¨è¿åä¿¡æ¯=" + BytesUtil.bytesToString(bytes)); |
| | | |
| | | InetSocketAddress insocket = (InetSocketAddress) ctx.channel() |
| | | .remoteAddress(); |
| | | } |
| | | |
| | | @Override |
| | | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { |
| | | ctx.close(); |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ld.igds.protocol.bhzn.inout.client; |
| | | |
| | | import lombok.Data; |
| | | |
| | | /** |
| | | * @version v1.0 |
| | | * @ProjectName: igds-v4 |
| | | * @ClassName: ReqGate |
| | | * @Author: vince |
| | | * @Date: 2023/7/4 11:53 |
| | | */ |
| | | @Data |
| | | public class ReqGate { |
| | | // private Integer StNum = 1; |
| | | // private Integer Cmd = 226; |
| | | private Integer[] Value = new Integer[]{1,1}; |
| | | private Integer[] LEDValue = new Integer[]{1,1}; |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ld.igds.protocol.bhzn.utils; |
| | | |
| | | public class CRC { |
| | | |
| | | /*å¯åå¨åå§å¼*/ |
| | | private final int regInitValue; |
| | | private int register; |
| | | /*CRC å¤é¡¹å¼ modbus å¤é¡¹å¼0x8005*/ |
| | | private final int polynomial; |
| | | |
| | | public CRC() { |
| | | this(0xffff, 0x8005); |
| | | } |
| | | |
| | | public CRC(int register, int polynomial) { |
| | | this.register = register; |
| | | this.regInitValue = register; |
| | | this.polynomial = Integer.reverse(polynomial)>>>Integer.numberOfLeadingZeros(polynomial); |
| | | } |
| | | |
| | | private void byteCRC(byte byt) { |
| | | register ^= (byt&0xff); |
| | | for(int i=0; i<8; i++){ |
| | | if((register&1)==0){ |
| | | register >>= 1; |
| | | }else{ |
| | | register = (register >> 1)^polynomial; |
| | | } |
| | | } |
| | | } |
| | | |
| | | public synchronized int crc(byte[] byts) { |
| | | if(null==byts||byts.length==0){ |
| | | return 0; |
| | | } |
| | | for(int i=0; i<byts.length; i++){ |
| | | byteCRC(byts[i]); |
| | | } |
| | | int retn = register; |
| | | register = this.regInitValue; |
| | | return retn; |
| | | } |
| | | |
| | | public static void main(String[] args){ |
| | | CRC crc = new CRC(); |
| | | byte[] data = {(byte)0x81, 0x12, 0x17, (byte)0xfe}; |
| | | System.out.println(Integer.toHexString(crc.crc(data))); |
| | | data = new byte[]{2, 7}; |
| | | System.out.println(Integer.toHexString(crc.crc(data))); |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ld.igds.protocol.bhzn.utils; |
| | | |
| | | /*** |
| | | * |
| | | * @author Andy |
| | | * |
| | | */ |
| | | public class CRC16 { |
| | | |
| | | static byte[] crc16_tab_h = { (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, |
| | | (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, |
| | | (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, |
| | | (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, |
| | | (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, |
| | | (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, |
| | | (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, |
| | | (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, |
| | | (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, |
| | | (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, |
| | | (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, |
| | | (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, |
| | | (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, |
| | | (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, |
| | | (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, |
| | | (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, |
| | | (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, |
| | | (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, |
| | | (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, |
| | | (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, |
| | | (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, |
| | | (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, |
| | | (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, |
| | | (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, |
| | | (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, |
| | | (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, |
| | | (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, |
| | | (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, |
| | | (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, |
| | | (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, |
| | | (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, |
| | | (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, |
| | | (byte) 0x81, (byte) 0x40 }; |
| | | |
| | | static byte[] crc16_tab_l = { (byte) 0x00, (byte) 0xC0, (byte) 0xC1, (byte) 0x01, (byte) 0xC3, (byte) 0x03, |
| | | (byte) 0x02, (byte) 0xC2, (byte) 0xC6, (byte) 0x06, (byte) 0x07, (byte) 0xC7, (byte) 0x05, (byte) 0xC5, |
| | | (byte) 0xC4, (byte) 0x04, (byte) 0xCC, (byte) 0x0C, (byte) 0x0D, (byte) 0xCD, (byte) 0x0F, (byte) 0xCF, |
| | | (byte) 0xCE, (byte) 0x0E, (byte) 0x0A, (byte) 0xCA, (byte) 0xCB, (byte) 0x0B, (byte) 0xC9, (byte) 0x09, |
| | | (byte) 0x08, (byte) 0xC8, (byte) 0xD8, (byte) 0x18, (byte) 0x19, (byte) 0xD9, (byte) 0x1B, (byte) 0xDB, |
| | | (byte) 0xDA, (byte) 0x1A, (byte) 0x1E, (byte) 0xDE, (byte) 0xDF, (byte) 0x1F, (byte) 0xDD, (byte) 0x1D, |
| | | (byte) 0x1C, (byte) 0xDC, (byte) 0x14, (byte) 0xD4, (byte) 0xD5, (byte) 0x15, (byte) 0xD7, (byte) 0x17, |
| | | (byte) 0x16, (byte) 0xD6, (byte) 0xD2, (byte) 0x12, (byte) 0x13, (byte) 0xD3, (byte) 0x11, (byte) 0xD1, |
| | | (byte) 0xD0, (byte) 0x10, (byte) 0xF0, (byte) 0x30, (byte) 0x31, (byte) 0xF1, (byte) 0x33, (byte) 0xF3, |
| | | (byte) 0xF2, (byte) 0x32, (byte) 0x36, (byte) 0xF6, (byte) 0xF7, (byte) 0x37, (byte) 0xF5, (byte) 0x35, |
| | | (byte) 0x34, (byte) 0xF4, (byte) 0x3C, (byte) 0xFC, (byte) 0xFD, (byte) 0x3D, (byte) 0xFF, (byte) 0x3F, |
| | | (byte) 0x3E, (byte) 0xFE, (byte) 0xFA, (byte) 0x3A, (byte) 0x3B, (byte) 0xFB, (byte) 0x39, (byte) 0xF9, |
| | | (byte) 0xF8, (byte) 0x38, (byte) 0x28, (byte) 0xE8, (byte) 0xE9, (byte) 0x29, (byte) 0xEB, (byte) 0x2B, |
| | | (byte) 0x2A, (byte) 0xEA, (byte) 0xEE, (byte) 0x2E, (byte) 0x2F, (byte) 0xEF, (byte) 0x2D, (byte) 0xED, |
| | | (byte) 0xEC, (byte) 0x2C, (byte) 0xE4, (byte) 0x24, (byte) 0x25, (byte) 0xE5, (byte) 0x27, (byte) 0xE7, |
| | | (byte) 0xE6, (byte) 0x26, (byte) 0x22, (byte) 0xE2, (byte) 0xE3, (byte) 0x23, (byte) 0xE1, (byte) 0x21, |
| | | (byte) 0x20, (byte) 0xE0, (byte) 0xA0, (byte) 0x60, (byte) 0x61, (byte) 0xA1, (byte) 0x63, (byte) 0xA3, |
| | | (byte) 0xA2, (byte) 0x62, (byte) 0x66, (byte) 0xA6, (byte) 0xA7, (byte) 0x67, (byte) 0xA5, (byte) 0x65, |
| | | (byte) 0x64, (byte) 0xA4, (byte) 0x6C, (byte) 0xAC, (byte) 0xAD, (byte) 0x6D, (byte) 0xAF, (byte) 0x6F, |
| | | (byte) 0x6E, (byte) 0xAE, (byte) 0xAA, (byte) 0x6A, (byte) 0x6B, (byte) 0xAB, (byte) 0x69, (byte) 0xA9, |
| | | (byte) 0xA8, (byte) 0x68, (byte) 0x78, (byte) 0xB8, (byte) 0xB9, (byte) 0x79, (byte) 0xBB, (byte) 0x7B, |
| | | (byte) 0x7A, (byte) 0xBA, (byte) 0xBE, (byte) 0x7E, (byte) 0x7F, (byte) 0xBF, (byte) 0x7D, (byte) 0xBD, |
| | | (byte) 0xBC, (byte) 0x7C, (byte) 0xB4, (byte) 0x74, (byte) 0x75, (byte) 0xB5, (byte) 0x77, (byte) 0xB7, |
| | | (byte) 0xB6, (byte) 0x76, (byte) 0x72, (byte) 0xB2, (byte) 0xB3, (byte) 0x73, (byte) 0xB1, (byte) 0x71, |
| | | (byte) 0x70, (byte) 0xB0, (byte) 0x50, (byte) 0x90, (byte) 0x91, (byte) 0x51, (byte) 0x93, (byte) 0x53, |
| | | (byte) 0x52, (byte) 0x92, (byte) 0x96, (byte) 0x56, (byte) 0x57, (byte) 0x97, (byte) 0x55, (byte) 0x95, |
| | | (byte) 0x94, (byte) 0x54, (byte) 0x9C, (byte) 0x5C, (byte) 0x5D, (byte) 0x9D, (byte) 0x5F, (byte) 0x9F, |
| | | (byte) 0x9E, (byte) 0x5E, (byte) 0x5A, (byte) 0x9A, (byte) 0x9B, (byte) 0x5B, (byte) 0x99, (byte) 0x59, |
| | | (byte) 0x58, (byte) 0x98, (byte) 0x88, (byte) 0x48, (byte) 0x49, (byte) 0x89, (byte) 0x4B, (byte) 0x8B, |
| | | (byte) 0x8A, (byte) 0x4A, (byte) 0x4E, (byte) 0x8E, (byte) 0x8F, (byte) 0x4F, (byte) 0x8D, (byte) 0x4D, |
| | | (byte) 0x4C, (byte) 0x8C, (byte) 0x44, (byte) 0x84, (byte) 0x85, (byte) 0x45, (byte) 0x87, (byte) 0x47, |
| | | (byte) 0x46, (byte) 0x86, (byte) 0x82, (byte) 0x42, (byte) 0x43, (byte) 0x83, (byte) 0x41, (byte) 0x81, |
| | | (byte) 0x80, (byte) 0x40 }; |
| | | |
| | | /** |
| | | * 计ç®CRC16æ ¡éª |
| | | * |
| | | * @param data |
| | | * éè¦è®¡ç®çæ°ç» |
| | | * @return CRC16æ ¡éªå¼ |
| | | */ |
| | | public static int calcCrc16(byte[] data) { |
| | | return calcCrc16(data, 0, data.length); |
| | | } |
| | | |
| | | /** |
| | | * 计ç®CRC16æ ¡éª |
| | | * |
| | | * @param data |
| | | * éè¦è®¡ç®çæ°ç» |
| | | * @param offset |
| | | * èµ·å§ä½ç½® |
| | | * @param len |
| | | * é¿åº¦ |
| | | * @return CRC16æ ¡éªå¼ |
| | | */ |
| | | public static int calcCrc16(byte[] data, int offset, int len) { |
| | | return calcCrc16(data, offset, len, 0xffff); |
| | | } |
| | | |
| | | /** |
| | | * 计ç®CRC16æ ¡éª |
| | | * |
| | | * @param data |
| | | * éè¦è®¡ç®çæ°ç» |
| | | * @param offset |
| | | * èµ·å§ä½ç½® |
| | | * @param len |
| | | * é¿åº¦ |
| | | * @param preval |
| | | * ä¹åçæ ¡éªå¼ |
| | | * @return CRC16æ ¡éªå¼ |
| | | */ |
| | | public static int calcCrc16(byte[] data, int offset, int len, int preval) { |
| | | int ucCRCHi = (preval & 0xff00) >> 8; |
| | | int ucCRCLo = preval & 0x00ff; |
| | | int iIndex; |
| | | for (int i = 0; i < len; ++i) { |
| | | iIndex = (ucCRCLo ^ data[offset + i]) & 0x00ff; |
| | | ucCRCLo = ucCRCHi ^ crc16_tab_h[iIndex]; |
| | | ucCRCHi = crc16_tab_l[iIndex]; |
| | | } |
| | | return ((ucCRCHi & 0x00ff) << 8) | (ucCRCLo & 0x00ff) & 0xffff; |
| | | } |
| | | |
| | | // æµè¯ |
| | | public static void main(String[] args) { |
| | | // 0x02 05 00 03 FF 00 , crc16=7C 09 |
| | | int crc = CRC16.calcCrc16(new byte[] { 0x02, 0x05, 0x00, 0x03, (byte) 0xff, 0x00 }); |
| | | System.out.println(String.format("0x%04x", crc)); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ld.igds.protocol.bhzn.utils; |
| | | |
| | | /** |
| | | *计ç®CRC8æ ¡éªå¼ |
| | | */ |
| | | public class CRC8 { |
| | | |
| | | public static void main(String[] args) { |
| | | |
| | | } |
| | | |
| | | static short[] gCrc16Table = { (short) 0x0000, (short) 0x1021, (short) 0x2042, (short) 0x3063, (short) 0x4084, |
| | | (short) 0x50a5, (short) 0x60c6, (short) 0x70e7, (short) 0xd94c, (short) 0xc96d, (short) 0xf90e, |
| | | (short) 0xe92f, (short) 0x99c8, (short) 0x89e9, (short) 0xb98a, (short) 0xa9ab, (short) 0x5844, |
| | | (short) 0x4865, (short) 0x7806, (short) 0x6827, (short) 0x18c0, (short) 0x08e1, (short) 0x3882, |
| | | (short) 0x28a3, (short) 0xcb7d, (short) 0xdb5c, (short) 0xeb3f, (short) 0xfb1e, (short) 0x8bf9, |
| | | (short) 0x9bd8, (short) 0xabbb, (short) 0xbb9a, (short) 0x4a75, (short) 0x5a54, (short) 0x6a37, |
| | | (short) 0x7a16, (short) 0x0af1, (short) 0x1ad0, (short) 0x2ab3, (short) 0x3a92, (short) 0xfd2e, |
| | | (short) 0xed0f, (short) 0xdd6c, (short) 0xcd4d, (short) 0xbdaa, (short) 0xad8b, (short) 0x9de8, |
| | | (short) 0x8dc9, (short) 0xc9cc, (short) 0xd9ed, (short) 0xe98e, (short) 0xf9af, (short) 0x8948, |
| | | (short) 0x9969, (short) 0xa90a, (short) 0xb92b, (short) 0x5af5, (short) 0x4ad4, (short) 0x7ab7, |
| | | (short) 0x6a96, (short) 0x1a71, (short) 0x0a50, (short) 0x3a33, (short) 0x2a12, (short) 0xdbfd, |
| | | (short) 0xcbdc, (short) 0xfbbf, (short) 0xeb9e, (short) 0x9b79, (short) 0x8b58, (short) 0xbb3b, |
| | | (short) 0xab1a, (short) 0x6ca6, (short) 0x7c87, (short) 0x4ce4, (short) 0x5cc5, (short) 0x2c22, |
| | | (short) 0x3c03, (short) 0x0c60, (short) 0x1c41, (short) 0x8108, (short) 0x9129, (short) 0xa14a, |
| | | (short) 0xb16b, (short) 0xc18c, (short) 0xd1ad, (short) 0xe1ce, (short) 0xf1ef, (short) 0x1231, |
| | | (short) 0x0210, (short) 0x3273, (short) 0x2252, (short) 0x52b5, (short) 0x4294, (short) 0x72f7, |
| | | (short) 0x62d6, (short) 0x9339, (short) 0x8318, (short) 0xb37b, (short) 0xa35a, (short) 0xd3bd, |
| | | (short) 0xc39c, (short) 0xf3ff, (short) 0xe3de, (short) 0x2462, (short) 0x3443, (short) 0x0420, |
| | | (short) 0x1401, (short) 0x64e6, (short) 0x74c7, (short) 0x44a4, (short) 0x5485, (short) 0xa56a, |
| | | (short) 0xb54b, (short) 0x8528, (short) 0x9509, (short) 0xe5ee, (short) 0xf5cf, (short) 0xc5ac, |
| | | (short) 0xd58d, (short) 0x3653, (short) 0x2672, (short) 0x1611, (short) 0x0630, (short) 0x76d7, |
| | | (short) 0x66f6, (short) 0x5695, (short) 0x46b4, (short) 0xb75b, (short) 0xa77a, (short) 0x9719, |
| | | (short) 0x8738, (short) 0xf7df, (short) 0xe7fe, (short) 0xd79d, (short) 0xc7bc, (short) 0x48c4, |
| | | (short) 0x58e5, (short) 0x6886, (short) 0x78a7, (short) 0x0840, (short) 0x1861, (short) 0x2802, |
| | | (short) 0x3823, (short) 0x83b9, (short) 0x9398, (short) 0xa3fb, (short) 0xb3da, (short) 0xc33d, |
| | | (short) 0xd31c, (short) 0xe37f, (short) 0xf35e, (short) 0x02b1, (short) 0x1290, (short) 0x22f3, |
| | | (short) 0x32d2, (short) 0x4235, (short) 0x5214, (short) 0x6277, (short) 0x7256, (short) 0xb5ea, |
| | | (short) 0xa5cb, (short) 0x95a8, (short) 0x8589, (short) 0xf56e, (short) 0xe54f, (short) 0xd52c, |
| | | (short) 0xc50d, (short) 0x34e2, (short) 0x24c3, (short) 0x14a0, (short) 0x0481, (short) 0x7466, |
| | | (short) 0x6447, (short) 0x5424, (short) 0x4405, (short) 0xa7db, (short) 0xb7fa, (short) 0x8799, |
| | | (short) 0x97b8, (short) 0xe75f, (short) 0xf77e, (short) 0xc71d, (short) 0xd73c, (short) 0x26d3, |
| | | (short) 0x36f2, (short) 0x0691, (short) 0x16b0, (short) 0x6657, (short) 0x7676, (short) 0x4615, |
| | | (short) 0x5634, (short) 0xedae, (short) 0xfd8f, (short) 0xcdec, (short) 0xddcd, (short) 0xad2a, |
| | | (short) 0xbd0b, (short) 0x8d68, (short) 0x9d49, (short) 0x7e97, (short) 0x6eb6, (short) 0x5ed5, |
| | | (short) 0x4ef4, (short) 0x3e13, (short) 0x2e32, (short) 0x1e51, (short) 0x0e70, (short) 0xff9f, |
| | | (short) 0xefbe, (short) 0xdfdd, (short) 0xcffc, (short) 0xbf1b, (short) 0xaf3a, (short) 0x9f59, |
| | | (short) 0x8f78, (short) 0x9188, (short) 0x81a9, (short) 0xb1ca, (short) 0xa1eb, (short) 0xd10c, |
| | | (short) 0xc12d, (short) 0xf14e, (short) 0xe16f, (short) 0x1080, (short) 0x00a1, (short) 0x30c2, |
| | | (short) 0x20e3, (short) 0x5004, (short) 0x4025, (short) 0x7046, (short) 0x6067, (short) 0x7c26, |
| | | (short) 0x6c07, (short) 0x5c64, (short) 0x4c45, (short) 0x3ca2, (short) 0x2c83, (short) 0x1ce0, |
| | | (short) 0x0cc1, (short) 0xef1f, (short) 0xff3e, (short) 0xcf5d, (short) 0xdf7c, (short) 0xaf9b, |
| | | (short) 0xbfba, (short) 0x8fd9, (short) 0x9ff8, (short) 0x6e17, (short) 0x7e36, (short) 0x4e55, |
| | | (short) 0x5e74, (short) 0x2e93, (short) 0x3eb2, (short) 0x0ed1, (short) 0x1ef0 }; |
| | | |
| | | static short[] crc8_tab = { (short) 0x00, (short) 0x07, (short) 0x0E, (short) 0x09, (short) 0x1C, (short) 0x1B, |
| | | (short) 0x12, (short) 0x15, (short) 0x38, (short) 0x3F, (short) 0x36, (short) 0x31, (short) 0x24, |
| | | (short) 0x23, (short) 0x2A, (short) 0x2D, (short) 0x70, (short) 0x77, (short) 0x7E, (short) 0x79, |
| | | (short) 0x6C, (short) 0x6B, (short) 0x62, (short) 0x65, (short) 0x48, (short) 0x4F, (short) 0x46, |
| | | (short) 0x41, (short) 0x54, (short) 0x53, (short) 0x5A, (short) 0x5D, (short) 0xE0, (short) 0xE7, |
| | | (short) 0xEE, (short) 0xE9, (short) 0xFC, (short) 0xFB, (short) 0xF2, (short) 0xF5, (short) 0xD8, |
| | | (short) 0xDF, (short) 0xD6, (short) 0xD1, (short) 0xC4, (short) 0xC3, (short) 0xCA, (short) 0xCD, |
| | | (short) 0x90, (short) 0x97, (short) 0x9E, (short) 0x99, (short) 0x8C, (short) 0x8B, (short) 0x82, |
| | | (short) 0x85, (short) 0xA8, (short) 0xAF, (short) 0xA6, (short) 0xA1, (short) 0xB4, (short) 0xB3, |
| | | (short) 0xBA, (short) 0xBD, (short) 0xC7, (short) 0xC0, (short) 0xC9, (short) 0xCE, (short) 0xDB, |
| | | (short) 0xDC, (short) 0xD5, (short) 0xD2, (short) 0xFF, (short) 0xF8, (short) 0xF1, (short) 0xF6, |
| | | (short) 0xE3, (short) 0xE4, (short) 0xED, (short) 0xEA, (short) 0xB7, (short) 0xB0, (short) 0xB9, |
| | | (short) 0xBE, (short) 0xAB, (short) 0xAC, (short) 0xA5, (short) 0xA2, (short) 0x8F, (short) 0x88, |
| | | (short) 0x81, (short) 0x86, (short) 0x93, (short) 0x94, (short) 0x9D, (short) 0x9A, (short) 0x27, |
| | | (short) 0x20, (short) 0x29, (short) 0x2E, (short) 0x3B, (short) 0x3C, (short) 0x35, (short) 0x32, |
| | | (short) 0x1F, (short) 0x18, (short) 0x11, (short) 0x16, (short) 0x03, (short) 0x04, (short) 0x0D, |
| | | (short) 0x0A, (short) 0x57, (short) 0x50, (short) 0x59, (short) 0x5E, (short) 0x4B, (short) 0x4C, |
| | | (short) 0x45, (short) 0x42, (short) 0x6F, (short) 0x68, (short) 0x61, (short) 0x66, (short) 0x73, |
| | | (short) 0x74, (short) 0x7D, (short) 0x7A, (short) 0x89, (short) 0x8E, (short) 0x87, (short) 0x80, |
| | | (short) 0x95, (short) 0x92, (short) 0x9B, (short) 0x9C, (short) 0xB1, (short) 0xB6, (short) 0xBF, |
| | | (short) 0xB8, (short) 0xAD, (short) 0xAA, (short) 0xA3, (short) 0xA4, (short) 0xF9, (short) 0xFE, |
| | | (short) 0xF7, (short) 0xF0, (short) 0xE5, (short) 0xE2, (short) 0xEB, (short) 0xEC, (short) 0xC1, |
| | | (short) 0xC6, (short) 0xCF, (short) 0xC8, (short) 0xDD, (short) 0xDA, (short) 0xD3, (short) 0xD4, |
| | | (short) 0x69, (short) 0x6E, (short) 0x67, (short) 0x60, (short) 0x75, (short) 0x72, (short) 0x7B, |
| | | (short) 0x7C, (short) 0x51, (short) 0x56, (short) 0x5F, (short) 0x58, (short) 0x4D, (short) 0x4A, |
| | | (short) 0x43, (short) 0x44, (short) 0x19, (short) 0x1E, (short) 0x17, (short) 0x10, (short) 0x05, |
| | | (short) 0x02, (short) 0x0B, (short) 0x0C, (short) 0x21, (short) 0x26, (short) 0x2F, (short) 0x28, |
| | | (short) 0x3D, (short) 0x3A, (short) 0x33, (short) 0x34, (short) 0x4E, (short) 0x49, (short) 0x40, |
| | | (short) 0x47, (short) 0x52, (short) 0x55, (short) 0x5C, (short) 0x5B, (short) 0x76, (short) 0x71, |
| | | (short) 0x78, (short) 0x7F, (short) 0x6A, (short) 0x6D, (short) 0x64, (short) 0x63, (short) 0x3E, |
| | | (short) 0x39, (short) 0x30, (short) 0x37, (short) 0x22, (short) 0x25, (short) 0x2C, (short) 0x2B, |
| | | (short) 0x06, (short) 0x01, (short) 0x08, (short) 0x0F, (short) 0x1A, (short) 0x1D, (short) 0x14, |
| | | (short) 0x13, (short) 0xAE, (short) 0xA9, (short) 0xA0, (short) 0xA7, (short) 0xB2, (short) 0xB5, |
| | | (short) 0xBC, (short) 0xBB, (short) 0x96, (short) 0x91, (short) 0x98, (short) 0x9F, (short) 0x8A, |
| | | (short) 0x8D, (short) 0x84, (short) 0x83, (short) 0xDE, (short) 0xD9, (short) 0xD0, (short) 0xD7, |
| | | (short) 0xC2, (short) 0xC5, (short) 0xCC, (short) 0xCB, (short) 0xE6, (short) 0xE1, (short) 0xE8, |
| | | (short) 0xEF, (short) 0xFA, (short) 0xFD, (short) 0xF4, (short) 0xF3 }; |
| | | |
| | | static byte[] crc8_tab2 = { (byte) 0x00, (byte) 0x07, (byte) 0x0E, (byte) 0x09, (byte) 0x1C, (byte) 0x1B, |
| | | (byte) 0x12, (byte) 0x15, (byte) 0x38, (byte) 0x3F, (byte) 0x36, (byte) 0x31, (byte) 0x24, (byte) 0x23, |
| | | (byte) 0x2A, (byte) 0x2D, (byte) 0x70, (byte) 0x77, (byte) 0x7E, (byte) 0x79, (byte) 0x6C, (byte) 0x6B, |
| | | (byte) 0x62, (byte) 0x65, (byte) 0x48, (byte) 0x4F, (byte) 0x46, (byte) 0x41, (byte) 0x54, (byte) 0x53, |
| | | (byte) 0x5A, (byte) 0x5D, (byte) 0xE0, (byte) 0xE7, (byte) 0xEE, (byte) 0xE9, (byte) 0xFC, (byte) 0xFB, |
| | | (byte) 0xF2, (byte) 0xF5, (byte) 0xD8, (byte) 0xDF, (byte) 0xD6, (byte) 0xD1, (byte) 0xC4, (byte) 0xC3, |
| | | (byte) 0xCA, (byte) 0xCD, (byte) 0x90, (byte) 0x97, (byte) 0x9E, (byte) 0x99, (byte) 0x8C, (byte) 0x8B, |
| | | (byte) 0x82, (byte) 0x85, (byte) 0xA8, (byte) 0xAF, (byte) 0xA6, (byte) 0xA1, (byte) 0xB4, (byte) 0xB3, |
| | | (byte) 0xBA, (byte) 0xBD, (byte) 0xC7, (byte) 0xC0, (byte) 0xC9, (byte) 0xCE, (byte) 0xDB, (byte) 0xDC, |
| | | (byte) 0xD5, (byte) 0xD2, (byte) 0xFF, (byte) 0xF8, (byte) 0xF1, (byte) 0xF6, (byte) 0xE3, (byte) 0xE4, |
| | | (byte) 0xED, (byte) 0xEA, (byte) 0xB7, (byte) 0xB0, (byte) 0xB9, (byte) 0xBE, (byte) 0xAB, (byte) 0xAC, |
| | | (byte) 0xA5, (byte) 0xA2, (byte) 0x8F, (byte) 0x88, (byte) 0x81, (byte) 0x86, (byte) 0x93, (byte) 0x94, |
| | | (byte) 0x9D, (byte) 0x9A, (byte) 0x27, (byte) 0x20, (byte) 0x29, (byte) 0x2E, (byte) 0x3B, (byte) 0x3C, |
| | | (byte) 0x35, (byte) 0x32, (byte) 0x1F, (byte) 0x18, (byte) 0x11, (byte) 0x16, (byte) 0x03, (byte) 0x04, |
| | | (byte) 0x0D, (byte) 0x0A, (byte) 0x57, (byte) 0x50, (byte) 0x59, (byte) 0x5E, (byte) 0x4B, (byte) 0x4C, |
| | | (byte) 0x45, (byte) 0x42, (byte) 0x6F, (byte) 0x68, (byte) 0x61, (byte) 0x66, (byte) 0x73, (byte) 0x74, |
| | | (byte) 0x7D, (byte) 0x7A, (byte) 0x89, (byte) 0x8E, (byte) 0x87, (byte) 0x80, (byte) 0x95, (byte) 0x92, |
| | | (byte) 0x9B, (byte) 0x9C, (byte) 0xB1, (byte) 0xB6, (byte) 0xBF, (byte) 0xB8, (byte) 0xAD, (byte) 0xAA, |
| | | (byte) 0xA3, (byte) 0xA4, (byte) 0xF9, (byte) 0xFE, (byte) 0xF7, (byte) 0xF0, (byte) 0xE5, (byte) 0xE2, |
| | | (byte) 0xEB, (byte) 0xEC, (byte) 0xC1, (byte) 0xC6, (byte) 0xCF, (byte) 0xC8, (byte) 0xDD, (byte) 0xDA, |
| | | (byte) 0xD3, (byte) 0xD4, (byte) 0x69, (byte) 0x6E, (byte) 0x67, (byte) 0x60, (byte) 0x75, (byte) 0x72, |
| | | (byte) 0x7B, (byte) 0x7C, (byte) 0x51, (byte) 0x56, (byte) 0x5F, (byte) 0x58, (byte) 0x4D, (byte) 0x4A, |
| | | (byte) 0x43, (byte) 0x44, (byte) 0x19, (byte) 0x1E, (byte) 0x17, (byte) 0x10, (byte) 0x05, (byte) 0x02, |
| | | (byte) 0x0B, (byte) 0x0C, (byte) 0x21, (byte) 0x26, (byte) 0x2F, (byte) 0x28, (byte) 0x3D, (byte) 0x3A, |
| | | (byte) 0x33, (byte) 0x34, (byte) 0x4E, (byte) 0x49, (byte) 0x40, (byte) 0x47, (byte) 0x52, (byte) 0x55, |
| | | (byte) 0x5C, (byte) 0x5B, (byte) 0x76, (byte) 0x71, (byte) 0x78, (byte) 0x7F, (byte) 0x6A, (byte) 0x6D, |
| | | (byte) 0x64, (byte) 0x63, (byte) 0x3E, (byte) 0x39, (byte) 0x30, (byte) 0x37, (byte) 0x22, (byte) 0x25, |
| | | (byte) 0x2C, (byte) 0x2B, (byte) 0x06, (byte) 0x01, (byte) 0x08, (byte) 0x0F, (byte) 0x1A, (byte) 0x1D, |
| | | (byte) 0x14, (byte) 0x13, (byte) 0xAE, (byte) 0xA9, (byte) 0xA0, (byte) 0xA7, (byte) 0xB2, (byte) 0xB5, |
| | | (byte) 0xBC, (byte) 0xBB, (byte) 0x96, (byte) 0x91, (byte) 0x98, (byte) 0x9F, (byte) 0x8A, (byte) 0x8D, |
| | | (byte) 0x84, (byte) 0x83, (byte) 0xDE, (byte) 0xD9, (byte) 0xD0, (byte) 0xD7, (byte) 0xC2, (byte) 0xC5, |
| | | (byte) 0xCC, (byte) 0xCB, (byte) 0xE6, (byte) 0xE1, (byte) 0xE8, (byte) 0xEF, (byte) 0xFA, (byte) 0xFD, |
| | | (byte) 0xF4, (byte) 0xF3 }; |
| | | |
| | | /** |
| | | * è®¡ç®æ°ç»çCRC8æ ¡éªå¼ |
| | | * |
| | | * @param data |
| | | * éè¦è®¡ç®çæ°ç» |
| | | * @return CRC8æ ¡éªå¼ |
| | | */ |
| | | public static byte calcCrc8(byte[] data) { |
| | | return calcCrc8(data, (short) 2, (short) (data.length - 2), (byte) 0); |
| | | } |
| | | |
| | | /** |
| | | * 计ç®CRC8æ ¡éªå¼ |
| | | * |
| | | * @param data |
| | | * æ°æ® |
| | | * @param offset |
| | | * èµ·å§ä½ç½® |
| | | * @param len |
| | | * é¿åº¦ |
| | | * @return æ ¡éªå¼ |
| | | */ |
| | | public static byte calcCrc8(byte[] data, short offset, short len) { |
| | | return calcCrc8(data, offset, len, (byte) 0); |
| | | } |
| | | |
| | | public static byte calcCrc8(byte[] ucPtr, short offset, short ucLen, byte preval) { |
| | | short ucIndex; // CRC8æ ¡éªè¡¨æ ¼ç´¢å¼ |
| | | short ucCRC8 = 0; // CRC8åèåå§å |
| | | short i = 0; |
| | | // è¿è¡CRC8使 ¡éª |
| | | while ((ucLen--) > 0) { |
| | | short indexI = (short) (i + offset); |
| | | short data = ucPtr[indexI]; |
| | | if (data < 0) { |
| | | int sd = Byte.toUnsignedInt(ucPtr[indexI]); |
| | | data = (short) sd; |
| | | } |
| | | ucIndex = (short) (ucCRC8 ^ data); |
| | | ucCRC8 = crc8_tab[(short) ucIndex]; |
| | | i++; |
| | | } |
| | | // è¿åCRC8æ ¡éªæ°æ® |
| | | return (byte) (~ucCRC8); |
| | | } |
| | | |
| | | public static short crc16Byte(short crc, byte data) { |
| | | return (short) ((crc << 8) ^ gCrc16Table[((crc >> 8) ^ data) & 0xff]); |
| | | } |
| | | |
| | | public static short calculateCrc16(byte[] buf, short offset, short length) { |
| | | short crc = 0; |
| | | short i = 0; |
| | | while ((length--) > 0) { |
| | | crc = crc16Byte(crc, buf[offset + (i++)]); |
| | | } |
| | | return crc; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ld.igds.protocol.bhzn.verb.analysis; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.ld.igds.common.CoreCommonService; |
| | | import com.ld.igds.common.CoreDeviceService; |
| | | import com.ld.igds.constant.BizType; |
| | | import com.ld.igds.es.dto.EsData; |
| | | import com.ld.igds.es.service.CoreEsService; |
| | | import com.ld.igds.gas.CoreGasService; |
| | | import com.ld.igds.io.constant.OrderRespEnum; |
| | | import com.ld.igds.io.notify.NotifyWebInvoker; |
| | | import com.ld.igds.models.DepotConf; |
| | | import com.ld.igds.models.DeviceSer; |
| | | import com.ld.igds.models.Gas; |
| | | import com.ld.igds.models.GasInfo; |
| | | import com.ld.igds.protocol.bhzn.verb.command.BaseRemoteImpl; |
| | | import com.ld.igds.protocol.bhzn.verb.dto.IoMessage; |
| | | import com.ld.igds.protocol.bhzn.verb.dto.Res208; |
| | | import com.ld.igds.protocol.bhzn.verb.dto.Res209; |
| | | import com.ld.igds.util.ContextUtil; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.text.DecimalFormat; |
| | | import java.text.NumberFormat; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * è½èè§£æ |
| | | */ |
| | | @Slf4j |
| | | @Component(AnalysisES.BEAN_ID) |
| | | public class AnalysisES extends BaseRemoteImpl { |
| | | |
| | | public static final String BEAN_ID = "bhzn.analysisEs"; |
| | | |
| | | @Autowired |
| | | private CoreDeviceService deviceService; |
| | | @Autowired |
| | | private NotifyWebInvoker notifyInvoker; |
| | | @Autowired |
| | | private CoreCommonService commonService; |
| | | @Autowired |
| | | private CoreEsService esService; |
| | | |
| | | /** |
| | | * 设å¤ç¶æ |
| | | * |
| | | * @param reMessage |
| | | * @param ser |
| | | */ |
| | | public void analysis(IoMessage reMessage, DeviceSer ser) { |
| | | |
| | | log.info("è½èå¼å§è§£æ"); |
| | | Res208 res208 = JSONObject.parseObject(reMessage.getContentStr(), Res208.class); |
| | | if(2==res208.getState()){ |
| | | |
| | | }else{ |
| | | log.info("æ°ä½æ²¡æéé宿ï¼åæ¶è§£æï¼"); |
| | | return; |
| | | } |
| | | DepotConf depotConf = commonService.getCacheDepotConfBySerId(ser.getCompanyId(),ser.getId() ); |
| | | |
| | | EsData esData = new EsData(); |
| | | esData.setCompanyId(ser.getCompanyId()); |
| | | esData.setDepotId(depotConf.getDepotId()); |
| | | esData.setDeviceId("0"); |
| | | esData.setDeviceName(""); |
| | | esData.setUpdateTime(new Date()); |
| | | esData.setUa(Double.valueOf(res208.getVA())); |
| | | esData.setUb(Double.valueOf(res208.getVB())); |
| | | esData.setUc(Double.valueOf(res208.getVC())); |
| | | |
| | | esData.setIa(Double.valueOf(res208.getIA())); |
| | | esData.setIb(Double.valueOf(res208.getIB())); |
| | | esData.setIc(Double.valueOf(res208.getIC())); |
| | | esData.setF(Double.valueOf(res208.getZPL())); |
| | | |
| | | esData.setEp(0.0); |
| | | esData.setEq(0.0); |
| | | esData.setEs(0.0); |
| | | esData.setEsFan(0.0); |
| | | esData.setEsAir(0.0); |
| | | esData.setEsLamp(0.0); |
| | | esData.setEs(res208.getZNH()); |
| | | log.info("æ§å¶æ----->>>å¹³å°ï¼è½èæ°æ®è§£æå®æ-ä»åº={}", esData.getDepotId()); |
| | | |
| | | esService.saveAndUpdateInc(esData); |
| | | } |
| | | |
| | | } |
| | |
| | | private AnalysisDevice analysisDevice; |
| | | @Autowired |
| | | private AnalysisGas analysisGas; |
| | | |
| | | @Autowired |
| | | private AnalysisES analysisEs; |
| | | |
| | | /** |
| | | * @param message |
| | |
| | | case 209: |
| | | analysisGas.analysis(message, ser); |
| | | break; |
| | | case 240: |
| | | analysisEs.analysis(message, ser); |
| | | break; |
| | | default: |
| | | log.error("æ§å¶æå½ä»¤ç è§£æå¤±è´¥ï¼"); |
| | | break; |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ld.igds.protocol.bhzn.verb.builder; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.ld.igds.constant.DeviceStatus; |
| | | import com.ld.igds.io.request.BaseRequest; |
| | | import com.ld.igds.io.request.ExeDevice; |
| | | import com.ld.igds.io.request.TempControlRequest; |
| | | import com.ld.igds.protocol.bhzn.verb.dto.IoMessage; |
| | | import com.ld.igds.protocol.bhzn.verb.dto.Req248; |
| | | import com.ld.igds.protocol.bhzn.verb.server.BhznVerbServerUtils; |
| | | import com.ld.igds.util.BytesUtil; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | @Slf4j |
| | | public class EsCommandBuilder extends CommandBuilder { |
| | | |
| | | private final static EsCommandBuilder instance = new EsCommandBuilder(); |
| | | |
| | | private EsCommandBuilder() { |
| | | } |
| | | |
| | | public static EsCommandBuilder getInstance() { |
| | | return instance; |
| | | } |
| | | |
| | | public IoMessage buildMessage(BaseRequest request) { |
| | | |
| | | IoMessage message = new IoMessage(); |
| | | message.setIp(request.getIp()); |
| | | message.setPort(request.getPort()); |
| | | message.setBegin(BhznVerbServerUtils.MSG_START1); |
| | | // è°æ´ä¿¡æ¯ |
| | | buildInfo(message, request); |
| | | message.setSign("0000"); |
| | | message.setVersion("V1.0000000"); |
| | | message.setStNum(Integer.parseInt(request.getDepotId())); |
| | | message.setSn(request.getSerId()); |
| | | message.setCmd(240); |
| | | message.setResult(""); |
| | | message.setOrderId(request.getOrderId()); |
| | | message.setEnd(BhznVerbServerUtils.MSG_END); |
| | | // çææ´ä½å½ä»¤ä¿¡æ¯åBYteä¿¡æ¯ |
| | | build(message); |
| | | |
| | | return message; |
| | | } |
| | | |
| | | public void buildInfo(IoMessage message, BaseRequest request) { |
| | | message.setContentStr("{}"); |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ld.igds.protocol.bhzn.verb.command; |
| | | |
| | | import com.ld.igds.io.RemoteEsService; |
| | | import com.ld.igds.io.constant.OrderRespEnum; |
| | | import com.ld.igds.io.request.BaseRequest; |
| | | import com.ld.igds.io.response.BaseResponse; |
| | | import com.ld.igds.protocol.bhzn.verb.builder.EsCommandBuilder; |
| | | import com.ld.igds.protocol.bhzn.verb.dto.IoMessage; |
| | | import com.ld.io.api.InvokeResult; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | /** |
| | | * å½ååè®®å®ç°ï¼è½èåè®® |
| | | * |
| | | * @author jiazx |
| | | */ |
| | | @Slf4j |
| | | @Component(RemoteEsServiceImpl.BEAN_ID) |
| | | public class RemoteEsServiceImpl extends BaseRemoteImpl implements RemoteEsService { |
| | | |
| | | public static final String BEAN_ID = "bhzn.remoteEsServiceImpl"; |
| | | |
| | | @Override |
| | | public BaseResponse checkEs(BaseRequest request) { |
| | | |
| | | try { |
| | | // Step1 çæè½èå½ä»¤ |
| | | IoMessage message = EsCommandBuilder.getInstance().buildMessage(request); |
| | | InvokeResult result = send(request.getIp(), request.getPort(), message.getByteMsg()); |
| | | |
| | | log.error("å¹³å°------>>>>æ§å¶æï¼è½èæ¥è¯¢-{}", message.toString()); |
| | | |
| | | if (InvokeResult.SUCCESS == result) { |
| | | return new BaseResponse(OrderRespEnum.ORDER_SUCCESS.getCode(), |
| | | "è½èå½ä»¤åéæåï¼"); |
| | | } else { |
| | | log.error("å¹³å°------>>>>æ§å¶æï¼è½èæ¥è¯¢-失败{}", result.getMessage()); |
| | | return new BaseResponse(OrderRespEnum.ORDER_ERROR.getCode(), |
| | | "å½ä»¤åéå¼å¸¸ï¼" + result.getMessage()); |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | log.error("è½èæ£æµå¼å¸¸ï¼{}", e); |
| | | return new BaseResponse(OrderRespEnum.ORDER_ERROR.getCode(), "å端å¼å¸¸ï¼" + e.getMessage()); |
| | | } |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ld.igds.protocol.bhzn.verb.dto; |
| | | |
| | | import lombok.Data; |
| | | |
| | | /** |
| | | * æ°ä½æ£æµè¿å |
| | | */ |
| | | @Data |
| | | public class Res208 { |
| | | |
| | | private Integer State ;// ç¶æ |
| | | private String Error ;// æ
éä¿¡æ¯ |
| | | private float VA ;//Aç¸çµå |
| | | private float VB ;//Bç¸çµå |
| | | private float VC ;//Cç¸çµå |
| | | private float ZI ;//æ»çµæµ |
| | | private float IA ;//Aç¸çµæµ |
| | | private float IB ;//Bç¸çµæµ |
| | | private float IC ;//Cç¸çµæµ |
| | | private float ZYG ;//æ»æååç |
| | | private float YGA ;//Aç¸æååç |
| | | private float YGB ;//Bç¸æååç |
| | | private float YGC ;//Cç¸æååç |
| | | private float ZWG ;//æ»æ ååç |
| | | private float WGA ;//Aç¸æ ååç |
| | | private float WGB ;//Bç¸æ ååç |
| | | private float WGC ;//Cç¸æ ååç |
| | | private float ZSG ;//æ»è§å¨åç |
| | | private float SGA ;//Aç¸è§å¨åç |
| | | private float SGB ;//Bç¸è§å¨åç |
| | | private float SGC ;//Cç¸è§å¨åç |
| | | private float ZGY ;//æ»åçå æ° |
| | | private float GYA ;//Aç¸åçå æ° |
| | | private float GYB ;//Bç¸åçå æ° |
| | | private float GYC ;//Cç¸åçå æ° |
| | | private float XJA ;//Aç¸è§åº¦ |
| | | private float XJB ;//Bç¸è§åº¦ |
| | | private float XJC ;//Cç¸è§åº¦ |
| | | private float ZPL ;//é¢ç |
| | | private float ZNH ;//æ»è½è |
| | | |
| | | } |
| | |
| | | deptId: deptId, |
| | | progress: recordData.progress, |
| | | bizType: bizType, |
| | | bizId: recordData.id, |
| | | plateNum: recordData.plateNum, |
| | | id1: snapDto1.id, |
| | | id2: snapDto2.id, |