Foxtable(狐表)用户栏目专家坐堂 → [求助]还是关于读卡写卡的问题求助!


  共有9577人关注过本帖树形打印复制链接

主题:[求助]还是关于读卡写卡的问题求助!

帅哥哟,离线,有人找我吗?
fjt4444
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:118 积分:906 威望:0 精华:0 注册:2013/3/21 12:44:00
[求助]还是关于读卡写卡的问题求助!  发帖心情 Post By:2013/4/1 15:16:00 [显示全部帖子]

最近在做一个“车辆租赁管理系统”,遇到了一个超出了我能力范围的难题:写卡读卡。目标要求:通过“D8非接触式读写器”工具,1、对M1卡进行车辆信息录入(就两条信息,1,车牌号;2,租用开始时间);2、对M1卡上的车辆信息读取出来。目前我已经按照狐表帮助文档上那个“连接第三方设备”上的内容导入了相应的DLL文件,并写好了全局代码:

Declare Function add_s Lib "dcrf32.dll" (ByVal i%) As Integer

Declare Function dc_init Lib "dcrf32.dll" (ByVal port%, ByVal baud As Long) As Long
Declare Function dc_exit Lib "dcrf32.dll" (ByVal icdev As Long) As Integer
Declare Function dc_request Lib "dcrf32.dll" (ByVal icdev As Long, ByVal mode%, tagtype As Long) As Integer
Declare Function dc_anticoll Lib "dcrf32.dll" (ByVal icdev As Long, ByVal bcnt%, snr As Long) As Integer
Declare Function dc_select Lib "dcrf32.dll" (ByVal icdev As Long, ByVal snr As Long, size As Byte) As Integer
Declare Function dc_card Lib "dcrf32.dll" (ByVal icdev As Long, ByVal mode%, snr As Long) As Integer
Declare Function dc_load_key Lib "dcrf32.dll" (ByVal icdev As Long, ByVal mode%, ByVal secnr%, ByRef nkey As Byte) As Integer
Declare Function dc_load_key_hex Lib "dcrf32.dll" (ByVal icdev As Long, ByVal mode%, ByVal secnr%, ByVal nkey As String) As Integer
Declare Function dc_authentication Lib "dcrf32.dll" (ByVal icdev As Long, ByVal mode%, ByVal scenr%) As Integer
Declare Function dc_read Lib "dcrf32.dll" (ByVal icdev As Long, ByVal adr%, ByVal sdata$) As Integer
Declare Function dc_read_hex Lib "dcrf32.dll" (ByVal icdev As Long, ByVal adr%, ByVal sdata$) As Integer
Declare Function dc_write Lib "dcrf32.dll" (ByVal icdev As Long, ByVal adr%, ByVal sdata$) As Integer
Declare Function dc_write_hex Lib "dcrf32.dll" (ByVal icdev As Long, ByVal adr%, ByVal sdata$) As Integer
Declare Function dc_changeb3 Lib "dcrf32.dll" (ByVal adr As Long, ByVal secer As Integer, ByRef KeyA As Byte, ByVal B0 As Integer, ByVal B1 As Integer, ByVal B2 As Integer, ByVal B3 As Integer, ByVal Bk As Integer, ByRef KeyB As Byte) As Integer
Declare Function dc_read_allhex Lib "dcrf32.dll" (ByVal icdev As Long, ByVal sdata$) As Integer
Declare Function dc_write_allhex Lib "dcrf32.dll" (ByVal icdev As Long, ByVal sdata$) As Integer
Declare Function dc_set_autoflag Lib "dcrf32.dll" (ByVal icdev As Long, ByVal flag%) As Integer
Declare Function dc_check_writehex Lib "dcrf32.dll" (ByVal icdev As Long, ByVal cardid As Long, ByVal mode As Integer, ByVal adr%, ByVal sdata$) As Integer
Declare Function dc_beep Lib "dcrf32.dll" (ByVal icdev As Long, ByVal time1 As Integer) As Integer

 

但由于我所使用的第三方设备没有狐表帮助文档上那个设备强大,我这边还需要自己定义一个write方法和一个read方法。可能是因为我智商不够用,或者是因为狐表对于第三方设备的例子和讲解太少的原因,我一直无法完成这个系统,对于一个购买了开发版的用户来说,我很悲剧,遇到问题问客服QQ,得到的答案还是帮助文档链接!所以特此发帖求助。究竟我该怎么写这个write和read方法,在哪里写?怎么调用?!


 回到顶部
帅哥哟,离线,有人找我吗?
fjt4444
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:118 积分:906 威望:0 精华:0 注册:2013/3/21 12:44:00
不是这么简单的!  发帖心情 Post By:2013/4/1 15:39:00 [显示全部帖子]

我用的这个第三方设备需要初始化、蜂鸣、核对密码等!以下是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;
        }
    }
}


 回到顶部