返回官网

JS获取电子秤串口数据

狒狒 2017-5-20 技术札记 1071 次
function uf_GetSerPortData()
{
    try
    {
        MSComm1 = new ActiveXObject("MSCOMMLib.MSComm.1");
        if ((typeof (MSComm1) == "undefined") || (MSComm1 == null))
        {
            alert("创建MSComm1对象失败!");
        }
        else
        {
            //绑定事件
            fn();
        }
    }
    catch (err)
    {
        alert(err.description);
    }
}

var fn=function(){
    function MSComm1::OnComm() {
        MSComm1_OnComm();
    }
}

对于 function MSComm1::OnComm() 这种写法,我之前从没见过,后来是在一篇博文上面看到的,试了下发现真的可以实现,事件绑定。这样就完成了在js中进行事件绑定了。后面的回调函数显示重量数值就很简单了。

 

其中   new ActiveXObject("MSCOMMLib.MSComm.1"); 引号中的是activex控件的ProgID,在注册表中classid所对应下的项中可以找到。也就是创建控件的时候起的名字。

 

下面贴上实现的完整代码(直接从实现demo当中抠出来的,关于数值显示的地方代码丑陋大家略过就行。但肯定是可以实现需求的,已经测试过)

//创建MSComm对象
function uf_GetSerPortData()
{
    try
    {
        MSComm1 = new ActiveXObject("MSCOMMLib.MSComm.1");
        if ((typeof (MSComm1) == "undefined") || (MSComm1 == null))
        {
            alert("创建MSComm1对象失败!");
        }
        else
        {
            //绑定事件
            fn();
        }
    }
    catch (err)
    {
        alert(err.description);
    }
}

var fn=function(){
    function MSComm1::OnComm() {
        MSComm1_OnComm();
    }
}

//事件响应
function   MSComm1_OnComm()
{
    switch(MSComm1.CommEvent)
    {
        case 1:{ window.alert("Send OK!"); break;}  //发送事件
        case 2: { Receive();break;} //接收事件
        default: alert("Event Raised!"+MSComm1.CommEvent);;
    }
}



function OperatePort()
{
    if(MSComm1.PortOpen==true)
    {
        try{MSComm1.PortOpen=false;
            SKButton1.value="打开串口";
        }catch(ex)
        {alert(ex.message);}
    }
    else{
        try{ MSComm1.PortOpen=true;
            MSComm1.InBufferCount = 0;
            SKButton1.value="关闭串口";
        }catch(ex)
        {alert(ex.message);}
    }
}

function ConfigPort()
{
    var comport="";
    var boundRate="";
    var jiaoyanwei="";
    var shujuwei="";
    var tingzhiwei="";
    comport=SKDBcombobox1.value;
    boundRate=SKDBcombobox2.value;
    jiaoyanwei=SKDBcombobox3.value;
    shujuwei=SKDBedit5.value;
    tingzhiwei=SKDBedit6.value;

    if(MSComm1.PortOpen==false)
    {
        try{
            /*
                MSComm1.CommPort=comport;
                MSComm1.Settings=boundRate+","+jiaoyanwei+","+shujuwei+","+tingzhiwei;
                MSComm1.OutBufferCount =0;           //清空发送缓冲区
                MSComm1.InBufferCount = 0;           //滑空接收缓冲区
             */
            MSComm1.CommPort="4";
            switch(SKDBcombobox1.value)
            {
                case "COM1":
                    MSComm1.CommPort="1";
                    break;
                case "COM2":
                    MSComm1.CommPort = "2";
                    break;
                case "COM3":
                    MSComm1.CommPort = "3";
                    break;
            }
            
            MSComm1.Settings="9600"+
                                 ","+"n"+
                                 ","+"8"+
                                 ","+"1";
            MSComm1.OutBufferCount =0;           //清空发送缓冲区
            MSComm1.InBufferCount = 0;           //滑空接收缓冲区
            MSComm1.RThreshold=1;                    //接收一个字节就触发omcom事件


            alert("已配置串口COM"+MSComm1.CommPort+"\n 参数:"+MSComm1.Settings);
        }catch(ex){alert(ex.message);}
    }
    else{ alert("请先关闭串口后再设置!");}
}
var tmpWeight = "";

//接收数据
function Receive()
{
    //alert("InBufferCount::"+MSComm1.InBufferCount);
    var inputvalue = MSComm1.Input;


    if (inputvalue.indexOf('g') >= 0) {
        return;
    }
    // alert(inputvalue);

    tmpWeight+=inputvalue.replace('-', '');

    if(tmpWeight.length>16)
    {
        if(tmpWeight.indexOf('000'))
        {
            var weight=trim(tmpWeight.substr(5,5));
            if(weight.indexOf('0')==0)
            {
                weight=weight.replace("0","0.")
            }
            SKDBedit7.value=weight;
            tmpWeight="";
        }

    }



    //alert("InBufferCount::"+MSComm1.InBufferCount);
}
/*
var weight;
var myArray=new Array();
function GetWeight()
{

}
*/
function serPortInit()
{
    SKDBcombobox1.value="COM4";
    SKDBcombobox2.value="9600";
    SKDBcombobox3.value="无NONE";
    SKDBedit5.value="8";
    SKDBedit6.value="1";

    //初始化创建MSComm1对象
    uf_GetSerPortData();
}



function trim(str){ //删除左右两端的空格
    return str.replace(/(^\s*)|(\s*$)/g, "");
}
function ltrim(str){ //删除左边的空格
    return str.replace(/(^\s*)/g,"");
}
function rtrim(str){ //删除右边的空格
    return str.replace(/(\s*$)/g,"");
}

发表评论

Copyright © 2016 DEWEBSTUDIO