SKT500下载协议


title: SKT500下载协议

SKT500下载协议_第1张图片
基本流程图.png

github上一个实现

STK500 implementations


import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.FileNotFoundException;

public class STK500 {
    private OutputStream outStream = null;
    private InputStream inputStream = null;

    private static void openStreams() {
        try {
            /* TODO: implement opening bluetooth */
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void closeStreams() {
        try {
            /* TODO: implement closing bluetooth */
            outStream.close();
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static byte[] readProgram() {
        FileInputStream fis = null;
        File file = new File("/sdcard/sumorobot/main.hex");

        // every line, except last one, has has 45 bytes (including \r\n)
        int programLines = (int) Math.ceil(file.length() / 45.0);
        // every line has 32 bytes of program data (excluding checksums, addresses, etc.)
        int unusedBytes = 45 - 32;
        // calculate program length according to program lines and unused bytes
        int programLength = (int) file.length() - (programLines * unusedBytes);
        // the actualy program data is half the size, as the hex file represents hex data in individual chars
        programLength /= 2;
        // create a byte array with the program length
        byte[] program = new byte[programLength];

        try {
            // open the file stream
            log.logcat("opening hex file", "d");
            fis = new FileInputStream(file);
            log.logcat("Total program size (in bytes) : " + programLength, "d");
            log.logcat("Total file size to read (in bytes) : " + fis.available(), "d");

            int content;
            int lineIndex = 0;
            int lineNumber = 1;
            int programIndex = 0;
            char[] line = new char[45];
            // read the file byte by byte
            while ((content = fis.read()) != -1) {
                // append byte to the line
                line[lineIndex++] = (char) content;
                // when the line is complete
                if (content == 10) {
                    // take only the actual program data form the line
                    for (int index = 9; index < lineIndex - 4; index += 2) {
                        // convert hexadecimals represented as chars into bytes
                        program[programIndex++] = Integer.decode("0x" + line[index] + line[index+1]).byteValue();
                    }
                    // start a new line
                    lineIndex = 0;
                }
            }
        } catch (IOException e) {
            log.logcat("reading hex failed: " + e.getMessage(), "d");
        } finally {
            try {
                if (fis != null)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return program;
    }

    public static void main (String[] args) {
        byte[] program = readProgram();
        System.out.println("program length: " + program.length);

        System.out.println("opening streams");
        openStreams();

        System.out.println("syncing");
        for (int i = 0; i < 5; i++) {
            outStream.write(0x30);
            outStream.write(0x20);
            try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}
        }

        System.out.println("waiting for response");
        int insync = inputStream.read();
        int ok = inputStream.read();
        if (insync == 0x14 && ok == 0x10) {
            System.out.println("insync");
        }

        System.out.println("reading major version");
        outStream.write(0x41);
        outStream.write(0x81);
        outStream.write(0x20);
        try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}

        System.out.println("waiting for response");
        insync = inputStream.read();
        int major = inputStream.read();
        ok = inputStream.read();
        if (insync == 0x14 && ok == 0x10) {
            System.out.println("insync", "d");
        }

        System.out.println("reading minor version");
        outStream.write(0x41);
        outStream.write(0x82);
        outStream.write(0x20);
        try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}

        System.out.println("waiting for response");
        insync = inputStream.read();
        int minor = inputStream.read();
        ok = inputStream.read();
        if (insync == 0x14 && ok == 0x10) {
            System.out.println("insync");
        }

        System.out.println("version: " + major + "." + minor);

        System.out.println("entering programming mode");
        outStream.write(0x50);
        outStream.write(0x20);
        try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}

        System.out.println("waiting for response");
        insync = inputStream.read();
        ok = inputStream.read();
        if (insync == 0x14 && ok == 0x10) {
            System.out.println("insync");
        }

        System.out.println("getting device signature");
        outStream.write(0x75);
        outStream.write(0x20);
        try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}

        System.out.println("waiting for response");
        insync = inputStream.read();
        byte [] signature = new byte[3];
        inputStream.read(signature, 0, 3);
        ok = inputStream.read();
        if (insync == 0x14 && ok == 0x10) {
            System.out.println("insync");
        }

        System.out.println("signature: " + signature[0] + "." + signature[1] + "." + signature[2]);

        int size = 0;
        int address = 0;
        int programIndex = 0;
        while (true) {
            int laddress = address % 256;
            int haddress = address / 256;
            address += 64;

            System.out.println("loading page address");
            outStream.write(0x55);
            outStream.write(laddress);
            outStream.write(haddress);
            outStream.write(0x20);
            try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}

            System.out.println("waiting for response");
            insync = inputStream.read();
            ok = inputStream.read();
            if (insync == 0x14 && ok == 0x10) {
                System.out.println("insync");
            }

            if (program.length - programIndex < 128) {
                size = program.length - programIndex;
            } else {
                size = 128;
            }
            System.out.println("programming page size: " + size + " haddress: " + haddress + " laddress: " + laddress);
            outStream.write(0x64);
            outStream.write(0x00);
            outStream.write(size);
            outStream.write(0x46);
            for (int i = 0; i < size; i++) {
                outStream.write(program[programIndex++]);
            }
            outStream.write(0x20);
            try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}

            System.out.println("receiving sync ack");
            insync = inputStream.read();
            ok = inputStream.read();

            if (insync == 0x14 && ok == 0x10) {
                System.out.println("insync");
            }

            if (size != 0x80) {
                break;
            }
        }
        System.out.println("program index: " + programIndex);

