JavaScript 掃描槍使用(一)

來源:互聯網
上載者:User

標籤:put   方式   nload   eve   nbsp   檔案中   相關   var   閉包   

JavaScript 掃描槍應用(一)
com.js為主要的代碼實現,test.html檔案為測試的頁面,其中包括了com.js檔案中方法的調用。以下為測試成功碼:
com.js

//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 = ""}



test.html

<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>



得出以上結果經過幾次失敗嘗試,嘗試內容如下:
1. 將test.html檔案中的
   <script   type="text/javascript"   for="MSComm1"  event="OnComm">
// MSComm1控制項每遇到 OnComm 事件就調用 MSComm1_OnComm()函數
MSComm1_OnComm(mscommCallBack)
   </script>段代碼調整為:
   <script type="text/javascript">
        window.onload = function() {
             MSComm1.OnComm = MSComm1_OnComm;//採用其他方式取代閉包的實現過程
        }
   </script>

2. 在頁面head的JavaScript代碼中調用initMSComm方法,將Object標籤對象添加到頁面的一個div中,頁面提示載入ActiveX控制項,但是無法綁定OnComm事件響應。

3. 當MSComm1.CommEvent為7時表示接收結束,可是只能接收到狀態為2的情況,所以無法採用狀態為7時的響應事件。

4. 當initMSComm方法調用時,且MSComm1.CommEvent為2的情況下,判斷MSComm1.inBufferCount為0時進行事件響應,可能是緩衝等多種原因(具體不太清楚),會導致inBufferCount出現多吃0的情況,所以沒有辦法使用,最終採用首次執行,定時擷取的方式完成緩衝內容的讀取操作。

    我的目的是通過一次方法調用,能夠實現對象的載入,響應事件的綁定,以及接收結束執行響應事件操作。由於我對JavaScript不夠熟練,所以沒能夠達到預期的目的,如果有更好的解決辦法,希望大家能夠給我留言。

相關檔案下載:http://www.posunitech.cn/download_list/columnsId=60&FrontColumns_navigation01-1362972848243FirstColumnId=36&FrontColumns_navigation01-1362972848243SecondColumnId=60&&downloadcategoryid=1&isMode=false.html

JavaScript 掃描槍使用(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.