Springboot整合netty串口

SpringNettyApplication.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package me;

import io.netty.channel.ChannelFuture;
import me.server.RxServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class SpringNettyApplication implements CommandLineRunner {


@Autowired
private RxServer rxServer;

public static void main(String[] args) {
SpringApplication.run(SpringNettyApplication.class, args);
}

@Override
public void run(String... args) throws Exception {
ChannelFuture future = rxServer.run();
Runtime.getRuntime().addShutdownHook(new Thread(){
@Override
public void run() {
rxServer.destroy();
}
});
future.channel().closeFuture().syncUninterruptibly();
}
}

server/RxServer.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package me.server;


import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFactory;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.oio.OioEventLoopGroup;
import io.netty.channel.rxtx.RxtxChannel;
import io.netty.channel.rxtx.RxtxChannelConfig;
import io.netty.channel.rxtx.RxtxDeviceAddress;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import me.handler.RxtxClientHandler;
import org.springframework.stereotype.Component;


@Slf4j
@Component
public class RxServer {

static OioEventLoopGroup group = new OioEventLoopGroup();
private static RxtxChannel channel;

public ChannelFuture run(){
ChannelFuture future = null;
var bootstrap = new Bootstrap();
try {
bootstrap.group(group).channelFactory(new ChannelFactory<RxtxChannel>() {
public RxtxChannel newChannel() {
return channel;
}
})
.handler(new ChannelInitializer<RxtxChannel>() {
@Override
protected void initChannel(RxtxChannel ch) throws Exception {
ch.pipeline().addLast(
// new LineBasedFrameDecoder(32768),
// new StringEncoder(),
// new StringDecoder(),
new RxtxClientHandler()
);

}
});
channel = new RxtxChannel();
channel.config().setBaudrate(9600)
.setDatabits(RxtxChannelConfig.Databits.DATABITS_8)
.setParitybit(RxtxChannelConfig.Paritybit.NONE)
.setStopbits(RxtxChannelConfig.Stopbits.STOPBITS_1);
future = bootstrap.connect(new RxtxDeviceAddress("COM8")).sync();
future.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
return future;
}

public void destroy() {
log.info("Shutdown Netty rx...");
if(channel != null) { channel.close();}
group.shutdownGracefully();
log.info("Shutdown Netty rx Success!");
}
}

handler/RxtxClientHandler.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
* Copyright 2013 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package me.handler;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;


@Slf4j
public class RxtxClientHandler extends SimpleChannelInboundHandler<ByteBuf> {

@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf o) throws Exception {

byte[] req = new byte[o.readableBytes()];
// 从里面读取数据,这里面的数据是16进制的,0x..
o.readBytes(req);

log.info(req.toString());

// 将hex解码成String
// System.out.println(new String(req));
// // 十六进制转换字符串,没有解码
// String str = HexUtil.encodeHexStr(req);
// System.out.println(str);
}
}

GitHub地址:https://github.com/zzzzzzzs/SpringbootIntegration