JAVA-串口或者Socket发送16进制指令数据
指令格式举例:02 9c A5 25 00 00 02 79 31 异或值
/**
* 字节数组转字符串,并转为大写
* @param buffer
* @param strSeq -> 分隔符
* @return 返回字符串格式示例为:010101
*/
public static String BytesToString(byte[] buffer, String strSeq)
{
if (buffernull || buffer.length0) {
return null;
}
if (strSeq==null) {
strSeq="";
}
StringBuilder sb = new StringBuilder();
for (int i=0; i<buffer.length; i++) {
sb.append(String.format("%02x", buffer[i]) + (i<buffer.length-1 ? strSeq : “”));
}
return sb.toString().toUpperCase();
}
/**
* bytes字符串转换为Byte值
* @param src Byte字符串,每个Byte之间没有分隔符
* @return byte[]
/
public static byte[] hexStr2Bytes(String src)
{
int m=0,n=0;
int l=src.length()/2;
System.out.println(l);
byte[] ret = new byte[l];
for (int i = 0; i < l; i++)
{
m=i2+1;
n=m+1;
ret[i] = Byte.decode(“0x” + src.substring(i*2, m) + src.substring(m,n));
}
return ret;
}
/**
* Hex转byte[],两种情况,Hex长度为奇数最后一个字符会被舍去
*/
public static byte[] hexTobytes(String hex, int num) {
String[] strings = hex.trim().split(" ");
if (strings.length != num) {
return null;
}
byte[] bytes = new byte[num];
for (int i = 0; i < num; i++) {
bytes[i] = (byte)Integer.parseInt(strings[i],16);
}
return bytes;
}
/**
* Hex转byte,hex只能含两个字符,如果是一个字符byte高位会是0
*/
public static byte hexTobyte(String hex) {
return (byte)Integer.parseInt(hex, 16);
}
/**
* 合并多个字节数组
* @param num 字节数组的数量
* @param b 多个字节数组
* @return 合并后的数组
*/
public static byte[] CombineBytes(int num, byte[]... b) {
if (num == b.length && num > 0) {
for (byte[] bb : b) {
if (bb == null) {
return null;
}
}
int idx = 0;
//byte[] bytes = new byte[1];
byte[] bytes = b[0];
while (idx < num - 1) {
if (idx == 0) {
bytes = CombineTwoBytes(b[0], b[1]);
} else {
bytes = CombineTwoBytes(bytes, b[idx + 1]);
}
idx++;
}
return bytes;
}
return null;
}
/**
* 合并两个字节数组
* @param b1 字节数组1
* @param b2 字节数组2
* @return 合并后的数组
*/
public static byte[] CombineTwoBytes(byte[] b1, byte[] b2) {
if (b1 == null || b2 == null) {
return null;
}
//合并数组
byte[] b = new byte[b1.length + b2.length];
System.arraycopy(b1,0,b,0,b1.length);
System.arraycopy(b2,0,b,b1.length,b2.length);
return b;
}
/**
* byte转16进制字符串
* @param bytes
* @return
*/
public static String bytesToHex(byte[] bytes) {
StringBuffer sb = new StringBuffer();
for(int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(bytes[i] & 0xFF);
if(hex.length() < 2){
sb.append(0);
}
sb.append(hex);
}
return sb.toString();
}
/**
* 计算异或
* @param content
* @return
*/
public static String yihuo(String content) {
content = change(content);
String[] b = content.split(" ");
int a = 0;
for (int i = 0; i < b.length; i++) {
a = a ^ Integer.parseInt(b[i], 16);
}
if(a<10){
StringBuffer sb = new StringBuffer();
sb.append(“0”);
sb.append(a);
return sb.toString();
}
return Integer.toHexString(a);
}
public static String change(String content) {
String str = "";
for (int i = 0; i < content.length(); i++) {
if (i % 2 == 0) {
str += " " + content.substring(i, i + 1);
} else {
str += content.substring(i, i + 1);
}
}
return str.trim();
}
byte[] head = {(byte)0x02};
//(byte)0x9C,(byte)0xA5,(byte)0x25,(byte)0x00,(byte)0x00,(byte)0x02,
byte[] stationMac = LabelUtils.hexTobytes(labelDTO.getLabelMac(),6);
byte[] end = {(byte)0x79,(byte)0x31};
byte[] bytes = LabelUtils.CombineBytes(3, head, stationMac, end);//合并多个字节数组
String bytesStr = LabelUtils.bytesToHex(bytes); //字节转十六进制字符串
String yihuo = LabelUtils.yihuo(bytesStr); //算出异或值
byte[] command= new byte[bytes.length+1];
System.arraycopy(bytes, 0,command ,0,bytes.length);
command[bytes.length] = LabelUtils.hexStr2Bytes(yihuo)[0]; //bytes字符串转换为Byte值
System.out.println(LabelUtils.hexStr2Bytes(yihuo));
byte[] fa={(byte)0xFA};
byte[] fb={(byte)0xFB};
byte[] sendCommand = LabelUtils.CombineBytes(3, fa, command, fb);
String s = LabelUtils.BytesToString(sendCommand, "");//字节数组转字符串,并转为大写
System.out.println("发送的指令:"+s);
InputStream inStream = null;
OutputStream outStream = null;
try {
Socket socket = new Socket("192.168.0.87",8001);
outStream = socket.getOutputStream();
inStream = socket.getInputStream();
outStream.write(sendCommand);
outStream.flush();
//收包
byte[] responseBytes = null;
byte[] tempBytes = new byte[512];
int readLength = inStream.read(tempBytes);
responseBytes = new byte[readLength];
System.arraycopy(tempBytes, 0, responseBytes, 0, readLength);
//收←◆FA 01 9C A5 25 00 00 02 79 31 57 FB
System.out.println(LabelUtils.BytesToString(responseBytes, ""));
outStream.close();
inStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}