C#如何读取24C64的数据?
现有一读卡机,可以读取24C64的内容,代码如下
using System;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
[StructLayout(LayoutKind.Sequential)]
public unsafe class IC
{
//对设备进行初始化
[DllImport("Mwic_32.dll", EntryPoint = "auto_init", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern int auto_init(int port, int baud);
//获取设备当前状态
[DllImport("Mwic_32.dll", EntryPoint = "get_status", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern Int16 get_status(int icdev, Int16* state);
//关闭设备通讯接口
[DllImport("Mwic_32.dll", EntryPoint = "ic_exit", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern int ic_exit(int icdev);
//使设备发出蜂鸣声
[DllImport("Mwic_32.dll", EntryPoint = "dv_beep", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern int dv_beep(int icdev, int time);
//向IC卡中写数据
[DllImport("Mwic_32.dll", EntryPoint = "swr_24c64", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern int swr_24c64(int icdev, int offset, int len, char* w_string);
//向IC卡中读数据
[DllImport("Mwic_32.dll", EntryPoint = "srd_24c64", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern int srd_24c64(int icdev, int offset, int len, char* r_string);
// public static extern int srd_24C64_Hex(int icdev,int offset,int le,ref
//检查卡是否正确
[DllImport("Mwic_32.dll", EntryPoint = "chk_24c64", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern int chk_24c64(int icdev);
[DllImport("Mwic_32.dll", EntryPoint = "asc_asc", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern int asc_asc(char* s_string, char* d_string, int len);
[DllImport("Mwic_32.dll", EntryPoint = "hex_asc", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern int hex_asc(char* s_string, char* d_string, int len);
}
}
IC类调用Mwic_32.dll类,
Mwic_32.dll的方法如下:
extern "C
{
//********************** commual subroutine ***********************
HANDLE __stdcall ic_init(__int16 port,unsigned long baud);
HANDLE __stdcall auto_init(__int16 port,unsigned long baud);
__int16 __stdcall ic_exit(HANDLE icdev);
__int16 __stdcall srd_ver(HANDLE icdev, __int16 len,
unsigned char *data_buffer);
__int16 __stdcall set_baud(HANDLE icdev, unsigned long baud);
__int16 __stdcall cmp_dvsc(HANDLE icdev, __int16 len,
unsigned char *dta_buffer);
__int16 __stdcall srd_dvsc(HANDLE icdev, __int16 len,
unsigned char *data_buffer);
__int16 __stdcall swr_dvsc(HANDLE icdev, __int16 len,
unsigned char *data_buffer);
__int16 __stdcall setsc_md(HANDLE icdev, __int16 mode);
__int16 __stdcall turn_on(HANDLE icdev);
__int16 __stdcall turn_off(HANDLE icdev);
__int16 __stdcall auto_pull(HANDLE icdev);
__int16 __stdcall get_status(HANDLE icdev,__int16 *state);
__int16 __stdcall dv_beep(HANDLE icdev, __int16 time);
__int16 __stdcall swr_eeprom(HANDLE icdev, __int16 offset,__int16 len,
unsigned char *data_buffer);
__int16 __stdcall srd_eeprom(HANDLE icdev, __int16 offset,__int16 len,
unsigned char *data_buffer);
__int16 __stdcall srd_snr(HANDLE icdev, __int16 len,
unsigned char *data_buffer);
__int16 __stdcall val_read(HANDLE icdev,unsigned long *p_Value);
__int16 __stdcall val_inc(HANDLE icdev, unsigned long _Value);
__int16 __stdcall val_dec(HANDLE icdev, unsigned long _Value);
__int16 __stdcall val_set(HANDLE icdev, unsigned long _Value);
__int32 __stdcall chk_baud(__int16 port);
__int16 __stdcall chk_card(HANDLE icdev);
__int16 __stdcall ic_encrypt( char *key,char *ptrSource, unsigned short msgLen, char *ptrDest);
__int16 __stdcall ic_decrypt( char *key,char *ptrSource, unsigned short msgLen, char *ptrDest);
unsigned long __stdcall DES_Encrypt( unsigned char *Key,__int16 KeyLen,
unsigned char *Source, unsigned long SrcLen,
unsigned char *result);
unsigned long __stdcall DES_Decrypt( unsigned char *Key,__int16 KeyLen,
unsigned char *Source, unsigned long SrcLen,
unsigned char *result);
__int16 __stdcall lib_ver(char *VerStr);
__int16 __stdcall asc_hex(unsigned char *asc, unsigned char *hex, long pair_len);
__int16 __stdcall hex_asc(unsigned char *hex,unsigned char *asc,long length);
__int16 __stdcall asc_asc(unsigned char *src,unsigned char *des,long len);
__int16 __stdcall float_uchar(float f,unsigned char *c);
__int16 __stdcall uchar_float(unsigned char *c,float *f);
__int16 __stdcall short_uchar(short i,unsigned char *c);
__int16 __stdcall uchar_short(unsigned char *c,short *i);
__int16 __stdcall long_uchar(long l,unsigned char *c);
__int16 __stdcall uchar_long(unsigned char *c,long *l);
//********************** operate sle 4404 **************************
__int16 __stdcall srd_4404(HANDLE icdev,__int16 offset,__int16 len,unsigned char *data_buffer);
__int16 __stdcall swr_4404(HANDLE icdev,__int16 offset,__int16 len,unsigned char *data_buffer);
__int16 __stdcall csc_4404(HANDLE icdev,__int16 len,unsigned char *data_buffer);
__int16 __stdcall wsc_4404(HANDLE icdev,__int16 len,unsigned char *data_buffer);
__int16 __stdcall rsc_4404(HANDLE icdev,__int16 len,unsigned char *data_buffer);
__int16 __stdcall rsct_4404(HANDLE icdev,__int16 *counter);
__int16 __stdcall cesc_4404(HANDLE icdev,__int16 len,unsigned char *data_buffer);
__int16 __stdcall wesc_4404(HANDLE icdev,__int16 len,unsigned char *data_buffer);
__int16 __stdcall resc_4404(HANDLE icdev,__int16 len,unsigned char *data_buffer);
__int16 __stdcall resct_4404(HANDLE icdev,__int16 *counter);
__int16 __stdcall ser_4404(HANDLE icdev,__int16 offset,__int16 len);
__int16 __stdcall fakefus_4404(HANDLE icdev,__int16 mode);
__int16 __stdcall clrpr_4404(HANDLE icdev);
__int16 __stdcall clrrd_4404(HANDLE icdev);
__int16 __stdcall psnl_4404(HANDLE icdev);
__int16 __stdcall chk_4404(HANDLE icdev);
//*********************** operate sle 4406 *************************
__int16 __stdcall srd_4406(HANDLE icdev,__int16 offset,__int16 len,unsigned char *data_buffer);
__int16 __stdcall swr_4406(HANDLE icdev,__int16 offset,__int16 len,unsigned char *data_buffer);
__int16 __stdcall csc_4406(HANDLE icdev,__int16 len,unsigned char *data_buffer);
__int16 __stdcall wsc_4406(HANDLE icdev,__int16 len,unsigned char *data_buffer);
__int16 __stdcall rsc_4406(HANDLE icdev,__int16 len,unsigned char *data_buffer);
__int16 __stdcall rsct_4406(HANDLE icdev,__int16 *Counter);
__int16 __stdcall eswc_4406(HANDLE icdev,__int16 offset);
__int16 __stdcall psnl_4406(HANDLE icdev);
__int16 __stdcall chk_4406(HANDLE icdev);
//*********************** operate at88sc102 ************************
__int16 __stdcall srd_102(HANDLE icdev,__int16 zone,__int16 offset,__int16 len,unsigned char *data_buffer);
__int16 __stdcall swr_102(HANDLE icdev,__int16 zone,__int16 offset,__int16 len,unsigned char *data_buffer);
__int16 __stdcall ser_102(HANDLE icdev,__int16 zone,__int16 offset,__int16 len);
__int16 __stdcall csc_102(HANDLE icdev,__int16 len,unsigned char *data_buffer);
__int16 __stdcall rsc_102(HANDLE icdev,__int16 len,unsigned char *data_buffer);
__int16 __stdcall wsc_102(HANDLE icdev,__int16 len,unsigned char *data_buffer);
__int16 __stdcall rsct_102(HANDLE icdev,__int16 *counter);
__int16 __stdcall cesc_102(HANDLE icdev,__int16 zone,__int16 len,unsigned char *data_buffer);
__int16 __stdcall resc_102(HANDLE icdev,__int16 zone,__int16 len,unsigned char *data_buffer);
__int16 __stdcall wesc_102(HANDLE icdev,__int16 zone,__int16 len,unsigned char *data_buffer);
__int16 __stdcall resct_102(HANDLE icdev,__int16 zone,__int16 *counter);
__int16 __stdcall clrpr_102(HANDLE icdev,__int16 zone);
__int16 __stdcall clrrd_102(HANDLE icdev,__int16 zone);
__int16 __stdcall fakefus_102(HANDLE icdev,__int16 mode);
__int16 __stdcall psnl_102(HANDLE icdev);
__int16 __stdcall chk_102(HANDLE icdev);
//********************* operate at88sc1604 **************************
__int16 __stdcall srd_1604(HANDLE icdev,__int16 zone,__int16 offset,__int16 len,unsigned char *data_buffer);
__int16 __stdcall swr_1604(HANDLE icdev,__int16 zone,__int16 offset,__int16 len,unsigned char *data_buffer);
__int16 __stdcall ser_1604(HANDLE icdev,__int16 zone,__int16 offset,__int16 len);
__int16 __stdcall csc_1604(HANDLE icdev,__int16 zone,__int16 len,unsigned char *data_buffer);
__int16 __stdcall rsc_1604(HANDLE icdev,__int16 zone,__int16 len,unsigned char *data_buffer);
__int16 __stdcall wsc_1604(HANDLE icdev,__int16 zone,__int16 len,unsigned char *data_buffer);
__int16 __stdcall rsct_1604(HANDLE icdev,__int16 zone,__int16 *counter);
__int16 __stdcall cesc_1604(HANDLE icdev,__int16 zone,__int16 len,unsigned char *data_buffer);
__int16 __stdcall resc_1604(HANDLE icdev,__int16 zone,__int16 len,unsigned char *data_buffer);
__int16 __stdcall wesc_1604(HANDLE icdev,__int16 zone,__int16 len,unsigned char *data_buffer);
__int16 __stdcall resct_1604(HANDLE icdev,__int16 zone,__int16 *counter);
__int16 __stdcall clrpr_1604(HANDLE icdev,__int16 zone);
__int16 __stdcall clrrd_1604(HANDLE icdev,__int16 zone);
__int16 __stdcall fakefus_1604(HANDLE icdev,__int16 mode);
__int16 __stdcall psnl_1604(HANDLE icdev);
__int16 __stdcall chk_1604(HANDLE icdev);
//********************* operate at88sc1604b **************************
__int16 __stdcall srd_1604b(HANDLE icdev,__int16 zone,__int16 offset,__int16 len,unsigned char *data_buffer);
__int16 __stdcall swr_1604b(HANDLE icdev,__int16 zone,__int16 offset,__int16 len,unsigned char *data_buffer);
__int16 __stdcall ser_1604b(HANDLE icdev,__int16 zone,__int16 offset,__int16 len);
__int16 __stdcall csc_1604b(HANDLE icdev,__int16 zone,__int16 len,unsigned char *data_buffer);
__int16 __stdcall rsc_1604b(HANDLE icdev,__int16 zone,__int16 len,unsigned char *data_buffer);
__int16 __stdcall wsc_1604b(HANDLE icdev,__int16 zone,__int16 len,unsigned char *data_buffer);
__int16 __stdcall rsct_1604b(HANDLE icdev,__int16 zone,__int16 *counter);
__int16 __stdcall cesc_1604b(HANDLE icdev,__int16 zone,__int16 len,unsigned char *data_buffer);
__int16 __stdcall resc_1604b(HANDLE icdev,__int16 zone,__int16 len,unsigned char *data_buffer);
__int16 __stdcall wesc_1604b(HANDLE icdev,__int16 zone,__int16 len,unsigned char *data_buffer);
__int16 __stdcall resct_1604b(HANDLE icdev,__int16 zone,__int16 *counter);
__int16 __stdcall clrpr_1604b(HANDLE icdev,__int16 zone);
__int16 __stdcall clrrd_1604b(HANDLE icdev,__int16 zone);
__int16 __stdcall fakefus_1604b(HANDLE icdev,__int16 mode);
__int16 __stdcall psnl_1604b(HANDLE icdev);
__int16 __stdcall chk_1604b(HANDLE icdev);
//************************ operate at24c01A ***************************
__int16 __stdcall swr_24c01a(HANDLE icdev, __int16 offset, __int16 len,
unsigned char *data_buffer);
__int16 __stdcall srd_24c01a(HANDLE icdev, __int16 offset, __int16 len,
unsigned char *data_buffer);
__int16 __stdcall chk_24c01a(HANDLE icdev);
//************************ operate at24c02 ***************************
__int16 __stdcall swr_24c02(HANDLE icdev, __int16 offset, __int16 len,
unsigned char *data_buffer);
__int16 __stdcall srd_24c02(HANDLE icdev, __int16 offset, __int16 len,
unsigned char *data_buffer);
__int16 __stdcall chk_24c02(HANDLE icdev);
//************************ operate at24c04 ***************************
__int16 __stdcall swr_24c04(HANDLE icdev, __int16 offset, __int16 len,
unsigned char *data_buffer);
__int16 __stdcall srd_24c04(HANDLE icdev, __int16 offset, __int16 len,
unsigned char *data_buffer);
__int16 __stdcall chk_24c04(HANDLE icdev);
//************************ operate at24c08 ***************************
__int16 __stdcall swr_24c08(HANDLE icdev, __int16 offset, __int16 len,
unsigned char *data_buffer);
__int16 __stdcall srd_24c08(HANDLE icdev, __int16 offset, __int16 len,
unsigned char *data_buffer);
__int16 __stdcall chk_24c08(HANDLE icdev);
//************************ operate at24c16 ***************************
__int16 __stdcall swr_24c16(HANDLE icdev, __int16 offset, __int16 len,
unsigned char *data_buffer);
__int16 __stdcall srd_24c16(HANDLE icdev, __int16 offset, __int16 len,
unsigned char *data_buffer);
__int16 __stdcall chk_24c16(HANDLE icdev);
//************************ operate at24c64 ***************************
__int16 __stdcall swr_24c64(HANDLE icdev, __int16 offset, __int16 len,
unsigned char *data_buffer);
__int16 __stdcall srd_24c64(HANDLE icdev, __int16 offset, __int16 len,
unsigned char *data_buffer);
__int16 __stdcall chk_24c64(HANDLE icdev);
//*********************** operate sle 4418 *************************
__int16 __stdcall srd_4418(HANDLE icdev,__int16 offset,__int16 len,unsigned char *data_buffer);
__int16 __stdcall swr_4418(HANDLE icdev,__int16 offset,__int16 len,unsigned char *data_buffer);
__int16 __stdcall rdwpb_4418(HANDLE icdev,__int16 offset,__int16 len,unsigned char *data_buffer);
__int16 __stdcall wrwpb_4418(HANDLE icdev,__int16 offset,__int16 len,unsigned char *data_buffer);
__int16 __stdcall pwr_4418(HANDLE icdev,__int16 offset,__int16 len,unsigned char *data_buffer);
__int16 __stdcall chk_4418(HANDLE icdev);
//*********************** operate sle 4428 *************************
__int16 __stdcall srd_4428(HANDLE icdev,__int16 offset,__int16 len,unsigned char *data_buffer);
__int16 __stdcall swr_4428(HANDLE icdev,__int16 offset,__int16 len,unsigned char *data_buffer);
__int16 __stdcall rdwpb_4428(HANDLE icdev,__int16 offset,__int16 len,unsigned char *data_buffer);
__int16 __stdcall wrwpb_4428(HANDLE icdev,__int16 offset,__int16 len,unsigned char *data_buffer);
__int16 __stdcall pwr_4428(HANDLE icdev,__int16 offset,__int16 len,unsigned char *data_buffer);
__int16 __stdcall csc_4428(HANDLE icdev,__int16 len,unsigned char *data_buffer);
__int16 __stdcall wsc_4428(HANDLE icdev,__int16 len,unsigned char *data_buffer);
__int16 __stdcall rsc_4428(HANDLE icdev,__int16 len,unsigned char *data_buffer);
__int16 __stdcall rsct_4428(HANDLE icdev,__int16 *counter);
__int16 __stdcall chk_4428(HANDLE icdev);
//*********************** operate sle 4442 **************************
__int16 __stdcall srd_4442(HANDLE icdev,__int16 offset,__int16 len,
unsigned char *data_buffer);
__int16 __stdcall swr_4442(HANDLE icdev,__int16 offset,__int16 len,
unsigned char *data_buffer);
__int16 __stdcall prd_4442(HANDLE icdev,__int16 len,
unsigned char *data_buffer);
__int16 __stdcall pwr_4442(HANDLE icdev,__int16 offset,__int16 len,
unsigned char *data_buffer);
__int16 __stdcall csc_4442(HANDLE icdev,__int16 len,
unsigned char *data_buffer);
__int16 __stdcall wsc_4442(HANDLE icdev,__int16 len,
unsigned char *data_buffer);
__int16 __stdcall rsc_4442(HANDLE icdev,__int16 len,
unsigned char *data_buffer);
__int16 __stdcall rsct_4442(HANDLE icdev,__int16 *counter);
__int16 __stdcall chk_4442(HANDLE icdev);
//*********************** operate sle 4432 **************************
__int16 __stdcall srd_4432(HANDLE icdev,__int16 offset,__int16 len,
unsigned char *data_buffer);
__int16 __stdcall swr_4432(HANDLE icdev,__int16 offset,__int16 len,
unsigned char *data_buffer);
__int16 __stdcall prd_4432(HANDLE icdev,__int16 len,
unsigned char *data_buffer);
__int16 __stdcall pwr_4432(HANDLE icdev,__int16 offset,__int16 len,
unsigned char *data_buffer);
__int16 __stdcall chk_4432(HANDLE icdev);
//*********************** operate at45d041 *************************
__int16 __stdcall srd_45d041(HANDLE icdev,__int16 page,__int16 offset,unsigned long len,
unsigned char *data_buffer);
__int16 __stdcall swr_45d041(HANDLE icdev,__int16 page,__int16 offset,unsigned long len,
unsigned char *data_buffer);
__int16 __stdcall rdstrg_45d041(HANDLE icdev,unsigned char *ch);
__int16 __stdcall chk_45d041(HANDLE icdev);
//************************ operate at93c46a ***************************
__int16 __stdcall swr_93c46a(HANDLE icdev,__int16 offset, __int16 len,
unsigned char *data_buffer);
__int16 __stdcall srd_93c46a(HANDLE icdev,__int16 offset, __int16 len,
unsigned char *data_buffer);
__int16 __stdcall eral_93c46a(HANDLE icdev);
__int16 __stdcall chk_93c46a(HANDLE icdev);
//************************ operate at93c46 ***************************
__int16 __stdcall swr_93c46(HANDLE icdev,__int16 offset, __int16 len, unsigned char *data_buffer);
__int16 __stdcall srd_93c46(HANDLE icdev,__int16 offset, __int16 len, unsigned char *data_buffer);
__int16 __stdcall eral_93c46(HANDLE icdev);
__int16 __stdcall chk_93c46(HANDLE icdev);
unsigned char *data_buffer);
//************************* cpu card ***************************************//
__int16 __stdcall cpu_reset(HANDLE icdev,unsigned char *data_buffer);
__int16 __stdcall cpu_protocol(HANDLE icdev,int len, unsigned char *send_cmd, unsigned char *receive_data);
__int16 __stdcall cpu_comres(HANDLE icdev,int len, unsigned char *send_cmd, unsigned char *receive_data);
}
//操作界面
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WindowsApplication1;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static int icdev = IC.auto_init(0, 9600);
private void button1_Click(object sender, EventArgs e)//建立连接
{
//初始化
//int icdev = IC.auto_init(0, 9600);
if (icdev < 0)
MessageBox.Show("端口初始化失败,请检查接口线是否连接正确。","错误提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
//检查卡
unsafe
{
Int16 status = 0;
Int16 result = 0;
result = IC.get_status(icdev, &status);
if (result != 0)
{
MessageBox.Show("设备当前状态错误!");
int d1 = IC.ic_exit(icdev);
//关闭设备
return;
}
if (status != 1)
{
MessageBox.Show("请插入IC卡");
int d2 = IC.ic_exit(icdev);
//关闭设备
return;
}
else
{
textBox1.Text = "设备正常运行!" + "\r\n";
}
}
unsafe
{
//Int16 status = 0;
Int32 result = 0;
result = IC.chk_24c64(icdev);
if (result != 0)
{
MessageBox.Show("卡的类型错误!");
int d3 = IC.ic_exit(icdev);
}
else
{
textBox1.Text = textBox1.Text+"IC卡类型正确!" + "\r\n";
IC.dv_beep(icdev, 20);
}
}
//textBox1.Text = "连接成功!"+"\r\n";
//textBox1.WordWrap = true;
}
private void button3_Click(object sender, EventArgs e)//读取
{
string ss = "";
char s = 'a';
char s1 = 's';
unsafe
{
int read = -1;
read = IC.srd_24c64(icdev, 2, 1, &s);//有数据可显示出来全部为'0' //read = IC.hex_asc(&s, &s1, 1);
//ss=Convert
//if (read == 0) MessageBox.Show("IC卡中数据读取成功!");
}
}
}
}