<html>
<head>
    <meta http-equiv="Content-Language" content="zh-cn">
    <meta name="GENERATOR" content="Microsoft FrontPage 6.0">
    <meta name="ProgId" content="FrontPage.Editor.Document">
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>USB IC 卡读卡器控件调用例程 V1.0</title>

<script language="javascript">
var strls = "";
var errorno = "";

//控制字定义,控制字指定,控制字的含义请查看本公司网站提供的动态库说明

//javascript无法自定义常量, 你可以使用变量代替

var BLOCK0_EN = 0x01;//读第一块的(16个字节)
var BLOCK1_EN = 0x02;//读第二块的(16个字节)
var BLOCK2_EN = 0x04;//读第四块的(16个字节)
var NEEDSERIAL = 0x08;//仅读指定序列号的卡
var EXTERNKEY = 0x10;//用明码认证密码,产品开发完成后，建议把密码放到设备的只写区，然后用该区的密码后台认证，这样谁都不知道密码是多少，需要这方面支持请联系
var NEEDHALT = 0x20;//读/写完卡后立即休眠该卡，相当于这张卡不在感应区。要相重新操作该卡必要拿开卡再放上去

var myctrlword = 0;
var myareano = 0;
var authmode = 0;


var mypiccserial = "";

var mypicckey = ""; 
var piccdata0_2 = ""; 

var mypicckey_old = "";//旧密码
var mypicckey_new = "";//新密码


function readcard()
{

var BLOCK0_EN = 0x01;//读第一块的(16个字节)
var BLOCK1_EN = 0x02;//读第二块的(16个字节)
var BLOCK2_EN = 0x04;//读第四块的(16个字节)
var NEEDSERIAL = 0x08;//仅读指定序列号的卡
var EXTERNKEY = 0x10;//用明码认证密码,产品开发完成后，建议把密码放到设备的只写区，然后用该区的密码后台认证，这样谁都不知道密码是多少，需要这方面支持请联系
var NEEDHALT = 0x20;//读/写完卡后立即休眠该卡，相当于这张卡不在感应区。要相重新操作该卡必要拿开卡再放上去

var myctrlword = 0;
var myareano = 0;
var authmode = 0;


var mypiccserial = "";

var mypicckey = ""; 
var piccdata0_2 = ""; 

var mypicckey_old = "";//旧密码
var mypicckey_new = "";//新密码
	//指定控制字
	myctrlword=BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY;
	//指定区号
	myareano = 8; //指定为第8区
	//批定密码模式
	authmode = 1; //大于0表示用A密码认证，推荐用A密码认证
	
	//指定序列号，未知卡序列号时可指定为8个0
	mypiccserial="00000000";

	//指定密码，以下密码为厂家出厂密码
	mypicckey = "ffffffffffff";
	
	strls=IcCardReader.piccreadex(myctrlword, mypiccserial,myareano,authmode,mypicckey);
	errorno = strls.substr(0,4);

	
	switch(errorno)
	{
		case "ER08":
			alert("寻不到卡");
			break;	
		case "ER09":
			alert("寻不到卡");
			break;	
		case "ER10":
			alert("寻不到卡");
			break;	

		case "ER11":
			CardIDShower.value = "密码认证错误\r\n";
			CardIDShower.value = CardIDShower.value + strls + "\r\n";
			CardIDShower.value = CardIDShower.value + "其中错误号为：" + errorno + "\r\n";
			CardIDShower.value = CardIDShower.value + "卡十六进制序列号为：" + strls.substr(5,8) + "\r\n";
			alert("密码认证错误");
			break;	
		case "ER12":
			CardIDShower.value = "密码认证错误" + "\r\n";
			CardIDShower.value = CardIDShower.value + strls + "\r\n";
			CardIDShower.value = CardIDShower.value + "其中错误号为：" + errorno + "\r\n";
			CardIDShower.value = CardIDShower.value + "卡十六进制序列号为：" + strls.substr(5,8) + "\r\n";
			alert("密码认证错误");
			break;
		case "ER13":
			CardIDShower.value = "读卡错误" + "\r\n";
			CardIDShower.value = CardIDShower.value + strls + "\r\n";
			CardIDShower.value = CardIDShower.value + "其中错误号为：" + errorno + "\r\n";
			CardIDShower.value = CardIDShower.value + "卡十六进制序列号为：" + strls.substr(5,8) + "\r\n";
			alert("读卡错误");
			break;	
			
		case "ER14":
			CardIDShower.value = "写卡错误" + "\r\n";
			CardIDShower.value = CardIDShower.value + strls + "\r\n";
			CardIDShower.value = CardIDShower.value + "其中错误号为：" + errorno + "\r\n";
			CardIDShower.value = CardIDShower.value + "卡十六进制序列号为：" + strls.substr(5,8) + "\r\n";
			alert("写卡错误");	
			break;
			
		case "ER21":
			alert("没找到动态库");	
			break;
			
		case "ER22":
			alert("动态库或驱动程序异常");
			break;	
		
		case "ER23":
			alert("读卡器未插上或动态库或驱动程序异常");
			break;	
		case "ER24":
			alert("操作超时，一般是动态库没有反应");
			break;	
		case "ER25":
			alert("发送字数不够");	
			break;
		case "ER26":
			alert("发送的CRC错");
			break;	
		case "ER27":
			alert("接收的字数不够");
			break;	
		case "ER28":
			alert("接收的CRC错");
			break;
		case "ER29":
			alert("函数输入参数格式错误,请仔细查看"	);
			break;
		default :
			//读卡成功,其中ER00表示完全成功,ER01表示完全没读到卡数据，ER02表示仅读该卡的第一块成功,，ER02表示仅读该卡的第一二块成功，这是刷卡太快原因
			CardIDShower.value = "读卡成功" + "\r\n";
			CardIDShower.value = CardIDShower.value + strls + "\r\n";
			CardIDShower.value = CardIDShower.value + "其中错误号为：" + errorno + "\r\n";
			CardIDShower.value = CardIDShower.value + "卡十六进制序列号为：" + strls.substr(5,8) + "\r\n";
			var e=new RegExp("F","g"); 
			CardIDShower.value = CardIDShower.value + "卡号：" + strls.substr(14,32).replace(e,"") + "\r\n";
			CardIDShower.value = CardIDShower.value + "密码：" + strls.substr(46,32).replace(e,"") + "\r\n";
			break;
	
		

	}



	
}

