You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package UnitTest;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class HexStringSenderBySocket {
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 KG() 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);
InputStream in = socket.getInputStream();
byte[] buffer = new byte[1024];
int len = in.read(buffer);
byte[] resp = new byte[len];
System.arraycopy(buffer, 0, resp, 0, len);
System.out.println("response: " + bytesToHexString(resp));
socket.close();
Thread.sleep(2000);
}
}
public static void getSbList() throws IOException {
String hex_str="08 00 34 F5 41 11 FE 81";
Socket socket = new Socket(IP, PORT);
byte[] bytes = hexStringToByteArray(hex_str);
OutputStream os = socket.getOutputStream();
os.write(bytes);
InputStream in = socket.getInputStream();
byte[] buffer = new byte[1024];
int len = in.read(buffer);
byte[] resp = new byte[len];
System.arraycopy(buffer, 0, resp, 0, len);
System.out.println("response: " + bytesToHexString(resp));
socket.close();
}
public static void main(String[] args) throws IOException, InterruptedException {
//KG();
getSbList();
}
public static String bytesToHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02X", b));
sb.append(" ");
}
return sb.toString();
}
/**
* 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;
}
}