main
黄海 2 years ago
parent 5e0561f866
commit a023089b9d

@ -289,7 +289,7 @@
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.32.Final</version>
<version>4.1.92.Final</version>
</dependency>
</dependencies>
<build>

@ -29,15 +29,15 @@ public class HexDataSender {
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline();
p.addLast(new HexDataEncoder());
p.addLast(new HexDataSenderHandler(hexData));
//ChannelPipeline p = ch.pipeline();
//p.addLast(new HexDataEncoder());
//p.addLast(new HexDataSenderHandler(hexData));
}
});
// Start the client.
ChannelFuture f = b.connect(host, port).sync();
f.channel().writeAndFlush(Unpooled.copiedBuffer(hexData.getBytes())).sync();
// Wait until the connection is closed.
f.channel().closeFuture().sync();
} finally {
@ -48,9 +48,9 @@ public class HexDataSender {
public static String IP = "10.10.21.18";
public static int PORT = 8001;
public static void Open(String sb_id) throws Exception {
public static void Open() 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(" ", "");
String hexData = "16 00 34 F5 41 11 FE 82 0D 02 6D FF 00 00 00 00 00 00 01 00 00 01".replace(" ","");
HexDataSender sender = new HexDataSender(IP, PORT);
sender.sendHexData(hexData);
}
@ -63,9 +63,9 @@ public class HexDataSender {
}
public static void main(String[] args) throws Exception {
Close("6D FF");
//Thread.sleep(3000);
//Open("6D FF");
//Close("6D FF");
Open();
}
}

@ -0,0 +1,70 @@
package UnitTest;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class HexStringSender {
public static String IP = "10.10.21.18";
public static int PORT = 8001;
//# 开
public static String open_hex_str = "16 00 34 F5 41 11 FE 82 0D 02 6D FF 00 00 00 00 00 00 01 00 00 01";
//关
public static String close_hex_str = "16 00 34 F5 41 11 FE 82 0D 02 6D FF 00 00 00 00 00 00 01 00 00 00";
public static void main(String[] args) throws IOException, InterruptedException {
for (int i = 1; ; i++) {
Socket socket = new Socket(IP, PORT);
String hex_str;
if (i % 2 == 0) hex_str = close_hex_str;
else hex_str = open_hex_str;
byte[] bytes = hexStringToByteArray(hex_str);
OutputStream os = socket.getOutputStream();
os.write(bytes);
socket.close();
Thread.sleep(2000);
}
}
/**
* 16
*
* @param hexString 16
* @return byte[]
*/
public static byte[] hexStringToByteArray(String hexString) {
hexString = hexString.replaceAll(" ", "");
int len = hexString.length();
byte[] bytes = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
// 两位一组,表示一个字节,把这样表示的16进制字符串还原成一个字节
bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character
.digit(hexString.charAt(i + 1), 16));
}
return bytes;
}
/**
*
*
* @return
*/
public static String BinaryToHexString(byte[] bytes) {
String hexStr = "0123456789ABCDEF";
String result = "";
String hex = "";
for (byte b : bytes) {
hex = String.valueOf(hexStr.charAt((b & 0xF0) >> 4));
hex += String.valueOf(hexStr.charAt(b & 0x0F));
result += hex + " ";
}
return result;
}
}
Loading…
Cancel
Save