JavaScript 扫描枪应用(二)

针对扫描前的前一种实现略有所调整,感觉更接近实现的目标:
JavaScript 代码如下:
// JavaScript Document

function createMSComm(div, callBackFunction) {	
	var mscomm_str = "<OBJECT id=MSComm1 CLASSID='clsid:648A5600-2C6E-101B-82B6-000000000014' codebase='MSCOMM32.OCX' type='application/x-oleobject'  style='width:100px;height:30px' >"	
		+"<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='1'/>"	
		+"<PARAM NAME='OutBufferSize'	VALUE='512'/>"	
		+"<PARAM NAME='SThreshold'		VALUE='0'/>"	
		+"<PARAM NAME='RThreshold'		VALUE='1'/>"	
		+"</OBJECT>";
	
	// 将OBJECT标签添加到层中
	div.innerHTML = mscomm_str;
	
	// 打开串口
	if(MSComm1.PortOpen==false){
		try{
			MSComm1.PortOpen=true;
		}catch(ex){
			alert("com1端口打开失败:"+ex.message);
		}
	}
	// 串口接收内容
	var mscomm_scan_value = "";
	
	// 串口响应事件
	mscomm.attachEvent("OnComm",function() {
		switch(this.CommEvent){
			case 2:  //接收事件
				if(mscomm_scan_value == "") {
					mscomm_scan_value = this.Input;
					// 定时读取缓存信息
					setTimeout("getMSCommScanValue()",300);
				}
				break;
			default: alert("Event Raised!"+MSComm1.CommEvent);
		}
	});
	
	// 读取缓存中剩余信息
	function getMSCommScanValue(){
		// 读取缓存中剩余的信息
		while(MSComm1.inBufferCount > 0) {
			mscomm_scan_value += MSComm1.Input;
		}
		// 执行页面传递过来的函数
		callBackFunction(mscomm_scan_value);
		// 清空当前读取信息
		mscomm_scan_value = ""
	}
}


html 代码如下:
<html>
<head>
<title>JavaScript串口测试</title>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
<script type="text/javascript" src="mscomm.js" >
</script>

<script type="text/javascript">
	var i = 0;
	function mscommCallBack(scanValue) {
		var ewtm = document.getElementById("ewtm");
		ewtm.innerHTML += "第" + (++i) + "次扫描:" + scanValue;
	}
	
	window.onload = function() {
		var mscomm_div = document.getElementById("mscomm_div");
		createMSComm(mscomm_div, mscommCallBack);
	}
</script>
</head>

<body>

二维条码:
<div id="ewtm"></div>
<div id="mscomm_div"></div>
</body>
</html>



    通过大家的提醒,这是通过ActiveX实现串口的操作,所以按照这个想法又进行改进,通过动态创建标签的方式将OBJECT标签添加到body中,但总是感觉插件没有被加载,而且在打开端口时,无法识别PortOpen属性。
    希望大家能够提供更多的想法和宝贵意见,非常感谢。

你可能感兴趣的:(JavaScript,html)