本示例使用的读卡器:https://item.taobao.com/item.htm?spm=a1z10.5-c-s.w4002-21818769070.35.74185b43tGWQH5&id=562957272162


<!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>EM_TK4100_ID读卡器DEMO</title>

    <script language="javascript">
		
        var iswsrun = false;
        var ws;
        var received_msg = "";
        var mytimer;
        var strls = "";
        var errorno = "";
        var readcarnow = false;
		var time1;

        var wsUri = "ws://127.0.0.1:39189";   //端口号必须与RFIDWebServer端口一致		

        function ButtonDisable() {  //删除按键的 onclick 事件,防止重复执行指令             
            document.getElementById("butt_beep").setAttribute("onclick", null);
            document.getElementById("butt_getdevnum").setAttribute("onclick", null);
            document.getElementById("butt_idr_read").setAttribute("onclick", null);
            document.getElementById("butt_idr_readone").setAttribute("onclick", null);
            document.getElementById("butt_loopread").setAttribute("onclick", null);
        }

        function beep() {				     //驱动发卡器响声令
            ButtonDisable();                 //删除按键的 onclick 事件,防止重复执行指令

            textarea.value = "";		
            WebSocketRun("idr_beep,30");	 //30为响声时长豪秒,取值范围1-65535
        }
		
        function getdevicenumber() {         //读取发卡器唯一出厂序号,可以当加密狗使用			
            ButtonDisable();                 //删除按键的 onclick 事件,防止重复执行指令

            textarea.value = "";		
			serialnumber.value="";
            WebSocketRun("IDpcdgetdevicenumber");			
        }		
		
        function ReadID() {					 //卡片在感应区内可以连续读取卡号
            ButtonDisable();                 //删除按键的 onclick 事件,防止重复执行指令

            textarea.value = "";
			carduid.value="";			
			Vendorcode.value="";
			card8h10dz.value="";
			card8h10df.value="";
			cardwg26z.value="";
			cardwg34z.value="";
            WebSocketRun("idr_read,");
        }			
		
        function ReadIDOne() {				 //卡片进入感应区内只可读取一次卡号,再次读取需将卡片移开再次放入感应区
            ButtonDisable();                 //删除按键的 onclick 事件,防止重复执行指令

            textarea.value = "";
			carduid.value="";			
			Vendorcode.value="";
			card8h10dz.value="";
			card8h10df.value="";
			cardwg26z.value="";
			cardwg34z.value="";
            WebSocketRun("idr_read_once,");
        }
		
        function read_loop() {
            if (!readcarnow) {
                ButtonDisable();
                time1 = setInterval("ReadID()", 1000);
                readcarnow = true;
            }
		}

        function read_stop() {
            if (readcarnow = true) {
                clearInterval(time1);
                readcarnow = false;
                ButtonEnabled()
            }
		}
		
        function ButtonEnabled() {  //恢复各button 的onclick事件            
            document.getElementById("butt_beep").setAttribute("onclick", "beep()");
            document.getElementById("butt_getdevnum").setAttribute("onclick", "getdevicenumber()");
            document.getElementById("butt_idr_read").setAttribute("onclick", "ReadID()");
            document.getElementById("butt_idr_readone").setAttribute("onclick", "ReadIDOne()");
            document.getElementById("butt_loopread").setAttribute("onclick", "read_loop()");
        }
		
		function DispErrInfo(errcode){
            var errstr = "";
            switch (errcode) {
                case "ReturnCode:008":
                    errstr = ",未寻到卡,请重新将卡放到发卡器的感应区!";
                    break;
                case "ReturnCode:021":
                    errstr = ",没有动态库!";
                    break;
                case "ReturnCode:022":
                    errstr = ",动态库或驱动程序异常!";
                    break;
                case "ReturnCode:023":
                    errstr = ",驱动程序错误或发卡器未连接!";
                    break;
                case "ReturnCode:024":
                    errstr = ",操作超时,一般是动态库没有反映!";
                    break;
                case "ReturnCode:025":
                    errstr = ",发送字数不够!";
                    break;
                case "ReturnCode:026":
                    errstr = ",发送的CRC错!";
                    break;
                case "ReturnCode:027":
                    errstr = ",接收的字数不够!";
                    break;
                case "ReturnCode:028":
                    errstr = ",接收的CRC错!";
                    break;
                case "ReturnCode:029":
                    errstr = ",函数输入参数格式错误!";
                    break;
                case "ReturnCode:444":
                    errstr = ",RFIDWebServer系统文件错误!";
                    break;
                default:
                    errstr = ",未知的错误!";
            }
            return errstr;
		}			
		
		
		window.onerror = function (e) {
			clearInterval(time1);
		    ButtonEnabled();  //恢复按键的onclick事件
            alert("不好意思,出错了!");
            return true;//屏蔽系统事件
        }

        function timeoutevent() {
            ws.close();
            textarea.value = "websockket返回超时";
			clearInterval(time1);
            ButtonEnabled();  //恢复按键的onclick事件
        }

        //websockket数据处理
        function WebSocketRev(evt) {
            clearTimeout(mytimer);
            received_msg = evt.data;
            ws.close();

            //在这里解析返回数据
            var strlist = received_msg.split(",");
            if (strlist.length > 1) {
                var dispstr = "";
                switch (strlist[0]) {
                    case "idr_beep":
                        dispstr = "函数名称:" + strlist[0] + "\r\n";
                        if (strlist[1] == "ReturnCode:000") {
                            dispstr = dispstr + "操作结果:" + strlist[1];
                            dispstr = dispstr + ",已执行蜂鸣响声操作!\r\n"
                        } else {
                            dispstr = dispstr + "操作结果:" + strlist[1];
                            dispstr = dispstr + DispErrInfo(strlist[1]);
                        }
                        textarea.value = dispstr;
                        break;

                    case "pcdgetdevicenumber":
                        dispstr = "函数名称:" + strlist[0] + "\r\n";
                        if (strlist[1] == "ReturnCode:000") {
                            dispstr = dispstr + "操作结果:" + strlist[1];
                            dispstr = dispstr + ",已读取设备编号!\r\n"
							serialnumber.value =  strlist[2] ;
                        } else {
                            dispstr = dispstr + "操作结果:" + strlist[1];
                            dispstr = dispstr + DispErrInfo(strlist[1]);
                        }
                        textarea.value = dispstr;
                        break;

                    case "idr_read":
                    case "idr_read_once":
                        dispstr = "函数名称:" + strlist[0] + "\r\n";
                        if (strlist[1] == "ReturnCode:000") {
                            dispstr = dispstr + "操作结果:" + strlist[1];
                            dispstr = dispstr + ",已读取卡号!\r\n"
							
                        } else {
                            dispstr = dispstr + "操作结果:" + strlist[1];
                            dispstr = dispstr + DispErrInfo(strlist[1]);
                        }
                        if (strlist.length > 2) {
                            carduid.value=strlist[2] ;
							Vendorcode.value=strlist[2].substring(0,2);
							
							HLCode=strlist[2].substring(2,4)+strlist[2].substring(4,6)+strlist[2].substring(6,8)+strlist[2].substring(8,10);
							card8h10dz.value=parseInt("0x"+HLCode).toString().padStart(10, '0');
							
							LHCode=strlist[2].substring(8,10)+strlist[2].substring(6,8)+strlist[2].substring(4,6)+strlist[2].substring(2,4);
							card8h10df.value=parseInt("0x"+LHCode).toString().padStart(10, '0');
							
							cardwg26z.value=parseInt("0x"+strlist[2].substring(4,6)).toString().padStart(3, '0')+parseInt("0x"+strlist[2].substring(6,10)).toString().padStart(5, '0');
							cardwg34z.value=parseInt("0x"+strlist[2].substring(2,6)).toString().padStart(5, '0')+parseInt("0x"+strlist[2].substring(6,10)).toString().padStart(5, '0');							
                        }
                        textarea.value = dispstr;
                        break;						
																									
                }
            }
            ButtonEnabled();  //恢复按键的onclick事件
        }		
		
        function WebSocketRun(sendinfo) {
            iswsrun = false;

            try {
                if ("WebSocket" in window) {
                    ws = new WebSocket(wsUri);
                }
                else if ("MozWebSocket" in window) {
                    ws = new MozWebSocket(wsUri);
                }
                else {
                    received_msg = "您的浏览器不支持WebSocket,请选用支持WebSocket的浏览器!";
                    ButtonEnabled();  //恢复按键的onclick事件
                    return;
                }

                clearTimeout(mytimer);

                ws.onopen = function (evt) {
                    ws.send(sendinfo);
                    iswsrun = true;

                    mytimer = setTimeout("timeoutevent()", 2000);
                };

                ws.onmessage = WebSocketRev;

                ws.onerror = function (e) {
                    if (iswsrun != true) {
                        received_msg = "请先在当前电脑下载>安装>运行我们的服务程序......";
                        window.open("http://www.bossknow.cn:90/RongShiWebReaderTest/RFIDWebServerSetup_Windows.rar", "top");//打开新窗口
						clearInterval(time1);
                        ButtonEnabled();  //恢复按键的onclick事件
                        alert("请先下载>安装>运行我们的服务程序,再刷新本页面......");
                    }
                };
            }
            catch (ex) {
                if (iswsrun != true) {
                    received_msg = "请先在当前电脑下载>安装>运行我们的服务程序......";
                    window.open("http://www.bossknow.cn:90/RongShiWebReaderTest/RFIDWebServerSetup_Windows.rar", "top");//打开新窗口
					clearInterval(time1);
                    ButtonEnabled();  //恢复按键的onclick事件
                    alert("请先下载>安装>运行我们的服务程序,再刷新本页面......");
                }
            }
        }
				
	</script>
    
	<style>
		th {
		  background-color:#F6FAFF;	
		  color: blue;
		  font-family:楷体;
		}
		td {
		  background-color:#F6FAFF;		
		  font-family:楷体;
		}  
    </style>        
    
