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
package ymodem;
 
 
 
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
 
public class getFilesDataBytes {
    public static final int SOH = 0x01; // 128字节数据包
    public static final int STX = 0x02; // 1024字节数据包
    private static int num = 0;  // 帧序号
 
    // 获取文件的起始数据包
    public static byte[] getStartData(File file) {
        int index = 0;
        byte[] dat = new byte[133];
        byte[] nameBytes = file.getName().getBytes();
        byte[] lengthBytes = String.valueOf(file.length()).getBytes();
        dat[index++] = SOH;
        dat[index++] = (byte) num++;
        dat[index++] = (byte) ~dat[1];
        // 组合文件名数据
        for (int i = 0; i < nameBytes.length; i++) {
            dat[i + index] = nameBytes[i];
        }
        index += nameBytes.length;
        dat[index++] = 0x00;
        // 组合文件长度数据
        for (int i = 0; i < lengthBytes.length; i++) {
            dat[i + index] = lengthBytes[i];
        }
        index += lengthBytes.length;
        dat[index++] = 0x00;
        byte[] crcDat = CRC16.getCRC16(dat, 3);
        dat[dat.length - 2] = crcDat[0];
        dat[dat.length - 1] = crcDat[1];
        return dat;
    }
 
    // 获取结束帧数据
    public static byte[] getEndData() {
        byte[] dat = new byte[133];
        dat[0] = SOH;
        dat[1] = (byte) 0x00;
        dat[2] = (byte) 0xFF;
        byte[] crcDat = CRC16.getCRC16(dat, 3);
        dat[dat.length - 2] = crcDat[0];
        dat[dat.length - 1] = crcDat[1];
        return dat;
    }
 
    /**
     * 获取数据帧数据
     *
     * @param file 文件
     * @return 数据帧数组
     * @throws IOException
     */
    public static byte[][] getData(File file) throws IOException {
        long len = file.length();
        int size = (int) Math.ceil(len / 1024d);
        int size1 = (int) Math.ceil(len / 1024d);
        double v = len % 1024d;
        if (v==0){
            int i = size + 1;
            size1=i;
        }
        //获取数据帧个数
        byte[][] result = new byte[size][1029];
        FileInputStream fis = new FileInputStream(file);
        for (int i = 0; i < size1-1 ; i++) {            //生成数据帧
            result[i][0] = STX;                        //1024字节数据包头
            result[i][1] = (byte) (num++);            //自动填充帧序号
            if (num > 255) {                        //帧序号最大255
                num = 0;
            }
            result[i][2] = (byte) ~result[i][1];        //取反帧序号
            fis.read(result[i], 3, 1024);
            byte[] crc16 = CRC16.getCRC16(result[i], 3);    //获取crc16校验
            result[i][result[i].length - 2] = crc16[0];
            result[i][result[i].length - 1] = crc16[1];    //填充crc16校验
        }
        int endLen = (int) len % 1024;
        //如果最后一帧不是整1024个字节
        if (endLen > 0) {
            //如果最后一帧长度小于等于128
            if (endLen <= 128) {
                //最后一帧使用128字节传输
                result[size - 1] = new byte[133];
                result[size - 1][0] = SOH;                        //128字节数据包头
                int n = fis.read(result[size - 1], 3, 128);        //填充128个字节数据
                for (int i = n + 3; i < 131; i++) {                    //剩余位填充0x1A
                    result[size - 1][i] = 0x1A;
                }
            } else {
                //最后一帧使用1024字节传输
                result[size - 1][0] = STX;                        //1024字节数据包头
                int n = fis.read(result[size - 1], 3, 1024);        //填充1024个数据
                for (int i = n + 3; i < 1027; i++) {                    //剩余位填充0x1A
                    result[size - 1][i] = 0x1A;
                }
            }
            result[size - 1][1] = (byte) (num++);                //自动填充帧序号
            if (num > 255) {                                //帧序号最大255
                num = 0;
            }
            result[size - 1][2] = (byte) ~result[size - 1][1];    //取反帧序号
            byte[] crc16 = CRC16.getCRC16(result[size - 1], 3);    //获取crc16校验
            result[size - 1][result[size - 1].length - 2] = crc16[0];
            result[size - 1][result[size - 1].length - 1] = crc16[1];    //填充crc16校验
        }
        fis.close();
        return result;
    }
 
 
 
    public static int getNum() {
        return num;
    }
 
    public static void setNum(int num) {
        getFilesDataBytes.num = num;
    }
}