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
138
139
140
141
142
143
144
145
package ymodem;
 
import gnu.io.SerialPort;
import index.JPanelMoudle.showDataComp;
import index.JPanelMoudle.versionUpgradeComp;
import tools.ChuanKou.SerialPortUtil;
import tools.ShowMessage;
import tools.Tools;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
 
public class YModemUpgradeThread extends Thread {
 
    private static byte[][] dataBytes; // 数据帧
    private static byte[] startBytes; // 起始帧
    private static byte[] endBytes ; // 结束帧
    private static final long TIMEOUT = 30000; // 超过30秒未回应则退出升级模式
    private static SerialPort serialPort;
  
    static int numSend = 0;
    static boolean SendDataFlag = false;
 
 
    public static void shengji(SerialPort serialPort1, String filePath1,byte[] byt){
        SendDataFlag=false;
        numSend=0;
        serialPort = serialPort1;
        SerialPortUtil.sendData(serialPort, byt);
        ComSendUpdateFile(filePath1);
        chushihua();
    }
    
    // 构造函数,接收串口和文件路径
    public static void chushihua() {
        if (versionUpgradeComp.isWrite()) {
            if (serialPort == null) {
                return;
            } else {
                new YModemUpgradeThread().startThread();
            }
        }
    }
 
    public void startThread() {
        Thread t = new Thread(this);
        t.start();
    }
 
    public void run() {
        try {
            // 调用升级过程
            upgtade();
        } catch (Exception e) {
            Thread.currentThread().interrupt(); // 恢复中断标志
            System.out.println("线程被中断,正在退出升级过程...");
            e.printStackTrace();
        }
    }
 
    //升级固件
    private void upgtade(){
        long startTime = System.currentTimeMillis();
        long l = System.currentTimeMillis() - startTime;
        boolean b =  l< TIMEOUT;
        while (b&&!SendDataFlag) {
            b=System.currentTimeMillis()-startTime<TIMEOUT;
            // 等待回应
            byte[] response = SerialPortUtil.readData(serialPort);
            // 如果设备没有回应(回应为空),则认为是超时
            if (response == null || response.length == 0) {
                continue;  // 如果没有回应,继续等待
            }
            showDataComp.addAreaTextMes(response,"127.0.0.1",9999);
            if (response[0] == ymodem.ACK) {
                numSend++;
                sendData(numSend);
            } else if (response[0] == ymodem.NAK || response[0] == ymodem.C) {
                // 收到NAK或CA,退出升级
                sendData(numSend);
            }else if (response[0]==ymodem.CA){
                break;
            }
        }
        if (!b){
            ShowMessage.zidingyi("数据接收超时,退出升级模式");
        }
        versionUpgradeComp.exitUpgrade();
    }
 
 
    //发送数据包
    public static void sendData(int shuJuZhen) {
        int length = dataBytes.length;
        if (shuJuZhen == 0) {
            SerialPortUtil.sendData(serialPort, startBytes);
            getProgress(shuJuZhen, length);
        } else if (shuJuZhen <= length && shuJuZhen > 0) {
            SerialPortUtil.sendData(serialPort, dataBytes[shuJuZhen - 1]);
            getProgress(shuJuZhen, length);
        } else if (shuJuZhen == length + 1||shuJuZhen == length + 2) {
            SerialPortUtil.sendData(serialPort,new byte[]{(byte) ymodem.EOT});
            getProgress(shuJuZhen, length);
        } else if (shuJuZhen == length + 3) {
            SerialPortUtil.sendData(serialPort,endBytes);
            getProgress(shuJuZhen, length);
            ShowMessage.zidingyi("升级完成");
            SendDataFlag=true;
            numSend = 0;
        }
    }
 
    //计算当前包的进度
    private static void getProgress(double shuJuZhen, int length) {
        int i =(int) Math.round((shuJuZhen / (length +3)) * 100);
        versionUpgradeComp.getJindu_SaoMiaoQi().setValue(i);
    }
 
    //获取要发送的帧数据
    public static void ComSendUpdateFile(String filePath){
        try {
            File file = new File(filePath);
            if (!file.exists()) {                                //如果文件不存在
                ShowMessage.zidingyi("文件不存在" + filePath);        //抛出文件找不到异常
                return;
            }
            getFilesDataBytes.setNum(0);
            startBytes = getFilesDataBytes.getStartData(file);//获取起始帧数据包
            dataBytes = getFilesDataBytes.getData(file);//获取数据帧数据包
            endBytes = getFilesDataBytes.getEndData();//获取结束帧数据包
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
 
    public static boolean isSendDataFlag() {
        return SendDataFlag;
    }
 
    public static void setSendDataFlag(boolean sendDataFlag) {
        SendDataFlag = sendDataFlag;
    }
}