</head>

<body>
<table width="866" height="295"  align="center">
  <tr>
    <th width="124" height="47" scope="row"><input style="width:120px" name="butt_beep" type="submit" id="butt_beep" onclick="beep()" value="驱动发卡器响声" /></th>
    <td width="716"><input name="butt_getdevnum" type="submit" id="butt_getdevnum" onclick="getdevicenumber()" value="读取发卡器唯一出厂序列号" />
      设备编号:
        <input style="color:red;text-align:center;" name="serialnumber" type="text" id="serialnumber" size="8" maxlength="8" /></td>
  </tr>
  
  <tr>
    <th height="76" scope="row"><p>
      <input  style="width:120px" name="butt_idr_read" type="submit" id="butt_idr_read" onclick="ReadID()" value="读取ID卡号" />
    </p>
      <p>
        
        <input style="width:120px" name="button" type="submit" id="butt_idr_readone" onclick="ReadIDOne()" value="只读一次ID卡号" />
      </p>
    <p>
      <input name="butt_loopread" type="submit" id="butt_loopread" style="width:120px" onclick="read_loop()" value="轮询读取卡号" />
    </p>
    <p>
      <input name="butt_stop" type="submit" id="butt_stop" style="width:120px" onclick="read_stop()" value="停止轮询读卡" />
  </p></th>
    <td><p>原始16进制卡号:
        <input style="color:red;text-align:center;" name="carduid" type="text" id="carduid" size="10" maxlength="10" />
      ,厂商代码:
      <input style="color:red;text-align:center;" name="Vendorcode" type="text" id="Vendorcode" size="2" maxlength="2" />
    </p>
      <p>转8H10D正码:
        <input style="color:red;text-align:center;" name="card8h10dz" type="text" id="card8h10dz" size="10" maxlength="10" />
    ,转8H10D反码:
    <input style="color:red;text-align:center;" name="card8h10df" type="text" id="card8h10df" size="10" maxlength="10" />
    </p>
    <p>转WG26正码:
      <input style="color:red;text-align:center;" name="cardwg26z" type="text" id="cardwg26z" size="10" maxlength="10" />
,转WG34正码:
<input style="color:red;text-align:center;" name="cardwg34z" type="text" id="cardwg34z" size="10" maxlength="10" />
    </p></td>
  </tr>
  
  <tr>
    <th height="78" scope="row"><p>操作提示</p></th>
    <td><textarea style="color:blue;" name="textarea" id="textarea" cols="100" rows="5" ></textarea></td>
  </tr>
</table>
</body>
</html>

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