日期:2014-05-16 浏览次数:20335 次
//com.js /** * 初始化MSComm1对象,并打开串口 */ function initMSComm(){ document.write("<OBJECT id=MSComm1 CLASSID=\"clsid:648A5600-2C6E-101B-82B6-000000000014\" codebase=\"MSCOMM32.OCX\" type=\"application/x-oleobject\" style=\"width:100px;height:30px\" >"); document.write("<PARAM NAME=\"CommPort\" VALUE=\"1\"/> "); document.write("<PARAM NAME=\"DataBits\" VALUE=\"8\"/> "); document.write("<PARAM NAME=\"StopBits\" VALUE=\"1\"/>"); document.write("<PARAM NAME=\"BaudRate\" VALUE=\"9600\"/>"); document.write("<PARAM NAME=\"Settings\" VALUE=\"9600,N,8,1\"/>"); document.write("<PARAM NAME=\"RTSEnable\" VALUE=\"1\"/>"); document.write("<PARAM NAME=\"DTREnable\" VALUE=\"1\"/>"); document.write("<PARAM NAME=\"Handshaking\" VALUE=\"0\"/>"); document.write("<PARAM NAME=\"NullDiscard\" VALUE=\"0\"/> "); document.write("<PARAM NAME=\"ParityReplace\" VALUE=\"?\"/>"); document.write("<PARAM NAME=\"EOFEnable\" VALUE=\"0\"/>"); document.write("<PARAM NAME=\"InputMode\" VALUE=\"0\"/>"); document.write("<PARAM NAME=\"InBufferSize\" VALUE=\"1024\"/>"); document.write("<PARAM NAME=\"InputLen\" VALUE=\"1\"/>"); document.write("<PARAM NAME=\"OutBufferSize\" VALUE=\"512\"/>"); document.write("<PARAM NAME=\"SThreshold\" VALUE=\"0\"/>"); document.write("<PARAM NAME=\"RThreshold\" VALUE=\"1\"/>"); document.write("</OBJECT>"); if(MSComm1.PortOpen==false){ try{ MSComm1.PortOpen=true; }catch(ex){ alert("com1端口打开失败:"+ex.message); } } } var mscomm_scan_value = ""; // 串口响应事件 function MSComm1_OnComm(callBackFunction){ switch(MSComm1.CommEvent){ case 2: //接收事件 if(mscomm_scan_value == "") { mscomm_scan_value = MSComm1.Input; // 定时读取缓存信息 setTimeout("getMSCommScanValue("+callBackFunction+")",300); } break; default: alert("Event Raised!"+MSComm1.CommEvent); } } function getMSCommScanValue(callBackFunction){ // 读取缓存中剩余的信息 while(MSComm1.InBufferSize > 0) { mscomm_scan_value += MSComm1.Input; } // 执行页面传递过来的函数 callBackFunction(mscomm_scan_value); // 清空当前读取信息 mscomm_scan_value = "" }
<html> <head> <title>JavaScript串口测试</title> <meta http-equiv="Content-Type" content="text/html; charset=GB2312" /> <script type="text/javascript" src="com.js" > </script> <script type="text/ecmascript" for="MSComm1" event="OnComm"> // MSComm1控件每遇到 OnComm 事件就调用 MSComm1_OnComm()函数 MSComm1_OnComm(mscommCallBack) </script> <script type="text/javascript"> function mscommCallBack(scanValue) { var ewtm = document.getElementById("ewtm"); ewtm.value = scanValue; } </script> </head> <body> <script type="text/javascript" > initMSComm(); </script> 二维条码:<input type="text" id="ewtm"/> </body> </html>