zsh_root
2025-01-06 7857a444de69124e9f7fb45f98b0ae818b107f23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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;
 
    }
}