parent
1dfb085d19
commit
5e0561f866
@ -0,0 +1,111 @@
|
|||||||
|
package UnitTest;
|
||||||
|
|
||||||
|
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.MessageToByteEncoder;
|
||||||
|
|
||||||
|
public class HexDataSender {
|
||||||
|
private final String host;
|
||||||
|
private final int port;
|
||||||
|
|
||||||
|
public HexDataSender(String host, int port) {
|
||||||
|
this.host = host;
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendHexData(String hexData) throws Exception {
|
||||||
|
EventLoopGroup workerGroup = new NioEventLoopGroup();
|
||||||
|
|
||||||
|
try {
|
||||||
|
Bootstrap b = new Bootstrap();
|
||||||
|
b.group(workerGroup);
|
||||||
|
b.channel(NioSocketChannel.class);
|
||||||
|
b.option(ChannelOption.SO_KEEPALIVE, true);
|
||||||
|
b.handler(new ChannelInitializer<SocketChannel>() {
|
||||||
|
@Override
|
||||||
|
public void initChannel(SocketChannel ch) {
|
||||||
|
ChannelPipeline p = ch.pipeline();
|
||||||
|
p.addLast(new HexDataEncoder());
|
||||||
|
p.addLast(new HexDataSenderHandler(hexData));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Start the client.
|
||||||
|
ChannelFuture f = b.connect(host, port).sync();
|
||||||
|
|
||||||
|
// Wait until the connection is closed.
|
||||||
|
f.channel().closeFuture().sync();
|
||||||
|
} finally {
|
||||||
|
workerGroup.shutdownGracefully();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String IP = "10.10.21.18";
|
||||||
|
public static int PORT = 8001;
|
||||||
|
|
||||||
|
public static void Open(String sb_id) throws Exception {
|
||||||
|
//开
|
||||||
|
String hexData = "16 00 34 F5 41 11 FE 82 0D 02 ? 00 00 00 00 00 00 01 00 00 01".replace("?", sb_id).replace(" ", "");
|
||||||
|
HexDataSender sender = new HexDataSender(IP, PORT);
|
||||||
|
sender.sendHexData(hexData);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Close(String sb_id) throws Exception {
|
||||||
|
//关
|
||||||
|
String hexData = "16 00 34 F5 41 11 FE 82 0D 02 ? 00 00 00 00 00 00 01 00 00 00".replace("?", sb_id).replace(" ", "");
|
||||||
|
HexDataSender sender = new HexDataSender(IP, PORT);
|
||||||
|
sender.sendHexData(hexData);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
Close("6D FF");
|
||||||
|
//Thread.sleep(3000);
|
||||||
|
//Open("6D FF");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class HexDataSenderHandler extends ChannelInboundHandlerAdapter {
|
||||||
|
private final String hexData;
|
||||||
|
|
||||||
|
public HexDataSenderHandler(String hexData) {
|
||||||
|
this.hexData = hexData;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void channelActive(ChannelHandlerContext ctx) {
|
||||||
|
byte[] bytes = hexStringToByteArray(hexData);
|
||||||
|
ByteBuf buffer = Unpooled.buffer(bytes.length);
|
||||||
|
buffer.writeBytes(bytes);
|
||||||
|
ctx.writeAndFlush(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
||||||
|
cause.printStackTrace();
|
||||||
|
ctx.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] hexStringToByteArray(String hexData) {
|
||||||
|
int len = hexData.length();
|
||||||
|
byte[] data = new byte[len / 2];
|
||||||
|
for (int i = 0; i < len; i += 2) {
|
||||||
|
data[i / 2] = (byte) ((Character.digit(hexData.charAt(i), 16) << 4) +
|
||||||
|
Character.digit(hexData.charAt(i + 1), 16));
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class HexDataEncoder extends MessageToByteEncoder<ByteBuf> {
|
||||||
|
@Override
|
||||||
|
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
|
||||||
|
byte[] bytes = new byte[msg.readableBytes()];
|
||||||
|
msg.getBytes(msg.readerIndex(), bytes);
|
||||||
|
out.writeBytes(bytes);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue