鐵路車號讀取,鐵路車號
前文已經介紹了《使用IE測試COM》,那麼我們用一個實際的執行個體來使用他。
在這之前,我們需要瞭解2個Javascript函數:
- replace(regexp/substr,replacement)
使用這個函數來替換多餘的空格。
str.replace(/\s/g, "");
- split(separator,howmany)
字串轉換成字元數組。
string.split(",");
一、js部分
我們把所有的方法放到一個對象(RepoInfo)中。
var RepoInfo = { };
1. 擷取對象
RepoInfo.getObj = function(objName) {return new ActiveXObject(objName);};
2. 擷取數組
RepoInfo.getArray = function(str) {var ret = new Array(16);var string = str.replace(/\s/g, "");ret = string.split(",");return ret;};
3. 解碼字串 (1)輸入字串校正 如果字串空,退出。 (2)輸出框清空 每次賦值前,清空。 (3)執行COM中的方法 在擷取對象非空的情況下,執行。
RepoInfo.decode = function(string) {if (string == null) return;var obj = RepoInfo.getObj("repoInfo.LabelInfo");var edtView = document.getElementById("edtView");edtView.value = " ";var str = RepoInfo.getArray(string);if (obj != null)edtView.value = obj.getInfo(str);};
二、html部分
1. 按鈕事件
從輸入框擷取字串作為js方法的輸入參數,注意:兩種引號的配合使用。
<input type="button" id="btnOk" value="執行資料解析" onClick="RepoInfo.decode(document.getElementById('edtData').value)" />
2. 頁面效果
標籤資料:
執行結果:
三、原始碼(含js代碼)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>報文資料資訊 - IE</title><script type="text/javascript">var RepoInfo = {};RepoInfo.getObj = function(objName) {return new ActiveXObject(objName);};RepoInfo.getArray = function(str) {var ret = new Array(16);var string = str.replace(/\s/g, "");alert(string);ret = string.split(",");return ret;};RepoInfo.decode = function(string) {if (string == null) return;var obj = RepoInfo.getObj("repoInfo.LabelInfo");var edtView = document.getElementById("edtView");edtView.value = " ";var str = RepoInfo.getArray(string);if (obj != null)edtView.value = obj.getInfo(str);};</script></head><body> <textarea name="note" cols="98" rows="7" readonly="readonly"> 注意:本測試只能運行在IE瀏覽器! 首先,要註冊Dll(regsvr32 repoInfo.dll); 然後,從“BinJiang_2005.rep”(濱江站),複製行資料到“標籤資料”框。 樣本(固定格式): 0xD3, 0x05, 0x94, 0x84, 0x00, 0x13, 0x51, 0x2F, 0x59, 0x34, 0x57, 0x45, 0x58, 0x50, 0x41, 0x98</textarea><br><br>標籤資料:<input type="text" id="edtData" size="98" /><br><br>執行結果:<input type="text" id="edtView" size="50" /><br><br> <input type="button" id="btnOk" value="執行資料解析" onClick="RepoInfo.decode(document.getElementById('edtData').value)" /></body></html>
編後話:
國內只有兩家正規的鐵路車號開發機構(所/企業):遠望穀和威克公司。
參考文檔(W3School):
1. replace 方法
2. RegExp 對象
3. split 方法