        System.out.println("leaving programming mode");
        outStream.write(0x51);
        outStream.write(0x20);
        try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}

        System.out.println("receiving sync ack");
        insync = inputStream.read();
        ok = inputStream.read();

        if (insync == 0x14 && ok == 0x10) {
            System.out.println("insync");
        }

        System.out.println("closing streams");
        closeStreams();
    }
}

自己实现的

    class uploadthread extends Thread {
        @Override
        public synchronized void run() {
            super.run();
            Message message = new Message();
            message.what = 1;
            message.obj = "开始下载!";

            downloadStatus = true;   //开始下载 先置位

            mHandler.sendMessage(message);
            mRxBle.sendData(mRxBle.getMsg("AT+REAST~*&$@!", 0));
            sleepsec(1000);
            mRxBle.sendData(mRxBle.getMsg("30203020302030203020", 1));
            responseOk = 4;
            monitorHandler.postDelayed(runnable, TIME); //每隔1s执行

        }

    }
 private void autoUpload() {

        mRxBle.receiveData().subscribe(new Action1() {
            @Override
            public void call(String receiveData) {
                byte[] tmp_byte = receiveData.getBytes();
                if (0 == tmp_byte.length) {
                    return;
                }
                String tmp = "";
                for (int i = 0; i < tmp_byte.length; i++) {
                    String hex = Integer.toHexString(tmp_byte[i] & 0xFF);
                    if (hex.length() == 1) {
                        hex = '0' + hex;
                    }
                    tmp += ' ';
                    tmp = tmp + hex;
                }
                System.out.println("接收到数据:" + tmp);
                if (tmp.endsWith(Integer.toHexString(0x10 & 0xff))) {
                    if (sendHexFlag == 0) {
                        responseOk++;
                    }
                    System.out.println("responseOk:" + responseOk + ";");

                    downloadStatus = true;  //正常下载 置位成功

                    switch (responseOk) {
                        case 5:
                            mRxBle.sendData(mRxBle.getMsg("418120418220", 1));//查询软件主次版本号码
                            break;
                        case 6: //查询设置参数

                            mRxBle.sendData(mRxBle.getMsg("428600000101010103ffffffff008004000000", 1));
                            try {
                                Thread.sleep(20);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            mRxBle.sendData(mRxBle.getMsg("800020", 1));
                            break;
                        case 7:// 并行编程
                            mRxBle.sendData(mRxBle.getMsg("450504d7c20020", 1));
                            break;
                        case 8://进入设置模式。
                            mRxBle.sendData(mRxBle.getMsg("5020", 1));     //进入设置模式。
                            break;
                        case 9://获取设备签名。
                            mRxBle.sendData(mRxBle.getMsg("7520", 1)); //获取设备签名。
                            break;
                        case 10://发送Hex文件
                            sendHexFlag++;

                            if (sendHexFlag % 2 == 1) {   //发送地址
                                int laddress = address % 256;
                                int haddress = address / 256;
                                address += 64;
                                System.out.println("发送地址。" + address);
                                String addressPackage = "";
                                addressPackage = "55" + to2String(laddress) + to2String(haddress) + "20";
                                mRxBle.sendData(mRxBle.getMsg(addressPackage, 1));
                            } else { //发送内容
                                if (program.length - programIndex < 128) {
                                    size = program.length - programIndex;
                                } else {
                                    size = 128;
                                }
                                System.out.println("programming page size: " + size);
                                if (size < 128) {
                                    int tempindex = programIndex;
                                    byte[] lastbytes = new byte[128];
                                    for (int i = 0; i < size - 6; i++) {
                                        lastbytes[i] = program[tempindex++];
                                    }
                                    for (int i = (size - 6); i < 128; i++) {
                                        lastbytes[i] = (byte) 0xff;
                                    }
                                    String size2str = to2String(128);
                                    String oneLine = "";
                                    for (int i = 0; i < 128; i++) {
                                        String hex = Integer.toHexString(lastbytes[i] & 0xff);
                                        if (hex.length() == 1) {
                                            hex = "0" + hex;
                                        }
                                        oneLine += hex;
                                    }
                                    oneLine = "6400" + size2str + "46" + oneLine + "20";
                                    System.out.println("长度:" + oneLine.length());
                                    for (int i = 0; i < ((int) (oneLine.length() / 38)); i++) {
                                        String ss = oneLine.substring(i * 38, i * 38 + 38);
                                        mRxBle.sendData(mRxBle.getMsg(ss, 1));

                                        try {
                                            Thread.sleep(20);
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        }

                                    }
                                } else {
                                    String size2str = to2String(size);
                                    String oneLine = "";
                                    for (int i = 0; i < size; i++) {
                                        String hex = Integer.toHexString(program[programIndex++] & 0xff);
                                        if (hex.length() == 1) {
                                            hex = "0" + hex;
                                        }
                                        oneLine += hex;
                                    }
                                    oneLine = "6400" + size2str + "46" + oneLine + "20";

                                    for (int i = 0; i < ((int) (oneLine.length() / 38)); i++) {
                                        String ss = oneLine.substring(i * 38, i * 38 + 38);
                                        mRxBle.sendData(mRxBle.getMsg(ss, 1));
                                        try {
                                            Thread.sleep(20);
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }
                                if (size != 0x80) {
                                    sendHexFlag = 0;
                                }

                            }
                            System.out.println("program.length:"+program.length + "    programindex   "+ programIndex);
                            int progress = 0;   // 下载进度
                            progress = programIndex * 100 / program.length;
                            System.out.println("progress:" + progress);
                            Message message = new Message();
                            message.what = 2;
                            message.obj = progress;
                            mHandler.sendMessage(message);
                            break;
                        case 11:
                            System.out.println("program index: " + programIndex);
                            System.out.println("leaving programming mode");
                            mRxBle.sendData(mRxBle.getMsg("5120", 1));
                            message = new Message();
                            message.what = 3;
                            message.obj = "下载成功!";
                            progress = 0;
                            mHandler.sendMessage(message);
                            System.out.println("下载成功!");


                            monitorHandler.removeCallbacks(runnable);
//                            Toast.makeText(ScanActivity.this, "下载成功", Toast.LENGTH_SHORT).show();
                            break;
                        default:
                            break;
                    }

                }
            }
        });
    }

你可能感兴趣的:(SKT500下载协议)