我用的这个第三方设备需要初始化、蜂鸣、核对密码等!以下是C#的代码:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using System.Runtime.InteropServices;
/// <summary>
/// DUKa 的摘要说明
/// </summary>
public class DUKa
{
public DUKa()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region 对USB接口的使用(PHILIPH卡)
[DllImport("dcrf32.dll")]
public static extern int dc_init(Int32 port, long baud); //初试化
[DllImport("dcrf32.dll")]
public static extern short dc_exit(int icdev);
[DllImport("dcrf32.dll")]
public static extern short dc_reset(int icdev, uint sec);
[DllImport("dcrf32.dll")]
public static extern short dc_request(int icdev, char _Mode, ref uint TagType);
[DllImport("dcrf32.dll")]
public static extern short dc_card(int icdev, char _Mode, ref ulong Snr);
[DllImport("dcrf32.dll")]
public static extern short dc_halt(int icdev);
[DllImport("dcrf32.dll")]
public static extern short dc_anticoll(int icdev, char _Bcnt, ref ulong IcCardNo);
[DllImport("dcrf32.dll")]
public static extern short dc_beep(int icdev, uint _Msec);
[DllImport("dcrf32.dll")]
public static extern short dc_authentication(int icdev, int _Mode, int _SecNr);
[DllImport("dcrf32.dll")]
public static extern short dc_load_key(int icdev, int mode, int secnr, [In] byte[] nkey); //密码装载到读写模块中
[DllImport("dcrf32.dll")]
public static extern short dc_load_key_hex(int icdev, int mode, int secnr, string nkey); //密码装载到读写模块中
[DllImport("dcrf32.dll")]
public static extern short dc_write(int icdev, int adr, [In] byte[] sdata); //向卡中写入数据
[DllImport("dcrf32.dll")]
public static extern short dc_write(int icdev, int adr, [In] string sdata); //向卡中写入数据
[DllImport("dcrf32.dll")]
public static extern short dc_write_hex(int icdev, int adr, [In] string sdata); //向卡中写入数据(转换为16进制)
[DllImport("dcrf32.dll")]
public static extern short dc_read(int icdev, int adr, [Out] byte[] sdata);
[DllImport("dcrf32.dll")]
public static extern short dc_read(int icdev, int adr, [MarshalAs(UnmanagedType.LPStr)] StringBuilder sdata); //从卡中读数据
[DllImport("dcrf32.dll")]
public static extern short dc_read_hex(int icdev, int adr, [MarshalAs(UnmanagedType.LPStr)] StringBuilder sdata); //从卡中读数据(转换为16进制)
[DllImport("dcrf32.dll")]
public static extern int a_hex(string oldValue, ref string newValue, Int16 len); //普通字符转换成十六进制字符
[DllImport("dcrf32.dll")]
public static extern void hex_a(ref string oldValue, ref string newValue, int len); //十六进制字符转换成普通字符
#endregion
public static bool WriteIn(string str)
{
Int32 _icdev = -1;
try
{
if (_icdev < 0)
{
_icdev = dc_init(100, 115200);
}
int st;
//1.初始化
if (_icdev > 0)
{
}
else
{
dc_exit(_icdev);
return false;
}
//蜂鸣
st = dc_beep(_icdev, 10);
if (st == 0)
{
}
else
{
dc_exit(_icdev);
return false;
}
ulong icCardNo = 0;
char tt = (char)0;
uint ss = 0;
st = dc_reset(_icdev, ss);
st = dc_card(_icdev, tt, ref icCardNo);
if (icCardNo != 0)
{
}
else
{
dc_exit(_icdev);
return false;
}
byte[] hexkey = new byte[6] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
st = dc_load_key(_icdev, 0, 1, hexkey);
//核对密码
int sector = 1;
st = dc_authentication(_icdev, 0, sector);
if (st == 0)
{
}
else
{
dc_exit(_icdev);
return false;
}
int address = 1;
byte[] data32;
data32 = new byte[16] { 0x6A, 0xC2, 0x92, 0xFA, 0xA1, 0x31, 0x5B, 0x4D, 0x6A, 0xC2, 0x92, 0xFA, 0xA1, 0x31, 0x5B, 0x4D };//= "12345678901234561234567890123456";
string data32_hex = "".PadLeft(32, ' ');
address = sector * 4 + 2;
//写卡操作
data32_hex = str;
st = dc_write(_icdev, address, data32_hex);
if (st == 0)
{
dc_exit(_icdev);
}
else
{
dc_exit(_icdev);
return false;
}
dc_exit(_icdev);
return true;
}
catch
{
dc_exit(_icdev);
return false;
}
}
public static bool ReadCard(string str, out string CardNo)
{
CardNo = "";
Int32 _icdev = -1;
try
{
if (_icdev < 0)
{
_icdev = dc_init(100, 115200);
}
int st;
//1.初始化
if (_icdev > 0)
{
}
else
{
dc_exit(_icdev);
return false;
}
//蜂鸣
st = dc_beep(_icdev, 10);
if (st == 0)
{
}
else
{
dc_exit(_icdev);
return false;
}
ulong icCardNo = 0;
char tt = (char)0;
uint ss = 0;
st = dc_reset(_icdev, ss);
st = dc_card(_icdev, tt, ref icCardNo);
if (icCardNo != 0)
{
}
else
{
dc_exit(_icdev);
return false;
}
byte[] hexkey = new byte[6] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
st = dc_load_key(_icdev, 0, 1, hexkey);
//核对密码
int sector = 1;
st = dc_authentication(_icdev, 0, sector);
if (st == 0)
{
}
else
{
dc_exit(_icdev);
return false;
}
StringBuilder temp = new StringBuilder(64);
StringBuilder temp1 = new StringBuilder(64);
int address = 1;
byte[] data32;
data32 = new byte[16] { 0x6A, 0xC2, 0x92, 0xFA, 0xA1, 0x31, 0x5B, 0x4D, 0x6A, 0xC2, 0x92, 0xFA, 0xA1, 0x31, 0x5B, 0x4D };//= "12345678901234561234567890123456";
string data32_hex = "".PadLeft(32, ' ');
address = sector * 4 + 2;
string databuff32 = string.Empty;
st = dc_read(_icdev, address, temp1);
if (st == 0)
{
CardNo = temp1.ToString();
}
dc_exit(_icdev);
return true;
}
catch
{
dc_exit(_icdev);
return false;
}
}
}