SKT500下载协议


title: SKT500下载协议

基本流程图.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<String>() {
            @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;
                    }

                }
            }
        });
    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,324评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,303评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,192评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,555评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,569评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,566评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,927评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,583评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,827评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,590评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,669评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,365评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,941评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,928评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,159评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,880评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,399评论 2 342

推荐阅读更多精彩内容