JavaScript一種直譯式腳本語言,是一種動(dòng)態(tài)類型、弱類型、基于原型的語言,內(nèi)置支持類型。它的解釋器被稱為JavaScript引擎,為瀏覽器的一部分,廣泛用于客戶端的腳本語言,最早是在HTML(標(biāo)準(zhǔn)通用標(biāo)記語言下的一個(gè)應(yīng)用)網(wǎng)頁上使用,用來給HTML網(wǎng)頁增加動(dòng)態(tài)功能。
本文介紹有關(guān)JavaScript獲取電子秤串口數(shù)據(jù)的編程代碼。
有關(guān)JavaScript獲取電子秤串口數(shù)據(jù)的完整代碼
//創(chuàng)建MSComm對(duì)象
functionuf_GetSerPortData()
{
try
{
MSComm1=newActiveXObject(“MSCOMMLib.MSComm.1”);
if((typeof(MSComm1)==“undefined”)||(MSComm1==null))
{
alert(“創(chuàng)建MSComm1對(duì)象失敗!”);
}
else
{
//綁定事件
fn();
}
}
catch(err)
{
alert(err.description);
}
}
varfn=function(){
functionMSComm1::OnComm(){
MSComm1_OnComm();
}
}
//事件響應(yīng)
functionMSComm1_OnComm()
{
switch(MSComm1.CommEvent)
{
case1:{window.alert(“SendOK!”);break;}//發(fā)送事件
case2:{Receive();break;}//接收事件
default:alert(“EventRaised!”+MSComm1.CommEvent);;
}
}
functionOperatePort()
{
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=“關(guān)閉串口”;
}catch(ex)
{alert(ex.message);}
}
}
functionConfigPort()
{
varcomport=“”;
varboundRate=“”;
varjiaoyanwei=“”;
varshujuwei=“”;
vartingzhiwei=“”;
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;//清空發(fā)送緩沖區(qū)
MSComm1.InBufferCount=0;//滑空接收緩沖區(qū)
*/
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;//清空發(fā)送緩沖區(qū)
MSComm1.InBufferCount=0;//滑空接收緩沖區(qū)
MSComm1.RThreshold=1;//接收一個(gè)字節(jié)就觸發(fā)omcom事件
alert(“已配置串口COM”+MSComm1.CommPort+“ 參數(shù):”+MSComm1.Settings);
}catch(ex){alert(ex.message);}
}
else{alert(“請(qǐng)先關(guān)閉串口后再設(shè)置!”);}
}
vartmpWeight=“”;
//接收數(shù)據(jù)
functionReceive()
{
//alert(“InBufferCount::”+MSComm1.InBufferCount);
varinputvalue=MSComm1.Input;
if(inputvalue.indexOf(‘g’)》=0){
return;
}
//alert(inputvalue);
tmpWeight+=inputvalue.replace(‘-’,‘’);
if(tmpWeight.length》16)
{
if(tmpWeight.indexOf(‘000’))
{
varweight=trim(tmpWeight.substr(5,5));
if(weight.indexOf(‘0’)==0)
{
weight=weight.replace(“0”,“0.”)
}
SKDBedit7.value=weight;
tmpWeight=“”;
}
}
//alert(“InBufferCount::”+MSComm1.InBufferCount);
}
/*
varweight;
varmyArray=newArray();
functionGetWeight()
{
}
*/
functionserPortInit()
{
SKDBcombobox1.value=“COM4”;
SKDBcombobox2.value=“9600”;
SKDBcombobox3.value=“無NONE”;
SKDBedit5.value=“8”;
SKDBedit6.value=“1”;
//初始化創(chuàng)建MSComm1對(duì)象
uf_GetSerPortData();
}
functiontrim(str){//刪除左右兩端的空格
returnstr.replace(/(^s*)|(s*$)/g,“”);
}
functionltrim(str){//刪除左邊的空格
returnstr.replace(/(^s*)/g,“”);
}
functionrtrim(str){//刪除右邊的空格
returnstr.replace(/(s*$)/g,“”);
}
評(píng)論
查看更多