/*----------------------------------------------------------------
// Copyright (C) 2008-2009
//
// 文件名:WebForm1.aspx
// 文件功能描述:测试握奇读写卡器函数
//
//
// 创建标识:李顺生 2009.08.24
//
// 修改标识:
// 修改描述:
//
// 修改标识:
// 修改描述:
//----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Runtime.InteropServices;
namespace prepay.Web
{
public partial class WebForm1 : System.Web.UI.Page
{
//[DllImport("wdcrwv.dll", EntryPoint = "CT_open")]
//public static extern IntPtr CT_open(System.Text.StringBuilder name, UInt32 param1, Byte param2);
[DllImport("wdcrwv.dll", EntryPoint = "CT_open")]
public static extern IntPtr CT_open(System.Text.StringBuilder name, UInt32 param1, Byte param2);
[DllImport("wdcrwv.dll", EntryPoint = "CT_close")]
public static extern Int32 CT_close(IntPtr fd);
[DllImport("wdcrwv.dll", EntryPoint = "ICC_set_NAD")]
public static extern void ICC_set_NAD(IntPtr fd, Byte param2);
[DllImport("wdcrwv.dll", EntryPoint = "ICC_reset")]
public static extern UInt32 ICC_reset(IntPtr fd, char[] lenr, char[] resp);
[DllImport("wdcrwv.dll", EntryPoint = "ICC_read_file")]
public static extern UInt32 ICC_read_file(IntPtr fd, UInt32 offset, UInt32 len, byte[] data);
//从外设向CPU卡读写器发送命令APDU并接收应答APDU
[DllImport("wdcrwv.dll", EntryPoint = "ICC_tsi_api", CharSet = CharSet.Auto)]
//public static extern UInt32 ICC_tsi_api(IntPtr fd, Byte len, Int32[] comm,ref Byte lenr, Int32[] resp);
//public static extern UInt32 ICC_tsi_api(IntPtr fd, Byte len, [MarshalAs(UnmanagedType.LPArray, SizeConst = 10)] byte[] comm,ref Byte lenr, [MarshalAs(UnmanagedType.LPArray, SizeConst = 40)] byte[] resp);
// public static extern UInt32 ICC_tsi_api(IntPtr fd, Byte len, Byte[] test, ref Byte lenr, ref Byte[] response);
public static extern uint ICC_tsi_api(IntPtr fd, byte len, byte[] comm,ref byte lenr, byte[] resp);
//检查读卡器的主卡座是否插入IC卡
[DllImport("wdcrwv.dll", EntryPoint = "ICC_present")]
public static extern UInt32 ICC_present(IntPtr fd);
static int j = 0;
//public static extern IntPtr CT_open(char[] name, UInt32 param1, Byte param2);
//public static extern int CT_open(System.Text.StringBuilder name, UInt32 param1, Byte param2);
// public static extern int CT_open(System.Text.StringBuilder name, int param1, char param2);
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Test(object sender, EventArgs e)
{
Byte param2 = 0x00; //78=N
UInt32 param1 = 0x01;
System.Text.StringBuilder name = new System.Text.StringBuilder("usb1", 200);
// string name ="wdcrwvp1";
//1、打开IC卡终端端口
IntPtr fd = CT_open(name, 2, 1);
//2、设置卡座NAD,默认值为0x12
Byte nadparam2 = new Byte();
nadparam2 = 0X12;
//ICC_set_NAD(fd, nadparam2);
ICC_set_NAD(fd, nadparam2);
//3、对IC卡进行复位
Byte[] comm = { 0X00, 0X12, 0X00, 0X00, 0X00 };
Byte lenrCOMM = new Byte();
Byte[] responseCOMM = new Byte[256];
//UInt32 reCOMM = ICC_tsi_api(fd, 5, comm, ref lenrCOMM, responseCOMM);//36864=0X9000
Byte lenr = new Byte();
Byte[] response = new Byte[256];
Byte[] test = { 0x00, 0xA4, 0x00,0X00,0X02, 0xEF, 0x01 };//选择购电信息文件
Byte[] test1 = { 0x00, 0xA4, 0x00, 0X00, 0X02, 0x00, 0x01 };
Byte len = 0x07;
UInt32 re = ICC_tsi_api(fd, 7, test, ref lenr, response);//27266=6A82 文件未找到
//UInt32 re = ICC_tsi_api(fd, len, test1, ref lenr, response);
Byte[] temp = new Byte[100];
//读取购电文件 //EF01 75BYTE
Byte[] file1 = { 0x00, 0xB0, 0x81, 0X00, 0x4B }; //读取购电信息文件1 推荐方法
Byte[] file1as = { 0x00, 0xB0, 0x00, 0X00, 0x4B }; //读取购电信息文件1方法2
Byte[] file2 = { 0x00, 0xB0, 0x82, 0X00,0x7F }; //读取购电信息文件2
Byte[] file1WD = { 0x04, 0xB0, 0x00, 0x00, 0x04, 0x5F, 0x53, 0x7A, 0x6D }; //读取购电信息文件1 04B0000004CA4021F4
Byte lenrfile1 = new Byte();
Byte[] responsefile1 = new Byte[256];
Byte lenrfile2 = new Byte();
Byte[] responsefile2 = new Byte[256];
UInt32 reFILE1 = ICC_tsi_api(fd, 9, file1WD, ref lenrfile1, responsefile1);//27392=0X6B00 错误的参数(偏移量超出EF)
UInt32 reFILE2 = ICC_tsi_api(fd, 5, file1, ref lenrfile2, responsefile2);//27392=0X6B00 错误的参数(偏移量超出EF)
//写购电文件
Byte[] WriteFile1 = { 0x00, 0xD6, 0x81, 0X00, 0x4B, 0x68, 0x01, 0x46, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x02, 0x00, 0x00, 0x00, 0x64, 0x00, 0x02, 0x00, 0x00, 0xC3, 0x50, 0x00, 0x00, 0x4E, 0x20, 0x00, 0x07, 0xA1, 0x20, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x08, 0x05, 0x25, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xD5, 0xC5, 0xC8, 0xFD, 0x20, 0x08, 0x05, 0x25, 0x00, 0x01, 0x00, 0xE8, 0x16 };
Byte WriteLenrFile1 = new Byte();
Byte[] WritePesponseFile1 = new Byte[256];
UInt32 WriteFILE1Result = ICC_tsi_api(fd, 0x80, WriteFile1, ref WriteLenrFile1, WritePesponseFile1);//WriteFILE1Result=36864 成功
//得到购电返写文件
Byte[] file1WDResponse = { 0x00, 0xC0, 0x00, 0x00, 0xF4 }; //读取购电信息文件1
Byte lenrfile1Res = new Byte();
Byte[] responsefile1Res = new Byte[256];
UInt32 reFILE1Response = ICC_tsi_api(fd, 5, file1WDResponse, ref lenrfile1Res, responsefile1Res);//27392=0X6B00 错误的参数(偏移量超出EF)
temp = response;
byte lenrResult = new Byte();
lenrResult = lenr;
String abc = lenrResult.ToString();
int length = Int32.Parse(abc);
Char[] resulttemp=new Char[20];
String tempString = "";
String tempaa = "";
for (int i = 0; i < length; i++)
{
tempaa = Convert.ToString(temp[i], 16);
tempString = tempString+ tempaa;
//resulttemp[i]=tempString.ToCharArray();
//Console.WriteLine(Convert.ToString(Convert.ToInt32(tempString), 16));
}
//检查读卡器的主卡座是否插入IC卡
UInt32 checkresult = ICC_present(fd);
//读取二进制文件
byte[] data = new byte[256];
UInt32 offset = 0;
UInt32 lenFlie = 75;
UInt32 sw = ICC_read_file(fd, offset, lenFlie, data);
//4、关闭打开的设备
Int32 result = CT_close(fd);
j = j + 1;
Response.Write(tempString + " " + j.ToString());
// System.Text.StringBuilder
// IntPtr b = CT_open("USB1", 1, '0');
// IntPtr c = CT_open("Wdcrwvp1", 1, 0);
//IntPtr d = CT_open("Wdcrwvp1", 1, '0');
}
}
}
dll 中含有unsigned char数组,被C#调用
最新推荐文章于 2021-11-10 16:41:20 发布