function writecard()
{

	//指定控制字
	myctrlword=BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY;
	//指定区号
	myareano = 8; //指定为第8区
	//批定密码模式
	authmode = 1; //大于0表示用A密码认证，推荐用A密码认证
	
	//指定序列号，未知卡序列号时可指定为8个0
	mypiccserial="00000000";

	//指定密码，以下密码为厂家出厂密码
	mypicckey = "ffffffffffff";


	
	//指定写卡内容，长度为48个字节，其中每个字节以两个字符表示为十六进制数
	var cardNoValue = cardNo.value;
	while(cardNoValue.length < 32){
		cardNoValue = cardNoValue + 'F';
	}
	var passwordValue = password.value;
	while(passwordValue.length < 32){
		passwordValue = passwordValue + 'F';
	}
	piccdata0_2 = cardNoValue+passwordValue+'00000000000000000000000000000000';
	CardIDShower.value =CardIDShower.value+"写卡数据：" +piccdata0_2+ "\r\n";
	strls=IcCardReader.piccwriteex(myctrlword, mypiccserial,myareano,authmode,mypicckey,piccdata0_2);
	errorno = strls.substr(0,4);
	switch(errorno)
	{
		case "ER08":
			alert("寻不到卡");
			break;
		case "ER09":
			alert("寻不到卡");
			break;
		case "ER10":
			alert("寻不到卡");
			break;
		case "ER11":
			CardIDShower.value = "密码认证错误" + "\r\n";
			CardIDShower.value = CardIDShower.value + strls + "\r\n";
			CardIDShower.value = CardIDShower.value + "其中错误号为：" + errorno + "\r\n";
			CardIDShower.value = CardIDShower.value + "卡十六进制序列号为：" + strls.substr(5,8) + "\r\n";
			alert("密码认证错误");
			break;
		case "ER12":
			CardIDShower.value = "密码认证错误" + "\r\n";
			CardIDShower.value = CardIDShower.value + strls + "\r\n";
			CardIDShower.value = CardIDShower.value + "其中错误号为：" + errorno + "\r\n";
			CardIDShower.value = CardIDShower.value + "卡十六进制序列号为：" + strls.substr(5,8) + "\r\n";
			alert("密码认证错误");
			break;
		case "ER13":
			CardIDShower.value = "读卡错误" + "\r\n";
			CardIDShower.value = CardIDShower.value + strls + "\r\n";
			CardIDShower.value = CardIDShower.value + "其中错误号为：" + errorno + "\r\n";
			CardIDShower.value = CardIDShower.value + "卡十六进制序列号为：" + strls.substr(5,8) + "\r\n";
			alert("读卡错误");	
			break;
			
		case "ER14":
			CardIDShower.value = "写卡错误" + "\r\n";
			CardIDShower.value = CardIDShower.value + strls + "\r\n";
			CardIDShower.value = CardIDShower.value + "其中错误号为：" + errorno + "\r\n";
			CardIDShower.value = CardIDShower.value + "卡十六进制序列号为：" + strls.substr(5,8) + "\r\n";
			alert("写卡错误");
			break;	
			
		case "ER21":
			alert("没找到动态库");	
			break;
			
		case "ER22":
			alert("动态库或驱动程序异常");	
			break;
		
		case "ER23":
			alert("读卡器未插上或动态库或驱动程序异常");
			break;	
		case "ER24":
			alert("操作超时，一般是动态库没有反应");
			break;	
		case "ER25":
			alert("发送字数不够");	
			break;
		case "ER26":
			alert("发送的CRC错");	
			break;
		case "ER27":
			alert("接收的字数不够");
			break;	
		case "ER28":
			alert("接收的CRC错");
			break;
		case "ER29":
			alert("函数输入参数格式错误,请仔细查看");
			break;
		default ://写卡成功,其中ER00表示完全成功,ER01表示完全没写到卡数据，ER02表示仅写该卡的第一块成功,，ER02表示仅写该卡的第一二块成功，这是刷卡太快原因
			CardIDShower.value = "写卡成功" + "\r\n";
			CardIDShower.value = CardIDShower.value + strls + "\r\n";
			CardIDShower.value = CardIDShower.value + "其中错误号为：" + errorno + "\r\n";
			CardIDShower.value = CardIDShower.value + "卡十六进制序列号为：" + strls.substr(5,8) + "\r\n";
			IcCardReader.pcdbeep(300);//100表示响100毫秒
			break;
		
	
	}
}

