做一个通过使用电子称直接连接电脑读取电子称重量的程序。中间遇到不少问题,记录下,下次再搞的时候好查找。
硬件方面: 有串口的电子称。通过超级终端能读到串口数据就可以。
软件方面: MSCOMM32.OCX。google一下能找到很多,使用Regsvr32注册
剩下的就是在Web页面中调用了。下面的代码修改自别人的程序
代码
<html>
<head>
<title>JavaScript串口测试</title>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
<SCRIPT ID=clientEventHandlersJS LANGUAGE=javascript>
<!--
function MSComm1_OnComm()
{
switch(MSComm1.CommEvent)
{
case 1:{ window.alert("Send OK!"); break;} //发送事件
case 2: { Receive();break;} //接收事件
default: alert("Event Raised!"+MSComm1.CommEvent);;
}
}
//-->
</SCRIPT>
<SCRIPT LANGUAGE=javascript FOR=MSComm1 EVENT=OnComm>
<!--
// MSComm1控件每遇到 OnComm 事件就调用 MSComm1_OnComm()函数
MSComm1_OnComm()
//-->
</SCRIPT>
<script language="JavaScript" type="text/JavaScript">
<!--
String.prototype.Blength = function(){
var arr = this.match(/[^\x00-\xff]/ig);
return arr == null ? this.length : this.length + arr.length;
}
function OperatePort()
{
if(MSComm1.PortOpen==true)
{
try{MSComm1.PortOpen=false;
document.getElementById("OperateButton").value="打开串口";
}catch(ex)
{alert(ex.message);}
}
else{
try{ MSComm1.PortOpen=true;
document.getElementById("OperateButton").value="关闭串口";
}catch(ex)
{alert(ex.message);}
}
}
function ConfigPort()
{
if(MSComm1.PortOpen==false)
{
try{
MSComm1.CommPort=document.getElementById("ComName").value;
MSComm1.Settings=document.getElementById("BaudRate").value.toString()+
","+document.getElementById("CheckBit").value.toString()+
","+document.getElementById("DataBits").value.toString()+
","+document.getElementById("StopBits").value.toString();
MSComm1.OutBufferCount =0; //清空发送缓冲区
MSComm1.InBufferCount = 0; //滑空接收缓冲区
alert("已配置串口COM"+MSComm1.CommPort+"\n 参数:"+MSComm1.Settings);
}catch(ex){alert(ex.message);}
}
else{ alert("请先关闭串口后再设置!");}
}
function Receive()
{
//alert("InBufferCount::"+MSComm1.InBufferCount);
var inputvalue = MSComm1.Input;
if (inputvalue.indexOf('g') >= 0) {
return;
}
// alert(inputvalue);
document.getElementById("txtReceive").value = inputvalue.replace('-','');
//alert("InBufferCount::"+MSComm1.InBufferCount);
}
-->
</script>
</head>
<body>
<form name="form1">
<fieldset style="200px;height:250px;text-align:center;">
<legend>配置串口</legend>
<div style="float:left;200px">
<br/>
<span>串口号:</span>
<select name="ComName" id="ComName" style="75px" >
<option value="1"selected >COM1</option>
<option value="2" >COM2</option>
<option value="3">COM3</option>
<option value="4" >COM4</option>
</select>
<br/>
<span>波特率:</span>
<select name="BaudRate" id="BaudRate" style="75px" >
<option value="9600" selected >9600</option>
<option value="57600" >57600</option>
<option value="115200" >115200</option>
</select>
<br/>
<span>校验位:</span>
<select name="CheckBit" id="CheckBit" style="75px" >
<option value="N" selected >无NONE</option>
<option value="O" >奇ODD</option>
<option value="E" >偶EVEN</option>
</select>
<br/>
<span>数据位:</span>
<input type=text id="DataBits" name="DataBits" value=8 style="75px;height:20px">
<br/>
<span>停止位:</span>
<input type=text id="StopBits" name="StopBits" value=1 style="75px;height:20px">
<br/>
<br/>
<input type="button" id="ConfigButton" style="80px;height:30px;font-size:13px" name="ConfigButton" value="配置串口" onClick="ConfigPort()">
<input type="button" id="OperateButton" style="80px;height:30px;font-size:13px" name="OperateButton" value="打开串口" onClick="OperatePort()">
</div>
</fieldset>
<fieldset style="200px;height:250px;text-align:center;">
<legend>接收区域</legend>
<div style="float:left;">
<textarea id="txtReceive" READONLY=TRUE name="txtReceive" style="200px;height:160px"></textarea>
<br/>
</div>
</fieldset>
</form>
<OBJECT id=MSComm1 CLASSID="clsid:648A5600-2C6E-101B-82B6-000000000014"
codebase="MSCOMM32.OCX" type="application/x-oleobject" style="LEFT:54px;TOP:14px" >
<PARAM NAME="CommPort" VALUE="1"/>
<PARAM NAME="DataBits" VALUE="8"/>
<PARAM NAME="StopBits" VALUE="1"/>
<PARAM NAME="BaudRate" VALUE="9600"/>
<PARAM NAME="Settings" VALUE="9600,N,8,1"/>
<PARAM NAME="RTSEnable" VALUE="1"/>
<PARAM NAME="DTREnable" VALUE="1"/>
<PARAM NAME="Handshaking" VALUE="0"/>
<PARAM NAME="NullDiscard" VALUE="0"/>
<PARAM NAME="ParityReplace" VALUE="?"/>
<PARAM NAME="EOFEnable" VALUE="0"/>
<PARAM NAME="InputMode" VALUE="0"/>
<PARAM NAME="InBufferSize" VALUE="1024"/>
<PARAM NAME="InputLen" VALUE="0"/>
<PARAM NAME="OutBufferSize" VALUE="512"/>
<PARAM NAME="SThreshold" VALUE="0"/>
<PARAM NAME="RThreshold" VALUE="1"/>
</OBJECT>
</body>
</html>
<html>
<head>
<title>JavaScript串口测试</title>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
<SCRIPT ID=clientEventHandlersJS LANGUAGE=javascript>
<!--
function MSComm1_OnComm()
{
switch(MSComm1.CommEvent)
{
case 1:{ window.alert("Send OK!"); break;} //发送事件
case 2: { Receive();break;} //接收事件
default: alert("Event Raised!"+MSComm1.CommEvent);;
}
}
//-->
</SCRIPT>
<SCRIPT LANGUAGE=javascript FOR=MSComm1 EVENT=OnComm>
<!--
// MSComm1控件每遇到 OnComm 事件就调用 MSComm1_OnComm()函数
MSComm1_OnComm()
//-->
</SCRIPT>
<script language="JavaScript" type="text/JavaScript">
<!--
String.prototype.Blength = function(){
var arr = this.match(/[^\x00-\xff]/ig);
return arr == null ? this.length : this.length + arr.length;
}
function OperatePort()
{
if(MSComm1.PortOpen==true)
{
try{MSComm1.PortOpen=false;
document.getElementById("OperateButton").value="打开串口";
}catch(ex)
{alert(ex.message);}
}
else{
try{ MSComm1.PortOpen=true;
document.getElementById("OperateButton").value="关闭串口";
}catch(ex)
{alert(ex.message);}
}
}
function ConfigPort()
{
if(MSComm1.PortOpen==false)
{
try{
MSComm1.CommPort=document.getElementById("ComName").value;
MSComm1.Settings=document.getElementById("BaudRate").value.toString()+
","+document.getElementById("CheckBit").value.toString()+
","+document.getElementById("DataBits").value.toString()+
","+document.getElementById("StopBits").value.toString();
MSComm1.OutBufferCount =0; //清空发送缓冲区
MSComm1.InBufferCount = 0; //滑空接收缓冲区
alert("已配置串口COM"+MSComm1.CommPort+"\n 参数:"+MSComm1.Settings);
}catch(ex){alert(ex.message);}
}
else{ alert("请先关闭串口后再设置!");}
}
function Receive()
{
//alert("InBufferCount::"+MSComm1.InBufferCount);
var inputvalue = MSComm1.Input;
if (inputvalue.indexOf('g') >= 0) {
return;
}
// alert(inputvalue);
document.getElementById("txtReceive").value = inputvalue.replace('-','');
//alert("InBufferCount::"+MSComm1.InBufferCount);
}
-->
</script>
</head>
<body>
<form name="form1">
<fieldset style="200px;height:250px;text-align:center;">
<legend>配置串口</legend>
<div style="float:left;200px">
<br/>
<span>串口号:</span>
<select name="ComName" id="ComName" style="75px" >
<option value="1"selected >COM1</option>
<option value="2" >COM2</option>
<option value="3">COM3</option>
<option value="4" >COM4</option>
</select>
<br/>
<span>波特率:</span>
<select name="BaudRate" id="BaudRate" style="75px" >
<option value="9600" selected >9600</option>
<option value="57600" >57600</option>
<option value="115200" >115200</option>
</select>
<br/>
<span>校验位:</span>
<select name="CheckBit" id="CheckBit" style="75px" >
<option value="N" selected >无NONE</option>
<option value="O" >奇ODD</option>
<option value="E" >偶EVEN</option>
</select>
<br/>
<span>数据位:</span>
<input type=text id="DataBits" name="DataBits" value=8 style="75px;height:20px">
<br/>
<span>停止位:</span>
<input type=text id="StopBits" name="StopBits" value=1 style="75px;height:20px">
<br/>
<br/>
<input type="button" id="ConfigButton" style="80px;height:30px;font-size:13px" name="ConfigButton" value="配置串口" onClick="ConfigPort()">
<input type="button" id="OperateButton" style="80px;height:30px;font-size:13px" name="OperateButton" value="打开串口" onClick="OperatePort()">
</div>
</fieldset>
<fieldset style="200px;height:250px;text-align:center;">
<legend>接收区域</legend>
<div style="float:left;">
<textarea id="txtReceive" READONLY=TRUE name="txtReceive" style="200px;height:160px"></textarea>
<br/>
</div>
</fieldset>
</form>
<OBJECT id=MSComm1 CLASSID="clsid:648A5600-2C6E-101B-82B6-000000000014"
codebase="MSCOMM32.OCX" type="application/x-oleobject" style="LEFT:54px;TOP:14px" >
<PARAM NAME="CommPort" VALUE="1"/>
<PARAM NAME="DataBits" VALUE="8"/>
<PARAM NAME="StopBits" VALUE="1"/>
<PARAM NAME="BaudRate" VALUE="9600"/>
<PARAM NAME="Settings" VALUE="9600,N,8,1"/>
<PARAM NAME="RTSEnable" VALUE="1"/>
<PARAM NAME="DTREnable" VALUE="1"/>
<PARAM NAME="Handshaking" VALUE="0"/>
<PARAM NAME="NullDiscard" VALUE="0"/>
<PARAM NAME="ParityReplace" VALUE="?"/>
<PARAM NAME="EOFEnable" VALUE="0"/>
<PARAM NAME="InputMode" VALUE="0"/>
<PARAM NAME="InBufferSize" VALUE="1024"/>
<PARAM NAME="InputLen" VALUE="0"/>
<PARAM NAME="OutBufferSize" VALUE="512"/>
<PARAM NAME="SThreshold" VALUE="0"/>
<PARAM NAME="RThreshold" VALUE="1"/>
</OBJECT>
</body>
</html>
电子称连接上电脑,设置允许加载ACtiveX什么的不多说了。此时正常情况是可以读到数据的,但总有那么多不正常
的情况,本人就被浪费了一个下午和一个晚上,但总算解决。开始的时候在Window2003下测试通过了。到WindowXP
下,怎么都不行。实在郁闷。还好网上有人给出问题的原因:XP的打补丁导致不去加载MSComm32.ocx。去掉的方法
将下面语句写入到.reg文件中,运行即可。
Windows Registry Editor Version 5.00
[-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\ActiveX Compatibility\{648A5600-2C6E-101B-82B6-000000000014}]