
document.write("<script type='text/javascript' src='../public/interface.js'></script>");
document.write("<script type='text/javascript' src='../static/layui/layui.all.js'></script>");

$(function(){

    var intreface = new Interface(1);


    //查找到所有串口
    function GetSerialFind() {
        let cnt = 0;
        var html = "";
        var allCom = intreface.getSerialFind().split(",");
        intreface.setSerialObj(allCom.length);
        for(let i=0; i<allCom.length; i++){
            var state = intreface.setSerialOpen(i, allCom[i], 9600, "None", 1, 8);//layer.msg(allCom[i] + ":" + state)
            if(state == "true"){
                html += "<option value ='" + allCom[i] + "'>" + allCom[i] + "</option>";
                eval("window.get_idn_" + cnt + " = function(i, dat){get_idn(i,dat);}");
                cnt++;
            }
        }
        intreface.setSerialObj(cnt);
        $("#Droplist_Port_input").html(html);
    }

    //加载串口波特率
    function SetSerialBaud() {
        // var allCom = [4800, 9600,19200,38400,57600,115200];
        var allBaud = [4800,4800, 9600,9600, 19200,19200, 38400,38400, 57600,57600, 115200,115200];//相同波特率查询两次,因为有些机型第一次并不应答
        var html = "";
        for(var i=0;i<allBaud.length;i++){
            html += "<option value ='" + allBaud[i] + "'>" + allBaud[i] + "</option>"
        }
        $("#Droplist_Baud_input").html(html);
    }




    let obj = {baud_cnt:[], port:[], baud:[], str:[], flag:true}
    $("#Button_open").click(function () {
        obj.port = [];
    });

    window.Time_1 = function() {
        if(obj.port.length <= 0){
            SetSerialBaud();
            GetSerialFind();
            obj.str = [];
            obj.port = [];
            obj.baud = [];
            obj.baud_cnt = [];
            $("#Droplist_Port_input option").each(function(){
                var port = $(this).val();
                obj.str.push("");
                obj.port.push(port);
                obj.baud_cnt.push(0);
            });

            $("#Droplist_Baud_input option").each(function(cnt){
                var baud = $(this).val();
                obj.baud.push(baud);
            });
        }
        else {
            for(let a=0; a<obj.port.length; a++){
                if(obj.flag == true && obj.baud_cnt[a] < obj.baud.length && intreface.getFreeTime(a) > 200){
                    if(intreface.getSerialIsOpen(a) == "True"){
                        var state = intreface.setSerialClose(a);
                    }
                    var state = intreface.setSerialOpen(a, obj.port[a], parseInt(obj.baud[obj.baud_cnt[a]]), "None", 1, 8);
                    if(intreface.getSerialComm(a, "*IDN?", "get_idn_" + a, 50, 200) == "ok"){//layer.msg(obj.str[a],{time:100000})
                        obj.str[a] += "Port: " + obj.port[a] + " Baud:" + obj.baud[obj.baud_cnt[a]] + " 尝试连接";
                        obj.baud_cnt[a]++;
                    }
                }
            }
        }
    }

    function to_url(a, addr) {

        obj.flag = false;
        intreface.setTimePause("Time_1", true);

        let PortState = JSON.parse(intreface.getSerialInfo());
        intreface.setSerialObj(1);
        let state = intreface.setSerialOpen(0, PortState.data[a]["PortName"], parseInt(PortState.data[a]["BaudRate"]), "None", 1, 8);

        // alert(JSON.stringify(PortState.data[a]) + "---" + addr);
        $(location).attr('href', addr);
    }

    function get_idn(a, str){
        if(str.length > 0){
            if(str.length > 10){
                if(str.indexOf("UTL82") >= 0 || str.indexOf("UTL85") >= 0){
                    to_url(a, '../ElectronicLoadTester/list_mode.html');
                }
                else if(str.indexOf("UT35") >= 0){
                    to_url(a, '../MicroOhm/dcresistancetester.html');
                }
                else if(str.indexOf("UDP53") >= 0){
                    to_url(a, '../dc_power_supply/portsettings.html');
                }else{ obj.str[a] += " 没有找到该机型\r";}
            }else{ obj.str[a] += " 应答长度太短\r";}
        }else{ obj.str[a] +=  " 没有应答\r";}


        let pt = "";
        for(let i=0; i<obj.str.length;i++){
            pt += obj.str[i];
        }
        $('#TextArea_log_input').val(pt);
    }

    intreface.setTime([["Time_1", 100]]);



});





