下面列出了怎么用io.netty.channel.rxtx.RxtxDeviceAddress的API类实例代码及写法,或者点击链接到github查看源代码。
public static void main(String[] args) throws Exception {
EventLoopGroup group = new OioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(RxtxChannel.class)
.handler(new ChannelInitializer<RxtxChannel>() {
@Override
public void initChannel(RxtxChannel ch) throws Exception {
ch.pipeline().addLast(
new LineBasedFrameDecoder(32768),
new StringEncoder(),
new StringDecoder(),
new RxtxClientHandler()
);
}
});
ChannelFuture f = b.connect(new RxtxDeviceAddress(PORT)).sync();
f.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
EventLoopGroup group = new OioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(RxtxChannel.class)
.handler(new ChannelInitializer<RxtxChannel>() {
@Override
public void initChannel(RxtxChannel ch) throws Exception {
ch.pipeline().addLast(
new LineBasedFrameDecoder(32768),
new StringEncoder(),
new StringDecoder(),
new RxtxClientHandler()
);
}
});
ChannelFuture f = b.connect(new RxtxDeviceAddress(PORT)).sync();
f.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
}
}
public void connect(@Nonnull String host, int port) {
requireNonNull(host, "host");
checkState(isInitialized(), "Not initialized");
if (isConnected()) {
LOG.debug("Already connected, doing nothing.");
return;
}
try {
bootstrap.option(RxtxChannelOption.BAUD_RATE, port);
channelFuture = bootstrap.connect(new RxtxDeviceAddress(host)).sync();
channelFuture.addListener((ChannelFuture future) -> {
if (future.isSuccess()) {
this.initialized = true;
LOG.info("串口连接并监听成功,名称[{}],波特率[{}]", host, port);
connectionEventListener.onConnect();
} else {
connectionEventListener.onFailedConnectionAttempt();
LOG.info("打开串口时失败,名称[" + host + "], 波特率[" + port + "], 串口可能已被占用!");
}
});
connectFuture = null;
} catch (Exception e) {
e.printStackTrace();
workerGroup.shutdownGracefully();
throw new RuntimeException("RxtxClientChannelManager initialized is " + isInitialized() + ", exception message: " + e.getMessage());
}
}