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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package ymodem;
 
import baowen.Dell_udpReceive;
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.IOException;
import java.net.*;
 
public class netWorkUpgradeThread extends Thread {
 
 
    private static byte[][] dataBytes; // 数据帧
    private static byte[] startBytes; // 起始帧
    private static byte[] endBytes ; // 结束帧
 
    private static final long TIMEOUT = 600000; // 超过30秒未回应则退出升级模式
    static int numSend = 0;
    static boolean SendDataFlag = false;
    static String ip = "";//目标设备ip地址
    static int port;//目标设备端口
    static DatagramSocket socket;
    static int bytlenth=1024;
    static DatagramPacket packet;
 
 
    public static void shengji(String ip1,int port1, String filePath1,byte[] byt,int nowPort){
        SendDataFlag=false;
        numSend=0;
        ip = ip1;
        port = port1;
        Dell_udpReceive.sendData(byt,ip,port);
        ComSendUpdateFile(filePath1);
        Dell_udpReceive.stopThread();//暂停解析线程
        chushihua(nowPort);
 
        new Dell_udpReceive(nowPort);//重新开始解析线程
    }
    
    // 构造函数,接收串口和文件路径
    public static void chushihua(int nowPort) {
        if (versionUpgradeComp.isWrite()) {
          if (socket==null){
              try {
                  socket = new DatagramSocket(nowPort);//创建socket
                  byte[] data = new byte[bytlenth]; // 创建byte数组
                  packet= new DatagramPacket(data,bytlenth);
                  new netWorkUpgradeThread().startThread();
              } catch (SocketException e) {
                  throw new RuntimeException(e);
              }
          }else {
              new netWorkUpgradeThread().startThread();
          }
        }
    }
 
    public void startThread() {
        Thread t = new Thread(this);
        t.start();
    }
 
    public void run() {
        try {
            // 调用升级过程
            upgtade();
            stopThread();
        } catch (Exception e) {
            Thread.currentThread().interrupt(); // 恢复中断标志
            System.out.println("线程被中断,正在退出升级过程...");
            e.printStackTrace();
        }
    }
 
    //升级固件
    private void upgtade(){
        long startTime = System.currentTimeMillis();
        boolean b = System.currentTimeMillis() - startTime < TIMEOUT;
        while (b&&!SendDataFlag) {
            // 等待回应
            byte[] recDat = getRecDat();
            // 如果设备没有回应(回应为空),则认为是超时
            if (recDat == null || recDat.length == 0) {
                continue;
            }
 
            showDataComp.addAreaTextMes(recDat,ip,port);
            if (recDat[0] == ymodem.ACK) {
                numSend++;
                sendData(numSend);
            } else if (recDat[0] == ymodem.NAK || recDat[0] == ymodem.C) {
                // 收到NAK或CA,退出升级
                sendData(numSend);
            }else if (recDat[0]==ymodem.CA){
                break;
            }
        }
        versionUpgradeComp.exitUpgrade();
    }
 
 
    //发送数据包
    public static void sendData(int shuJuZhen) {
        int length = dataBytes.length;
        if (shuJuZhen == 0) {
            sendData(startBytes,ip,port);
            getProgress(shuJuZhen, length);
        } else if (shuJuZhen <= length && shuJuZhen > 0) {
            sendData(dataBytes[shuJuZhen - 1],ip,port);
            getProgress(shuJuZhen, length);
        } else if (shuJuZhen == length + 1||shuJuZhen == length + 2) {
            sendData(new byte[]{(byte) ymodem.EOT},ip,port);
            getProgress(shuJuZhen, length);
        } else if (shuJuZhen == length + 3) {
            sendData(endBytes,ip,port);
            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 byte[] getRecDat()  {
        byte[] dat = new byte[0];
        try {
            dat = null;
            packet = new DatagramPacket(dat,100);
            socket.receive(packet);
            String hostAddress = packet.getAddress().getHostAddress();
            if (hostAddress.equals(ip)){
                int lenth=packet.getLength();
                dat=subBytes(packet.getData(),0, lenth);
            }
            return dat;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
 
 
        /**将数据发给网关*/
    public static void sendData(byte[] data,String ipout,int portout) {
        DatagramPacket packet=null;
        InetAddress address=null;
        //如果数据类型相同
        try {
            address=InetAddress.getByName(ipout);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        packet=new DatagramPacket(data,data.length, address, portout);
        try {
            socket.send(packet);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    public static byte[] subBytes(byte[] src, int begin, int count) {
        byte[] bs = new byte[count];
        System.arraycopy(src, begin, bs, 0, count);
        return bs;
    }
 
 
 
 
    public static void stopThread() {
        if (socket != null && !socket.isClosed()) {
            socket.close();//关闭socket对象
        }
    }
}