以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [求助]还是关于读卡写卡的问题求助!  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=30647)

--  作者:fjt4444
--  发布时间: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
--  发布时间: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;
        }
    }
}


--  作者:狐狸爸爸
--  发布时间:2013/4/1 16:35:00
--  
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports System.Text
Imports System.Runtime.InteropServices
\'\'\' <summary>
\'\'\' DUKa 的摘要说明
\'\'\' </summary>
Public Class DUKa
    \'
    \' TODO: 在此处添加构造函数逻辑
    \'
    Public Sub New()
    End Sub
#Region "对USB接口的使用(PHILIPH卡)"
    <DllImport("dcrf32.dll")> _
    Public Shared Function dc_init(ByVal port As Int32, ByVal baud As Long) As Integer
    End Function
    \'初试化
    <DllImport("dcrf32.dll")> _
    Public Shared Function dc_exit(ByVal icdev As Integer) As Short
    End Function
    <DllImport("dcrf32.dll")> _
    Public Shared Function dc_reset(ByVal icdev As Integer, ByVal sec As UInteger) As Short
    End Function
    <DllImport("dcrf32.dll")> _
    Public Shared Function dc_request(ByVal icdev As Integer, ByVal _Mode As Char, ByRef TagType As UInteger) As Short
    End Function
    <DllImport("dcrf32.dll")> _
    Public Shared Function dc_card(ByVal icdev As Integer, ByVal _Mode As Char, ByRef Snr As ULong) As Short
    End Function
    <DllImport("dcrf32.dll")> _
    Public Shared Function dc_halt(ByVal icdev As Integer) As Short
    End Function
    <DllImport("dcrf32.dll")> _
    Public Shared Function dc_anticoll(ByVal icdev As Integer, ByVal _Bcnt As Char, ByRef IcCardNo As ULong) As Short
    End Function
    <DllImport("dcrf32.dll")> _
    Public Shared Function dc_beep(ByVal icdev As Integer, ByVal _Msec As UInteger) As Short
    End Function
    <DllImport("dcrf32.dll")> _
    Public Shared Function dc_authentication(ByVal icdev As Integer, ByVal _Mode As Integer, ByVal _SecNr As Integer) As Short
    End Function
    <DllImport("dcrf32.dll")> _
    Public Shared Function dc_load_key(ByVal icdev As Integer, ByVal mode As Integer, ByVal secnr As Integer, <[In]()> ByVal nkey As Byte()) As Short
    End Function
    \'密码装载到读写模块中
    <DllImport("dcrf32.dll")> _
    Public Shared Function dc_load_key_hex(ByVal icdev As Integer, ByVal mode As Integer, ByVal secnr As Integer, ByVal nkey As String) As Short
    End Function
    \'密码装载到读写模块中
    <DllImport("dcrf32.dll")> _
    Public Shared Function dc_write(ByVal icdev As Integer, ByVal adr As Integer, <[In]()> ByVal sdata As Byte()) As Short
    End Function
    \'向卡中写入数据
    <DllImport("dcrf32.dll")> _
    Public Shared Function dc_write(ByVal icdev As Integer, ByVal adr As Integer, <[In]()> ByVal sdata As String) As Short
    End Function
    \'向卡中写入数据
    <DllImport("dcrf32.dll")> _
    Public Shared Function dc_write_hex(ByVal icdev As Integer, ByVal adr As Integer, <[In]()> ByVal sdata As String) As Short
    End Function
    \'向卡中写入数据(转换为16进制)
    <DllImport("dcrf32.dll")> _
    Public Shared Function dc_read(ByVal icdev As Integer, ByVal adr As Integer, <Out()> ByVal sdata As Byte()) As Short
    End Function
    <DllImport("dcrf32.dll")> _
    Public Shared Function dc_read(ByVal icdev As Integer, ByVal adr As Integer, <MarshalAs(UnmanagedType.LPStr)> ByVal sdata As StringBuilder) As Short
    End Function
    \'从卡中读数据
    <DllImport("dcrf32.dll")> _
    Public Shared Function dc_read_hex(ByVal icdev As Integer, ByVal adr As Integer, <MarshalAs(UnmanagedType.LPStr)> ByVal sdata As StringBuilder) As Short
    End Function
    \'从卡中读数据(转换为16进制)
    <DllImport("dcrf32.dll")> _
    Public Shared Function a_hex(ByVal oldValue As String, ByRef newValue As String, ByVal len As Int16) As Integer
    End Function
    \'普通字符转换成十六进制字符
    <DllImport("dcrf32.dll")> _
    Public Shared Sub hex_a(ByRef oldValue As String, ByRef newValue As String, ByVal len As Integer)
    End Sub
    \'十六进制字符转换成普通字符
