西门子SLE4442读写操作源码(明华读写模块)

来源:互联网 发布:大学生电脑软件 编辑:程序博客网 时间:2024/05/01 07:29

 SE4442卡为256字节加密卡,存在读数据、写数据、保护数据以及密码操作。

电气性能

256×8位EEPROM

字节直接寻址
低地址32字节数据设不可恢复的写保护位
32位保护位
写数据操作结束标志
符合7816-3标准的复位响应
字节擦除然后写入时间:标准值25ms
最少10000次的写擦除周期
数据保存期:10年
保密特性

三字节的用户密码。
密码核对正确前,全部数据只可读,不可改写。
核对密码正确后可以更改数据,包括密码再内。
错误计数器,初始值为3,密码核对出错1次,便减1,若计数器值为0,则卡自动锁死,数据只可读出,不可再进行更改也无法再进行密码核对;若不为零时,有一次密码核对正确,可恢复到初始值3。
写保护区(前32个字节)的每一字节可单独进行写保护,进行写保护后,内容不可再更改(即固化数据)。
提供操作函数

写操作函数: swr_4442()
读操作函数: srd_4442()
测卡型函数: chk_4442()
效验密码函数: csc_4442()
读取密码函数: rsc_4442()
更改密码函数: wsc_4442()
读密码错误计数: rsct_4442()
写保护位函数: pwr_4442()
读保护位函数: prd_4442()


int swr_4442(int icdev, int offset, int len, unsigned char *w_string)
说明:向指定地址写数据
调用:icdev:通讯设备标识符
offset: 偏移地址,其值范围0~255
len:字符串长度,其值范围1~256
w_string: 写入数据
返回: <0 错误
=0 正确

int srd_4442(int icdev, int offset, int len, unsigned char* r_string )
说明:从指定地址读数据
调用:icdev:通讯设备标识符
offset: 偏移地址,其值范围0~255

len:字符串长度,其值范围1~256
r_string: 读出数据所存放地址指针
返回: <>0 错误
=0

int chk_4442(int icdev)
说明:检查卡型是否正确
调用:icdev: 通讯设备标识符
返回: <0 错误
=0 正确
读写SLE4442卡范例
下面C语言范例,首先测试是否有SLE4442卡插入读写器。若有,核对密码,然后进行读写操作,并比较两次操作数据是否相同,若相同,返回操作正确。

 

#include
#include mwic.h

int main()
{
int i,st;
unsigned char ch1[100],ch2[100];
int icdev;
//intialize COM2 with baud rate 9600
icdev=auto_init(1,9600);
if(icdev<0)
{
printf(initialize error !/n);
return(icdev);
}
//check whether SLE4442 card inserted
st=chk_4442(icdev);
if(st)
{
printf(Wrong card or have no card inserted !/n);
return(st);
}
ch1[0]=0xb6;
ch1[1]=0x23;
ch1[2]=0x07;
//Compare secury code
st=csc_4442(icdev,3,ch1);

if(st)
{
printf(Compare secury code error !/n);
return(st);
}
//initialize char ch1 and ch2
for(i=0;i<100;i++)
{
ch1[i]=i;
ch2[i]=0xff;
}
//Write ch1 to card with offset=0x20,len=100
st=swr_4442(icdev,0x20,100,ch1);
if(st)
{
printf(Write error !/n);
return(st);
}
//Read form card and stroe in ch2 with offset=0x20,len=100
st=srd_4442(icdev,0x20,100,ch2);
if(st)
{
printf(Read error !/n);

return(st);
}
//Compare ch1 with ch2
for(i=0;i<100;i++)
{
if(ch1[i]!=ch2[i])
{
printf(Compare error !/n);
return(st);
}
}
printf(OK !/n);
return(0);
}

 

int csc_4442(int icdev, int len, unsigned char* p_string)
说明:核对卡密码
调用:icdev:通讯设备标识符
len:密码个数,其值为3
p_string: 密码字符串指针
返回: <0 错误
=0 密码正确

int wsc_4442(int icdev, int len,unsigned char* p_string)
说明:改写卡密码
调用:icdev:通讯设备标识符
len:密码个数,其值为3
p_string: 新密码地址指针
返回:<0 错误
=0 正确

int rsc_4442(int icdev, int len, unsigned char* p_string)
说明:读出卡密码
调用:icdev:通讯设备标识符
len:密码个数,其值为3
p_string: 存放密码地址指针
返回:<>0 错误
=0 正确

int rsct_4442(int icdev, int* counter)
说明:读出密码错误计数器值
调用:icdev:通讯设备标识符
counter:密码错误记数值存放指针
返回: <0 错误
>=0 正确

SLE4442密码操作范例:

下面C语言范例,首先测试是否有SLE4442卡插入读写器。若有,核对密码并读取错误计数,然后改写密码并回读,比较两次操作数据是否相同,若相同,返回操作正确。
#include
#include mwic.h
int main()
{
int i,st;
unsigned char ch1[100],ch2[100];
int icdev;
//intialize COM2 with baud rate 9600
icdev=auto_init(1,9600);
if(icdev<0)
{
printf(initialize error !/n);
return(icdev);
}
//check whether SLE4442 card inserted
st=chk_4442(icdev);

if(st)
{
printf(Wrong card or have no card inserted !/n);
return(st);
}
ch1[0]=0xb6;
ch1[1]=0x23;
ch1[2]=0x07;
//Compare secury code
st=csc_4442(icdev,3,ch1);
if(st)
{
printf(Compare secury code error !/n);
return(st);
}
//Read error counter
st=rsct_4442(icdev,&i);
if(st)
{
printf(Read error counter error !/n);
return(st);
}
printf(揺rror counter = %d/n?i);
ch1[0]=0xaa;

ch1[1]=0xaa;
ch1[2]=0xaa;
//Change security code with 0xaaaaaa
st=wsc_4442(icdev,3,ch1);
if(st)
{
printf(Write security code error !/n);
return(st);
}
printf(揘ew security code is 0xaaaaaa/n?;
//Read security code
st=rsc_4442(icdev,3,ch2);
if(st)
{
printf(Read security code error !/n);
return(st);
}
//Compare ch1 with ch2
for(i=0;i<3;i++)
{
if(ch1[i]!=ch2[i])
{
printf(Compare error !/n);

return(st);
}
}
printf(OK !/n);
return(0);
)

原创粉丝点击