javascript读取RFID卡号源码

发布时间:2023年12月20日

本示例使用的读卡器:Web浏览器JS网页可编程二次开发RFID读卡器Android Linux乐乐课堂-淘宝网 (taobao.com)?

<!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 wsUri = "ws://127.0.0.1:39189";   //端口号必须与RFIDWebServer端口一致						

        function beep() {				     //驱动发卡器响声令
            textarea.value = "";		
            WebSocketRun("idr_beep,30");	 //30为响声时长豪秒,取值范围1-65535
        }
		
        function getdevicenumber() {         //读取发卡器唯一出厂序号,可以当加密狗使用			
            textarea.value = "";		
			serialnumber.value="";
            WebSocketRun("IDpcdgetdevicenumber");			
        }		
		
        function ReadID() {					//卡片在感应区内可以连续读取卡号
            textarea.value = "";
			carduid.value="";			
			Vendorcode.value="";
			card8h10dz.value="";
			card8h10df.value="";
			cardwg26z.value="";
			cardwg34z.value="";
            WebSocketRun("idr_read,");
        }			
		
        function ReadIDOne() {				//卡片进入感应区内只可读取一次卡号,再次读取需将卡片移开再次放入感应区
            textarea.value = "";
			carduid.value="";			
			Vendorcode.value="";
			card8h10dz.value="";
			card8h10df.value="";
			cardwg26z.value="";
			cardwg34z.value="";
            WebSocketRun("idr_read_once,");
        }						
		
		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) {
            alert("不好意思,出错了!");
            return true;//屏蔽系统事件
        }

        function timeoutevent() {
            ws.close();
            textarea.value = "websockket返回超时";
        }

        //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;						
																									
                }
            }
        }		
		
        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的浏览器!";
                    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.icmcu.com/ICID15693-02V2test/RFIDWebServerSetup.exe", "top");//打开新窗口

                        alert("请先下载>安装>运行我们的服务程序,再刷新本页面......");
                    }
                };
            }
            catch (ex) {
                if (iswsrun != true) {
                    received_msg = "请先在当前电脑下载>安装>运行我们的服务程序......";
                    window.open("http://www.icmcu.com/ICID15693-02V2test/RFIDWebServerSetup.exe", "top");//打开新窗口

                    alert("请先下载>安装>运行我们的服务程序,再刷新本页面......");
                }
            }
        }
				
	</script>
</head>

<body>
<table width="866" height="224" border="1" align="center">
  <tr>
    <th width="124" height="47" scope="row"><input 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 name="serialnumber" type="text" id="serialnumber" size="8" maxlength="8" /></td>
  </tr>
  
  <tr>
    <th height="76" scope="row"><p>
      <input name="butt_idr_read" type="submit" id="butt_idr_read" onclick="ReadID()" value="连续读取ID卡号" />
    </p>
      <p>&nbsp; </p>
    <p>
      <input name="button" type="submit" id="button" onclick="ReadIDOne()" value="只读一次ID卡号" />
    </p></th>
    <td><p>原始16进制卡号:
      <input name="carduid" type="text" id="carduid" size="10" maxlength="10" />
      ,厂商代码:
      <input name="Vendorcode" type="text" id="Vendorcode" size="2" maxlength="2" />
    </p>
      <p>转8H10D正码:
  <input name="card8h10dz" type="text" id="card8h10dz" size="10" maxlength="10" />
    ,转8H10D反码:
    <input name="card8h10df" type="text" id="card8h10df" size="10" maxlength="10" />
    </p>
    <p>转WG26正码:
      <input name="cardwg26z" type="text" id="cardwg26z" size="10" maxlength="10" />
,转WG34正码:
<input name="cardwg34z" type="text" id="cardwg34z" size="10" maxlength="10" />
    </p></td>
  </tr>
  
  <tr>
    <th height="78" scope="row"><p>操作提示</p></th>
    <td><textarea name="textarea" id="textarea" cols="100" rows="5" ></textarea></td>
  </tr>
</table>
</body>
</html>

?

文章来源:https://blog.csdn.net/zhangjin7422/article/details/135113794
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。