function changecardkey()
{
	//指定控制字
	myctrlword= EXTERNKEY;
	//指定区号
	myareano = 8; //指定为第8区
	//批定密码模式
	authmode = 1; //大于0表示用A密码认证，推荐用A密码认证
	
	//指定序列号，未知卡序列号时可指定为8个0
	mypiccserial="00000000";

	
	//旧密码
	mypicckey_old = "ffffffffffff";

	//新密码
	mypicckey_new = "ffffffffffff";


	
	
	strls=IcCardReader.piccchangesinglekey(myctrlword, mypiccserial,myareano,authmode,mypicckey_old,mypicckey_new)
	errorno = strls.substr(0,4);

	if(errorno == "ER00")
	{
		CardIDShower.value = CardIDShower.value + "修改密码成功，卡序列为:" + strls.substr(strls.length - 8,8) + "\r\n";
	} 
	else if(errorno == "ER08")
	{
		alert("寻不到卡");
	} 
	else
	{
		CardIDShower.value = CardIDShower.value + "修改密码：错误:" + errorno  + "\r\n";
	}
	

	
}


function getdevicenumber()
{

	strls=IcCardReader.pcdgetdevicenumber();
	errorno = strls.substr(0,4);
	if(errorno == "ER00")
        {

		CardIDShower.value = CardIDShower.value + "设备硬件号为:" + strls.substr(strls.length - 8,8) + "\r\n";
	
	}
	

}
function beep()
{
	
	IcCardReader.pcdbeep(100);//100表示响100毫秒
	 
}


function clears1()
{
	//alert("abc");		
	CardIDShower.value = "";
}

</script>

<object classid="clsid:05782014-9FF7-468C-BE96-8EDC73084202" id="IcCardReader" viewastext
        width="0" height="0">
    </object>

</head>
<body>



    <table border="0" style="border-collapse: collapse" width="549" height="300">
        <tr>
            <td width="160" rowspan="5">
<textarea rows="22" cols="118" id="CardIDShower" name="S1">
</textarea>
            </td>
        </tr>
        <tr>
            <td width="160">
                <input type="button" value=" 读 卡 " onclick="javascript:readcard()" />
            </td>
        </tr>
        <tr>
            <td width="160">
			卡号：<input type="text" id="cardNo">
			密码：<input type="text" id="password">
                <input type="button" value=" 写 卡 " onclick="javascript:writecard()" />
            </td>
        </tr>
        <tr>
            <td width="160">
                <input type="button" value="蜂鸣器响" onclick="javascript:beep()" />
            </td>
        </tr>

	<tr>
            <td width="160">
            </td>
        </tr>

	<tr>
            <td width="160">
                <input type="button" value="读取读写器硬件号" onclick="javascript:getdevicenumber()" />
            </td>
        </tr>


        <tr>
            <td width="160">
                <input type="button" value="清空提示" onclick="javascript:clears1()" />
            </td>
        </tr>
    </table>
    <p>
        <font style="font-size: 9pt">提示：</font></p>
    <p>
        <font style="font-size: 9pt">&nbsp;&nbsp;&nbsp; 1、运行本例程前，需先注册 IcCardReader 控件，注册方法为：“开始”-&gt;“运行”-&gt;输入“regsvr32
            ***\IcCardReader.ocx”-&gt;“确定”。其中“***”为控件所在路径。</font></p>
    <p>
        <font style="font-size: 9pt">&nbsp;&nbsp;&nbsp; 2、在IE的Internet 属性设定，让浏览器允许运行 ActiveX 控件。</font></p>
    <p>
        <font style="font-size: 9pt">&nbsp;&nbsp;&nbsp; 3、如果是在服务器端运行本网页，还需在IE的Internet 属性中设定，将服务器网址设为可信站点，否则网页无权运行本地控件。</font></p>
</body>
</html>
