Browse Source

电子车牌协议对接

qzyReal 1 year ago
parent
commit
e4d1efe3dc

+ 5 - 0
config.xml

@@ -15,6 +15,11 @@
                 <port>4003</port>
                 <brand>2000</brand>
             </led>
+            <electron>
+                <enable>true</enable>
+                <port>4002</port>
+                <brand>2000</brand>
+            </electron>
             <plc>
                 <enable>true</enable>
                 <port>4001</port>

+ 12 - 0
src/main/java/com/gzlh/api/OpenApi.java

@@ -1,10 +1,13 @@
 package com.gzlh.api;
 
+import cn.hutool.core.thread.ThreadUtil;
 import com.gzlh.bus.EventConfig;
 import com.gzlh.config.ModuleEnum;
 import com.gzlh.bus.EventBus;
 import com.gzlh.config.SystemObject;
 import com.gzlh.config.dto.SerialSetting;
+import com.gzlh.device.electron.client.ElectronNettyConfig;
+import com.gzlh.device.electron.job.ElectronReadJob;
 import com.gzlh.device.led.event.LedDefaultEvent;
 import com.gzlh.device.led.utils.LedOptions;
 import com.gzlh.utils.ResultJson;
@@ -23,6 +26,7 @@ public class OpenApi {
     @Resource
     EventBus eventBus;
 
+
     @RequestMapping("test")
     public ResultJson test(String msg,String line,String showType,String color){
         eventBus.startEvent(ModuleEnum.LED_MODULE.getModuleEn()+"."+ LedDefaultEvent.READ);
@@ -32,6 +36,14 @@ public class OpenApi {
         LedOptions ledOptions=new LedOptions();
         ledOptions.setLine(line).setShowType(showType).setColor(color);
         SystemObject.ledFactory.handler(ledDTO.getBrand()).sendMsg(msg,ledOptions);
+
         return ResultJson.success();
     }
+    @RequestMapping("electron")
+    public ResultJson test(String msg){
+        ElectronReadJob job=   new ElectronReadJob("4002FEC0");
+        ThreadUtil.execute(job);
+        return ResultJson.success();
+    }
+
 }

+ 7 - 3
src/main/java/com/gzlh/config/SystemObject.java

@@ -2,6 +2,7 @@ package com.gzlh.config;
 
 import com.gzlh.device.capture.factory.CaptureFactory;
 import com.gzlh.device.capture.properties.CapturePropertiesConfig;
+import com.gzlh.device.electron.factory.ElectronFactory;
 import com.gzlh.device.infrared.config.RedPropertiesConfig;
 import com.gzlh.device.infrared.factory.RedFactory;
 import com.gzlh.device.led.factory.LedFactory;
@@ -23,11 +24,14 @@ public class SystemObject {
     public static WeighbridgePropertiesConfig weighbridgePropertiesConfig;
 
     public static LedFactory ledFactory;
-
-    public static CaptureFactory captureFactory;
-
     public static CapturePropertiesConfig capturePropertiesConfig;
 
+    public static CaptureFactory captureFactory;
+    public static ElectronFactory electronFactory;
+    @Autowired
+      void setElectronFactory(ElectronFactory electronFactory) {
+        SystemObject.electronFactory = electronFactory;
+    }
 
     @Autowired
     void setApplicationConfig(ApplicationConfig applicationConfig) {

+ 10 - 0
src/main/java/com/gzlh/config/dto/SerialSetting.java

@@ -18,6 +18,16 @@ public class SerialSetting implements Serializable {
     @JsonProperty("plc")
     private PlcDTO plc;
 
+    @JsonProperty("electron")
+    private ElectronSetting electron;
+
+    @Data
+    public static class ElectronSetting{
+        private Boolean enable;
+        private int port;
+        private Integer brand;
+    }
+
     @Data
     @NoArgsConstructor
     public static class Weighbridge{

+ 8 - 0
src/main/java/com/gzlh/device/electron/action/IElectronAction.java

@@ -0,0 +1,8 @@
+package com.gzlh.device.electron.action;
+
+public interface IElectronAction {
+    /**
+     * 开始读卡
+     */
+    String START_READ_CARD="START_READ_CARD";
+}

+ 16 - 0
src/main/java/com/gzlh/device/electron/brand/ElectronBrandType.java

@@ -0,0 +1,16 @@
+package com.gzlh.device.electron.brand;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+@Getter
+@AllArgsConstructor
+public enum ElectronBrandType {
+
+    RUI_BRAND(2000,"瑞德电子车牌")
+    ;
+
+
+    private int code;
+    private String brand;
+}

+ 44 - 0
src/main/java/com/gzlh/device/electron/client/ElectronClientHandler.java

@@ -0,0 +1,44 @@
+package com.gzlh.device.electron.client;
+
+import cn.hutool.core.util.StrUtil;
+import com.gzlh.bus.EventConfig;
+import com.gzlh.config.SystemObject;
+import io.netty.channel.ChannelHandler;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.SimpleChannelInboundHandler;
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+@ChannelHandler.Sharable
+public class ElectronClientHandler extends SimpleChannelInboundHandler<String> {
+
+    private ElectronNettyConfig electronNettyConfig;
+
+    public ElectronClientHandler(ElectronNettyConfig electronNettyConfig) {
+        this.electronNettyConfig = electronNettyConfig;
+    }
+
+    @Override
+    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
+        if (StrUtil.isEmpty(msg)){
+            return;
+        }
+        log.info("电子车牌 客户端收到消息:" + msg);
+        SystemObject.electronFactory.handler(EventConfig.serialSetting.getElectron().getBrand())
+                .handlerMsg(msg.toUpperCase());
+    }
+
+    @Override
+    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
+        log.error("电子车牌 客户端 连接断开,进行重连");
+        electronNettyConfig.connect();
+    }
+
+    @Override
+    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
+        cause.printStackTrace();
+        ctx.close();
+    }
+
+}
+

+ 105 - 0
src/main/java/com/gzlh/device/electron/client/ElectronNettyConfig.java

@@ -0,0 +1,105 @@
+package com.gzlh.device.electron.client;
+
+import com.gzlh.bus.EventConfig;
+import com.gzlh.config.dto.SerialSetting;
+import com.gzlh.config.netty.NettyDecoder;
+import com.gzlh.utils.XorUtils;
+import io.netty.bootstrap.Bootstrap;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.*;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.SocketChannel;
+import io.netty.channel.socket.nio.NioSocketChannel;
+import io.netty.handler.codec.string.StringEncoder;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.nio.charset.StandardCharsets;
+import java.util.concurrent.TimeUnit;
+
+@Configuration
+@Slf4j
+public class ElectronNettyConfig {
+
+
+
+    @Autowired
+    private ElectronClientHandler electronClientHandler;
+
+    private Channel channel;
+
+    @Bean("electronBootstrap")
+    public Bootstrap bootstrap() {
+        SerialSetting serialSetting=EventConfig.serialSetting;
+        String host = serialSetting.getHost();
+        int port = serialSetting.getElectron().getPort();
+        EventLoopGroup group = new NioEventLoopGroup();
+        return new Bootstrap()
+                .group(group)
+                .channel(NioSocketChannel.class)
+                .remoteAddress(host, port)
+                .option(ChannelOption.SO_KEEPALIVE, true)
+                .handler(new ChannelInitializer<SocketChannel>() {
+                    @Override
+                    protected void initChannel(SocketChannel ch) {
+                        try {
+                            ChannelPipeline pipeline = ch.pipeline();
+                            pipeline.addLast( new NettyDecoder());
+                            pipeline.addLast( new StringEncoder(StandardCharsets.UTF_8));
+                            pipeline.addLast(electronClientHandler);
+                        } catch (Exception e) {
+                            log.info("error connect:{}", e.getMessage());
+                        }
+                    }
+                });
+    }
+
+    @Bean("electronClientHandler")
+    public ElectronClientHandler electronClientHandler() {
+        return new ElectronClientHandler(this);
+    }
+
+    public void connect() {
+        SerialSetting serialSetting=EventConfig.serialSetting;
+        String host = serialSetting.getHost();
+        int port = serialSetting.getElectron().getPort();
+        ChannelFuture future = bootstrap().connect();
+        future.addListener((ChannelFutureListener) future1 -> {
+            if (future1.isSuccess()) {
+                channel = future1.channel();
+                log.info("电子车牌 串口服务器连接成功,{},{}", host, port);
+            } else {
+                log.error("-------------电子车牌 连接服务器失败,{},{}-----------,进行重连", host, port);
+                future1.channel().eventLoop().schedule(this::connect, 5, TimeUnit.SECONDS);
+            }
+        });
+        try {
+            future.channel().closeFuture().sync();
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+    }
+
+    public void send(String message) {
+        log.info("电子车牌发送命令:{}",message);
+        if (channel != null && channel.isActive()) {
+            ByteBuf bufff = Unpooled.buffer();
+            bufff.writeBytes(XorUtils.hexString2Bytes(message));
+            channel.writeAndFlush(bufff);
+        } else {
+            log.error("未建立连接,无法发送消息");
+        }
+    }
+
+    public void close() {
+        if (channel != null) {
+            channel.close();
+        }
+    }
+
+
+}
+

+ 22 - 0
src/main/java/com/gzlh/device/electron/factory/ElectronFactory.java

@@ -0,0 +1,22 @@
+package com.gzlh.device.electron.factory;
+
+
+
+import com.gzlh.device.capture.handler.ICaptureHandler;
+import com.gzlh.device.electron.handler.IElectronHandler;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+@Service
+public class ElectronFactory {
+    @Autowired
+    private List<IElectronHandler>handlers;
+
+    public IElectronHandler handler(int code){
+       return handlers.stream().filter(h->h.brandType().getCode()==code)
+                .findAny().orElseThrow(()->new IllegalArgumentException("不支持电子车牌"));
+    }
+
+}

+ 29 - 0
src/main/java/com/gzlh/device/electron/handler/IElectronHandler.java

@@ -0,0 +1,29 @@
+package com.gzlh.device.electron.handler;
+
+import com.gzlh.device.capture.brand.CaptureBrandType;
+import com.gzlh.device.electron.brand.ElectronBrandType;
+
+public interface IElectronHandler {
+
+    ElectronBrandType brandType();
+
+
+    /**
+     * 处理命令
+     * @param action
+     */
+    void handlerAction(String action);
+
+    /**
+     * 发送信息
+     * @param msg
+     */
+    void sendMsg(String msg);
+
+    /**
+     * 处理信息
+     * @param msg
+     */
+    void handlerMsg(String msg);
+
+}

+ 60 - 0
src/main/java/com/gzlh/device/electron/handler/impl/ElectronHandlerRuiDe.java

@@ -0,0 +1,60 @@
+package com.gzlh.device.electron.handler.impl;
+
+import cn.hutool.core.util.StrUtil;
+import com.gzlh.device.electron.brand.ElectronBrandType;
+import com.gzlh.device.electron.client.ElectronNettyConfig;
+import com.gzlh.device.electron.handler.IElectronHandler;
+import com.gzlh.device.electron.utils.CheckNumUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+
+/**
+ * 瑞德电子车牌处理类
+ */
+@Component
+@Slf4j
+public class ElectronHandlerRuiDe implements IElectronHandler {
+    private final String CALL_BACK_HEADER="F0";
+
+    @Resource
+    private ElectronNettyConfig electronNettyConfig;
+    @Override
+    public ElectronBrandType brandType() {
+        return ElectronBrandType.RUI_BRAND;
+    }
+
+    @Override
+    public void handlerAction(String action) {
+
+    }
+
+    @Override
+    public void sendMsg(String msg) {
+        electronNettyConfig.send(msg);
+    }
+
+    @Override
+    public void handlerMsg(String msg) {
+        int len=msg.length();
+        //引导码
+        String bootCode=StrUtil.sub(msg,0,2);
+        if (StrUtil.equals(bootCode,CALL_BACK_HEADER)){
+            //长度
+            String lenHex=StrUtil.sub(msg,2,4);
+            //命令Command
+            String command=StrUtil.sub(msg,4,6);
+            //return data,返回的数据
+            String data= StrUtil.sub(msg,0,len-2);
+            //校验码
+           String checkNum= StrUtil.sub(msg,len-2,len);
+           //计算出来的校验码
+           String num=  CheckNumUtils.calculateChecksum(data);
+           if (!StrUtil.equals(checkNum,num)){
+               return;
+           }
+        }
+    }
+
+}

+ 23 - 0
src/main/java/com/gzlh/device/electron/job/ElectronReadJob.java

@@ -0,0 +1,23 @@
+package com.gzlh.device.electron.job;
+
+import cn.hutool.extra.spring.SpringUtil;
+import com.gzlh.bus.EventConfig;
+import com.gzlh.config.SystemObject;
+import com.gzlh.device.electron.client.ElectronNettyConfig;
+
+/**
+ *下发指令读取卡
+ */
+public class ElectronReadJob implements Runnable{
+    private String command;
+
+    public ElectronReadJob(String command) {
+        this.command = command;
+    }
+
+    @Override
+    public void run() {
+        SystemObject.electronFactory.handler(EventConfig.serialSetting.getElectron().getBrand())
+                .sendMsg(command);
+    }
+}

+ 29 - 0
src/main/java/com/gzlh/device/electron/utils/CheckNumUtils.java

@@ -0,0 +1,29 @@
+package com.gzlh.device.electron.utils;
+
+import cn.hutool.core.util.HexUtil;
+
+public class CheckNumUtils {
+    public static String calculateChecksum(byte[] uBuff, int uBuffLen) {
+        int uSum = 0;
+        for (int i = 0; i < uBuffLen; i++) {
+            uSum = uSum + (uBuff[i] & 0xFF);
+        }
+        uSum = (~uSum) + 1;
+        return HexUtil.toHex(uSum & 0xFF);
+    }
+    public static byte[] hexStringToByteArray(String s) {
+        int len = s.length();
+        byte[] data = new byte[len / 2];
+        for (int i = 0; i < len; i += 2) {
+            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+                    + Character.digit(s.charAt(i + 1), 16));
+        }
+        return data;
+    }
+
+    public static String calculateChecksum(String s){
+        byte[]array=   hexStringToByteArray(s);
+        return   calculateChecksum(array,array.length);
+    }
+
+}

+ 1 - 1
src/main/java/com/gzlh/device/led/handler/impl/LedHandlerFengLiYuan.java

@@ -21,7 +21,7 @@ public class LedHandlerFengLiYuan implements ILedHandler {
 
     @Override
     public LedBrandType brandType() {
-        return LedBrandType.FEND_LI_YUAN;
+        return LedBrandType.FENGLIYUAN;
     }
 
     @Override

+ 1 - 4
src/main/java/com/gzlh/device/led/utils/CRC16.java

@@ -37,10 +37,7 @@ public class CRC16 {
         return sb.toString();
     }
 
-    public static void main(String[] args) {
-        System.out.println(HexUtil.toHex(51));
-        System.out.println(Integer.toHexString(51));
-    }
+
 
 
 }

+ 1 - 1
src/main/java/com/gzlh/device/plc/job/SearchJob.java

@@ -17,7 +17,7 @@ public class SearchJob {
     private PlcNettyConfig plcNettyConfig;
     @Scheduled(fixedDelay = 2000)
     public void searchJob(){
-        plcNettyConfig.send("0102000000187800");
+       // plcNettyConfig.send("0102000000187800");
     }
 
 }

+ 330 - 0
src/main/java/com/gzlh/startup/ReaderJavaWDemo.java

@@ -0,0 +1,330 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package com.gzlh.startup;
+
+
+ import java.net.DatagramSocket; 
+import java.net.InetAddress;
+ import java.net.InetSocketAddress; 
+ import java.net.SocketAddress; 
+ import java.nio.ByteBuffer; 
+ import java.nio.CharBuffer; 
+ import java.nio.channels.DatagramChannel; 
+ import java.nio.channels.SelectionKey; 
+ import java.nio.channels.Selector; 
+ import java.nio.charset.Charset; 
+ import java.util.Iterator; 
+ import java.util.Set; 
+
+
+/**
+  * @author 徐辛波(sinpo.xu@hotmail.com) Oct 19, 2008
+  */
+ public class ReaderJavaWDemo extends Thread {
+ 	public void run() {
+ 		//TestOri();//测试原始的
+            
+            TestOrm();//
+            
+ 	}
+      
+        public static void Bcd2AscEx(byte asc[], byte bcd[], int len)
+        {
+            int	i, j;
+            int k;
+
+            j = (len + len%2) / 2;
+            k = 3*j;
+            for(i=0; i<j; i++)
+            {
+                    asc[3*i]	= (byte)((bcd[i] >> 4) & 0x0f);
+                    asc[3*i+1]	= (byte)(bcd[i] & 0x0f);
+                    asc[3*i+2]	= 0x20;
+            }
+            for(i=0; i<k; i++)
+            {
+                    if ( (i+1) % 3 == 0 )
+                    {
+                            continue;
+                    }
+                    if( asc[i] > 0x09)
+                    {
+                            asc[i]	= (byte)(0x41 + asc[i] - 0x0a);
+                    }
+                    else	
+                    {
+                            asc[i]	+= 0x30;
+                    }
+            }
+
+            asc[k] = 0;
+            
+        }
+        
+        public static void Bcd2AscEy(char asc[], char bcd[], int len)
+        {
+            int	i, j;
+            int k;
+
+            j = (len + len%2) / 2;
+            k = 3*j;
+            for(i=0; i<j; i++)
+            {
+                    asc[3*i]	= (char)((bcd[i] >> 4) & 0x0f);
+                    asc[3*i+1]	= (char)(bcd[i] & 0x0f);
+                    asc[3*i+2]	= 0x20;
+            }
+            for(i=0; i<k; i++)
+            {
+                    if ( (i+1) % 3 == 0 )
+                    {
+                            continue;
+                    }
+                    if( asc[i] > 0x09)
+                    {
+                            asc[i]	= (char)(0x41 + asc[i] - 0x0a);
+                    }
+                    else	
+                    {
+                            asc[i]	+= 0x30;
+                    }
+            }
+
+            asc[k] = 0;
+            
+        }
+        
+        public static void TestOri(){
+            
+           Selector selector = null;
+ 		try {
+ 			DatagramChannel channel = DatagramChannel.open();
+ 			DatagramSocket socket = channel.socket();
+ 			channel.configureBlocking(false);
+ 			socket.bind(new InetSocketAddress(5057));
+ 
+ 			selector = Selector.open();
+ 			channel.register(selector, SelectionKey.OP_READ);
+ 		} catch (Exception e) {
+ 			e.printStackTrace();
+ 		}
+ 
+ 		ByteBuffer byteBuffer = ByteBuffer.allocate(65536);
+                
+ 		//while (true) {
+                if (true) {
+ 			try {
+                            
+                                byte chTemp[] = new byte[1024];
+ 				int eventsCount = selector.select(3000);
+ 				if (eventsCount > 0) {
+ 					Set selectedKeys = selector.selectedKeys();
+ 					Iterator iterator = selectedKeys.iterator();
+ 					while (iterator.hasNext()) {
+ 						SelectionKey sk = (SelectionKey) iterator.next();
+ 						iterator.remove();
+ 						if (sk.isReadable()) {
+ 							DatagramChannel datagramChannel = (DatagramChannel) sk
+ 									.channel();
+ 							SocketAddress sa = datagramChannel
+ 									.receive(byteBuffer);
+ 							byteBuffer.flip();
+ 
+ 							// 测试:通过将收到的ByteBuffer首先通过缺省的编码解码成CharBuffer 再输出马储油平台
+ 							//CharBuffer charBuffer = Charset.defaultCharset().decode(byteBuffer);
+                                                        //ByteBuffer btBuffer = Charset.defaultCharset().decode(byteBuffer);
+                                                        
+                                                        Bcd2AscEx(chTemp, byteBuffer.array(), 8);
+ 							//System.out.println("receive message:"+ charBuffer.toString());
+                                                        System.out.println("receive message:"+ String.valueOf(chTemp));
+                                                        
+ 							byteBuffer.clear();
+ 
+ 							//String echo = "This is the reply message from 服务器。";
+ 							//ByteBuffer buffer = Charset.defaultCharset()
+ 							//		.encode(echo);
+                                                        //chTemp = new char[2];
+                                                        //chTemp[0] = 0x56;
+                                                        //chTemp[1] = 0x78;
+                                                        
+                                                        //ByteBuffer buffer = ByteBuffer.allocate(chTemp.length);
+                                                        //Charset cs = Charset.forName ("UTF-8");
+                                                        //CharBuffer cb = CharBuffer.allocate (chTemp.length);
+                                                        //cb.put (chTemp);
+                                                        //cb.flip ();
+                                                        
+                                                        //ByteBuffer bb = cs.encode (cb);
+
+                                                        
+                                                        
+                                                        //datagramChannel.send(bb, sa);
+ 							//datagramChannel.write(buffer);
+ 						}
+ 					}
+ 				}
+ 			} catch (Exception e) {
+ 				e.printStackTrace();
+ 			}
+ 		}
+ 
+                
+                System.out.println("Program end!\n");
+                 
+        }
+        
+        
+        
+        //连接读写器测试
+        public static void TestOrm(){
+            
+           Selector selector = null;
+ 		try {
+ 			DatagramChannel channel = DatagramChannel.open();
+ 			DatagramSocket socket = channel.socket();
+ 			channel.configureBlocking(false);
+ 			socket.bind(new InetSocketAddress(5057));
+ 
+ 			selector = Selector.open();
+ 			channel.register(selector, SelectionKey.OP_READ);
+                        
+                        
+                        
+                        //byte[] chTemp = new byte[] {0x40, 0x03, 0x0F, 0x00, (byte)0xAE};
+                        //byte[] chTemp = new byte[] {0x40, 0x02, 0x02, (byte)0xBC};
+                        byte[] chTemp = new byte[] {0x40, 0x02, 0x06, (byte)0xB8};
+                        //chTemp[0] = 0x40;
+                        //chTemp[1] = 0x03;
+                        //chTemp[2] = 0x0F;
+                        //chTemp[3] = 0x00;
+                        //chTemp[4] = (byte)0xAE;
+
+                        
+            ByteBuffer bb = ByteBuffer.allocate (chTemp.length);
+            bb.put (chTemp);
+                        bb.flip ();
+            
+
+
+
+                        
+                        
+                        
+                        //DatagramChannel ch= DatagramChannel.open();
+ 			//DatagramSocket so = ch.socket();
+                        //ch.configureBlocking(false);
+                        //SocketAddress sa = so.getLocalSocketAddress();
+                        byte[] bs = new byte[] { (byte) 192, (byte) 168, 1, (byte) 254 };
+                        InetAddress address=InetAddress.getByAddress(bs);
+                        SocketAddress sa = new InetSocketAddress(address, 4002);
+                        
+                     int i=   channel.send(bb, sa);
+            System.out.println("i------------->"+i);
+                        
+                        
+ 		} catch (Exception e) {
+ 			e.printStackTrace();
+ 		}
+ 
+                
+                
+ 		ByteBuffer byteBuffer = ByteBuffer.allocate(65535);
+                
+ 		//while (true) {
+                if (true) {
+ 			try {
+                                int iRecvLen = 0;
+                                byte[] chTemp = new byte[4096];
+ 				int eventsCount = selector.select(5000);
+ 				if (eventsCount > 0) {
+ 					Set selectedKeys = selector.selectedKeys();
+ 					Iterator iterator = selectedKeys.iterator();
+ 					while (iterator.hasNext()) {
+ 						SelectionKey sk = (SelectionKey) iterator.next();
+ 						iterator.remove();
+ 						if (sk.isReadable()) {
+                                                    
+ 							DatagramChannel datagramChannel = (DatagramChannel) sk
+ 									.channel();
+                                                        byteBuffer.clear();
+ 							SocketAddress sa = datagramChannel.receive(byteBuffer);
+ 							byteBuffer.flip();
+ 
+                                                        
+                                                        iRecvLen = byteBuffer.limit();
+ 							// 测试:通过将收到的ByteBuffer首先通过缺省的编码解码成CharBuffer 再输出马储油平台
+ 							CharBuffer charBuffer = Charset.forName("UTF-8").decode(byteBuffer);
+                                                        Bcd2AscEx(chTemp, byteBuffer.array(), iRecvLen*2);
+ 							//System.out.println("receive message:"+ charBuffer.toString());
+                                                        String s = new String(chTemp, "UTF-8");
+                                                        System.out.println("receive message["+ iRecvLen +"]: "+ s.trim());
+                                                        
+ 							byteBuffer.clear();
+ 
+ 							//String echo = "This is the reply message from 服务器。";
+ 							//ByteBuffer buffer = Charset.defaultCharset()
+ 							//		.encode(echo);
+                                                        //chTemp = new char[2];
+                                                        //chTemp[0] = 0x56;
+                                                        //chTemp[1] = 0x78;
+                                                        
+                                                        //ByteBuffer buffer = ByteBuffer.allocate(chTemp.length);
+                                                        //Charset cs = Charset.forName ("UTF-8");
+                                                        //CharBuffer cb = CharBuffer.allocate (chTemp.length);
+                                                        //cb.put (chTemp);
+                                                        //cb.flip ();
+                                                        
+                                                        //ByteBuffer bb = cs.encode (cb);
+
+                                                        
+                                                        //要发送就下面打开
+                                                        //datagramChannel.send(bb, sa);
+ 							//datagramChannel.write(buffer);
+ 						}
+ 					}
+ 				}
+ 			} catch (Exception e) {
+ 				e.printStackTrace();
+ 			}
+ 		}
+ 
+                
+                System.out.println("TestOrm end!\n");
+                 
+        }
+        
+        
+        
+        // char转byte
+
+        private byte[] getBytes (char[] chars) {
+        Charset cs = Charset.forName ("UTF-8");
+        CharBuffer cb = CharBuffer.allocate (chars.length);
+        cb.put (chars);
+                        cb.flip ();
+        ByteBuffer bb = cs.encode (cb);
+
+        return bb.array();
+
+        }
+
+        // byte转char
+
+        private char[] getChars (byte[] bytes) {
+            Charset cs = Charset.forName ("UTF-8");
+            ByteBuffer bb = ByteBuffer.allocate (bytes.length);
+            bb.put (bytes);
+                        bb.flip ();
+            CharBuffer cb = cs.decode (bb);
+
+        return cb.array();
+}
+
+
+
+        public static void main(String[] args) {
+ 		new ReaderJavaWDemo().start();
+ 	}
+        
+ }

+ 6 - 0
src/main/java/com/gzlh/startup/StartupRunner.java

@@ -16,6 +16,7 @@ import com.gzlh.bus.EventBus;
 import com.gzlh.config.hksdk.HCNetSDK;
 import com.gzlh.config.hksdk.HkUtils;
 import com.gzlh.config.hksdk.bo.HKCacheManager;
+import com.gzlh.device.electron.client.ElectronNettyConfig;
 import com.gzlh.device.led.client.LedNettyConfig;
 import com.gzlh.device.infrared.client.RedNettyConfig;
 import com.gzlh.device.plc.client.PlcNettyConfig;
@@ -40,6 +41,8 @@ public class StartupRunner implements CommandLineRunner {
     private LedNettyConfig ledNettyConfig;
     @Resource
     private PlcNettyConfig plcNettyConfig;
+    @Resource
+    private ElectronNettyConfig electronNettyConfig;
 
     @Resource
     private BackgroundPropertiesConfig backgroundPropertiesConfig;
@@ -65,6 +68,9 @@ public class StartupRunner implements CommandLineRunner {
         if (serialSetting.getPlc().getEnable()){
             ThreadUtil.execute(() -> plcNettyConfig.connect());
         }
+        if (serialSetting.getElectron().getEnable()){
+            ThreadUtil.execute(() -> electronNettyConfig.connect());
+        }
         if (backgroundPropertiesConfig.isEnable()) {
             //ThreadUtil.execute(() -> backgroundClientNetty.connect());
         }