#End Region
    Public Shared Function WriteIn(ByVal str As String) As Boolean
        Dim _icdev As Int32 = -1
        Try
            If _icdev < 0 Then
                _icdev = dc_init(100, 115200)
            End If
            Dim st As Integer
            \'1.初始化
            If _icdev > 0 Then
            Else
                dc_exit(_icdev)
                Return False
            End If
            \'蜂鸣
            st = dc_beep(_icdev, 10)
            If st = 0 Then
            Else
                dc_exit(_icdev)
                Return False
            End If
            Dim icCardNo As ULong = 0
            Dim tt As Char = ChrW(0)
            Dim ss As UInteger = 0
            st = dc_reset(_icdev, ss)
            st = dc_card(_icdev, tt, icCardNo)
            If icCardNo <> 0 Then
            Else
                dc_exit(_icdev)
                Return False
            End If
            Dim hexkey As Byte() = New Byte(5) {&HFF, &HFF, &HFF, &HFF, &HFF, &HFF}

            st = dc_load_key(_icdev, 0, 1, hexkey)
            \'核对密码
            Dim sector As Integer = 1
            st = dc_authentication(_icdev, 0, sector)
            If st = 0 Then
            Else
                dc_exit(_icdev)
                Return False
            End If
            Dim address As Integer = 1
            Dim data32 As Byte()
            data32 = New Byte(15) {&H6A, &HC2, &H92, &HFA, &HA1, &H31, _
             &H5B, &H4D, &H6A, &HC2, &H92, &HFA, _
             &HA1, &H31, &H5B, &H4D}
            \'= "12345678901234561234567890123456";
            Dim data32_hex As String = "".PadLeft(32, " "c)
            address = sector * 4 + 2
            \'写卡操作
            data32_hex = str
            st = dc_write(_icdev, address, data32_hex)
            If st = 0 Then
                dc_exit(_icdev)
            Else
                dc_exit(_icdev)
                Return False
            End If
            dc_exit(_icdev)
            Return True
        Catch
            dc_exit(_icdev)
            Return False
        End Try
    End Function
--  作者:狐狸爸爸
--  发布时间:2013/4/1 16:35:00
--  

    Public Shared Function ReadCard(ByVal str As String, ByRef CardNo As String) As Boolean
        CardNo = ""
        Dim _icdev As Int32 = -1
        Try
            If _icdev < 0 Then
                _icdev = dc_init(100, 115200)
            End If
            Dim st As Integer
            \'1.初始化
            If _icdev > 0 Then
            Else
                dc_exit(_icdev)
                Return False
            End If
            \'蜂鸣
            st = dc_beep(_icdev, 10)
            If st = 0 Then
            Else
                dc_exit(_icdev)
                Return False
            End If
            Dim icCardNo As ULong = 0
            Dim tt As Char = ChrW(0)
            Dim ss As UInteger = 0
            st = dc_reset(_icdev, ss)
            st = dc_card(_icdev, tt, icCardNo)
            If icCardNo <> 0 Then
            Else
                dc_exit(_icdev)
                Return False
            End If
            Dim hexkey As Byte() = New Byte(5) {&HFF, &HFF, &HFF, &HFF, &HFF, &HFF}
            st = dc_load_key(_icdev, 0, 1, hexkey)
            \'核对密码
            Dim sector As Integer = 1
            st = dc_authentication(_icdev, 0, sector)
            If st = 0 Then
            Else
                dc_exit(_icdev)
                Return False
            End If
            Dim temp As New StringBuilder(64)
            Dim temp1 As New StringBuilder(64)
            Dim address As Integer = 1
            Dim data32 As Byte()
            data32 = New Byte(15) {&H6A, &HC2, &H92, &HFA, &HA1, &H31, _
             &H5B, &H4D, &H6A, &HC2, &H92, &HFA, _
             &HA1, &H31, &H5B, &H4D}
            \'= "12345678901234561234567890123456";
            Dim data32_hex As String = "".PadLeft(32, " "c)
            address = sector * 4 + 2
            Dim databuff32 As String = String.Empty
            st = dc_read(_icdev, address, temp1)
            If st = 0 Then
                CardNo = temp1.ToString()
            End If
            dc_exit(_icdev)
            Return True
        Catch
            dc_exit(_icdev)
            Return False
        End Try
    End Function
End Class


--  作者:狐狸爸爸
--  发布时间:2013/4/1 16:36:00
--  

我帮你转换为vb.net的语法,你调整一下。

一个帖子发不下,分成两个了