package baowen;
|
|
import gnu.io.SerialPort;
|
import index.JPanelMoudle.showDataComp;
|
import tools.ChuanKou.SerialPortUtil;
|
import tools.Jiaoyan;
|
import tools.Tools;
|
|
public class WriteReadAnchor {
|
|
//给扫描器发送指令
|
public static void updateLora(String zhi,
|
byte diZhi,
|
byte duxie,
|
String anchorID,
|
SerialPort serialPort,
|
byte lenth) {
|
byte[] write = write_loraAnc(diZhi,lenth ,duxie, Integer.parseInt(zhi),anchorID);
|
if (write == null) {
|
return;
|
}
|
sendData(serialPort, write);
|
}
|
|
public static void sendData(SerialPort serialPort, byte[] write) {
|
SerialPortUtil.sendData(serialPort,write);
|
showDataComp.addSendAreaTextMes(write);
|
}
|
|
|
/**@param start 起始地址
|
* @param lenth数据长度
|
* @param value[]具体的值*/
|
private static byte[] write_loraAnc(byte start ,byte lenth,byte duXie,int value,String id) {
|
byte[] buf =new byte[12+lenth];
|
byte[] buf1 =new byte[8+lenth];
|
//包头
|
buf[0]=(byte) 0x55;
|
//包头
|
buf[1]=(byte) 0xAA;
|
//指令类型
|
buf1[0]=(byte) 0x30;
|
//数据长度
|
buf1[1]=(byte) ((byte) 8+lenth);
|
//读模式
|
buf1[2]=(byte) duXie;
|
//起始地址
|
buf1[3]=(byte) start;
|
|
|
buf1[4]= Tools.toByteArray(id)[1];
|
buf1[5]=Tools.toByteArray(id)[0];
|
buf1[6]=Tools.toByteArray(id)[1];
|
buf1[7]=Tools.toByteArray(id)[0];
|
|
buf1[8]=Tools.intToRegisters(value)[3];
|
buf1[9]=Tools.intToRegisters(value)[2];
|
for(int i=0;i<buf1.length;i++) {
|
buf[i+2]=buf1[i];
|
}
|
//校验码
|
buf[buf.length-2]= Jiaoyan.check(buf1)[1];
|
buf[buf.length-1]=Jiaoyan.check(buf1)[0];
|
return buf;
|
}
|
|
|
|
//读取扫描器版本
|
public static byte[] ReadData(byte ref, byte lenth) {
|
byte[] buf = new byte[10];
|
byte[] buf1 = new byte[6];
|
//包头
|
buf[0] = (byte) 0x55;
|
//包头
|
buf[1] = (byte) 0xAA;
|
|
//指令类型(参数读写模式)
|
buf1[0] = (byte) 0x03;
|
//数据长度
|
buf1[1] = (byte) 0x06;
|
|
//读模式(读模式)
|
buf1[2] = (byte) 0x01;
|
//起始地址
|
buf1[3] = ref;
|
//数据长度
|
buf1[4] = lenth;
|
|
//固定值(读模式下为0x00)
|
buf1[5] = (byte) 0x00;
|
for (int i = 0; i < buf1.length; i++) {
|
buf[i + 2] = buf1[i];
|
}
|
|
//校验码
|
buf[8] = Jiaoyan.check(buf1)[1];
|
buf[9] = Jiaoyan.check(buf1)[0];
|
;
|
return buf;
|
}
|
|
|
/**@param start 起始地址
|
* @param lenth数据长度
|
* @param value[]具体的值*/
|
public static byte[] write_lora(byte start ,byte lenth,int value) {
|
byte[] buf =new byte[9+lenth];
|
byte[] buf1 =new byte[5+lenth];
|
//包头
|
buf[0]=(byte) 0x55;
|
//包头
|
buf[1]=(byte) 0xAA;
|
//指令类型
|
buf1[0]=(byte) 0x03;
|
//数据长度
|
buf1[1]=(byte) ((byte) 5+lenth);
|
//读模式
|
buf1[2]=(byte) 0x02;
|
//起始地址
|
buf1[3]=(byte) start;
|
|
//读写长度
|
buf1[4]=(byte) lenth;
|
|
buf1[5]=Tools.intToRegisters(value)[3];
|
buf1[6]=Tools.intToRegisters(value)[2];
|
for(int i=0;i<buf1.length;i++) {
|
buf[i+2]=buf1[i];
|
}
|
//校验码
|
buf[buf.length-2]=Jiaoyan.check(buf1)[1];
|
buf[buf.length-1]=Jiaoyan.check(buf1)[0];
|
return buf;
|
|
}
|
}
|