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;
|
}
|
}
|