1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.example.rxtx;
17
18 import io.netty.bootstrap.Bootstrap;
19 import io.netty.channel.ChannelFuture;
20 import io.netty.channel.ChannelInitializer;
21 import io.netty.channel.EventLoopGroup;
22 import io.netty.channel.oio.OioEventLoopGroup;
23 import io.netty.channel.rxtx.RxtxChannel;
24 import io.netty.channel.rxtx.RxtxDeviceAddress;
25 import io.netty.handler.codec.LineBasedFrameDecoder;
26 import io.netty.handler.codec.string.StringDecoder;
27 import io.netty.handler.codec.string.StringEncoder;
28
29
30
31
32 public final class RxtxClient {
33
34 static final String PORT = System.getProperty("port", "/dev/ttyUSB0");
35
36 public static void main(String[] args) throws Exception {
37 EventLoopGroup group = new OioEventLoopGroup();
38 try {
39 Bootstrap b = new Bootstrap();
40 b.group(group)
41 .channel(RxtxChannel.class)
42 .handler(new ChannelInitializer<RxtxChannel>() {
43 @Override
44 public void initChannel(RxtxChannel ch) throws Exception {
45 ch.pipeline().addLast(
46 new LineBasedFrameDecoder(32768),
47 new StringEncoder(),
48 new StringDecoder(),
49 new RxtxClientHandler()
50 );
51 }
52 });
53
54 ChannelFuture f = b.connect(new RxtxDeviceAddress(PORT)).sync();
55
56 f.channel().closeFuture().sync();
57 } finally {
58 group.shutdownGracefully();
59 }
60 }
61 }