package com.fjasmin.testjava.newLed;
import org.w3c.dom.ls.LSOutput;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import static com.fjasmin.testjava.newLed.test01.CRC16_IBM;
import static com.fjasmin.testjava.newLed.test01.conver;
public class LEDBright {
public static void main(String[] args) {
System.out.println(setBright(1));
}
private static String setBright( int bright){
//头部
final byte[] HEAD_LED = new byte[]{(byte) 0xA5, (byte) 0xA5, (byte) 0xA5, (byte) 0xA5, (byte) 0xA5, (byte) 0xA5, (byte) 0xA5, (byte) 0xA5};
//尾部
final byte[] END_LED = new byte[]{(byte) 0x5A};
//1.区域数据
ByteBuffer birghtBuffer = ByteBuffer.allocate(512);
birghtBuffer.put((byte)0X01);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X80);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0Xfe);
birghtBuffer.put((byte)0X02);
birghtBuffer.put((byte)0X37);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0Xa3);
birghtBuffer.put((byte)0X02);
birghtBuffer.put((byte)0X01);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X01);
//设置亮度
birghtBuffer.put((byte) (bright & 0XFF));
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.put((byte)0X00);
birghtBuffer.flip();
int birghtBufferLength = birghtBuffer.limit();
byte[] birghtBufferData = new byte[birghtBufferLength];
birghtBuffer.get(birghtBufferData);
//数据效验 包校验为包头数据和数据域的校验值。
short checkdata = (short) CRC16_IBM(birghtBufferData);
//完整包
ByteBuffer birght_buffer_all = ByteBuffer.allocate(2048);
birght_buffer_all.put(HEAD_LED);
birght_buffer_all.put(birghtBufferData);
birght_buffer_all.order(ByteOrder.LITTLE_ENDIAN);
birght_buffer_all.putShort(checkdata);//这里要与CheckMode的值对应上 =2,不需要校验
birght_buffer_all.put(END_LED);
birght_buffer_all.flip();
return bytes2hex(conver(birght_buffer_all));
}
public static String bytes2hex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
String tmp = null;
for (byte b : bytes) {
// 将每个字节与0xFF进行与运算,然后转化为10进制,然后借助于Integer再转化为16进制
tmp = Integer.toHexString(0xFF & b);
if (tmp.length() == 1) {
tmp = "0" + tmp;
}
sb.append(tmp);
}
return sb.toString();
}
}
网友评论