通用会员卡设计

类关系草图

设计目的:

设计一个可以作为各种消费系统通用的会员卡管理子系统;能适应多种介质的会员卡和多种读卡器;涵盖诸如计次、储值、积分等基本业务功能;子系统能自成系统,同时具备通用系统的开放性,可扩展和可重用。

正式类图

已经实现明华IC多种卡的支持

程序部分代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DataCool.BLL;
using DataCool.BLL.CardBLL;

namespace MemberWinForm
{
    public partial class MWICDemoMain : Form
    {
        MWICDevice demoDevice = new MWICDevice();
        public MWICDemoMain()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {           
            if (demoDevice.InitDevice())
            {
                demoDevice.DeviceBeep();
                tbxDllVersion.Text = demoDevice.GetDeviceVersion();
                tbxComPort.Text = demoDevice.ComPortName;
                tbMemo.Text = "";
                tbMemo.Text = demoDevice.GetDeviceInfor().Trim();
                tbxCardInfor.Text = demoDevice.ChkCard();
                demoDevice.EndSession();
            }           
        }

        private void btWriteDevice_Click(object sender, EventArgs e)
        {
            demoDevice.InitDevice();
            demoDevice.SetDeviceInfor(tbMemo.Text.Trim());
            tbMemo.Text = demoDevice.GetDeviceInfor().Trim();
            demoDevice.EndSession();
        }

        private void btInitPort_Click(object sender, EventArgs e)
        {
            if (System.IO.File.Exists("MWICDeviceCfg.XML"))
            {
                System.IO.File.Delete("MWICDeviceCfg.XML");
                demoDevice = null;
                demoDevice = new MWICDevice();
                if (demoDevice.InitDevice())
                {
                    tbxComPort.Text = "未连接";
                    demoDevice.DeviceBeep();                 
                    tbxComPort.Text = demoDevice.ComPortName;
                    demoDevice.EndSession();
                }
            }
        }

        private void btChkCard_Click(object sender, EventArgs e)
        {
            demoDevice.InitDevice();
            tbxCardInfor.Text = demoDevice.ChkCard();
            demoDevice.EndSession();           
        }

        private void btCSCPwd_Click(object sender, EventArgs e)
        {
            bool result = false;
            StringBuilder sb;
            DeviceICFactory deviceICFactory = new DeviceICFactory(tbxCardInfor.Text.Trim());
            MWICDevice deviceBLL = deviceICFactory.GetICDeviceBLL();
            result = deviceBLL.cardOperateIfc.CSCCompanyPassword(tbxCompanyPwd.Text.Trim(), out sb);
            if (sb != null) lbAlterInfor.Text = sb.ToString();
            if (result)
               tbxCompanyPwd.Text = "核对密码正确!";
        }

        private void btWritePwd_Click(object sender, EventArgs e)
        {
            bool result = false;
            StringBuilder sb;
            DeviceICFactory deviceICFactory = new DeviceICFactory(tbxCardInfor.Text.Trim());
            MWICDevice deviceBLL = deviceICFactory.GetICDeviceBLL();
            result = deviceBLL.cardOperateIfc.SetUserCompanyCardPassword(tbxCompanyPwd.Text.Trim(), out sb);
            if (sb != null) lbAlterInfor.Text = sb.ToString();
            if (result)
                tbxCompanyPwd.Text = "修改密码正确!";
        }

        private void btReadData_Click(object sender, EventArgs e)
        {
            bool result = false;
            StringBuilder sb;
            string Msg="";
            DeviceICFactory deviceICFactory = new DeviceICFactory(tbxCardInfor.Text.Trim());
            MWICDevice deviceBLL = deviceICFactory.GetICDeviceBLL();
            result = deviceBLL.cardOperateIfc.ReadCardData((short)numOffset.Value,(short)numLength.Value,out Msg,out sb);
            if (sb != null) lbAlterInfor.Text = sb.ToString();
            if (result)
            {
                tbxDataInput.Text = Msg;
                lbAlterInfor.Text = "读卡操作成功!";
            }
        }

        private void btWriteData_Click(object sender, EventArgs e)
        {
            bool result = false;
            StringBuilder sb;
            DeviceICFactory deviceICFactory = new DeviceICFactory(tbxCardInfor.Text.Trim());
            MWICDevice deviceBLL = deviceICFactory.GetICDeviceBLL();
            result = deviceBLL.cardOperateIfc.WriteCardData((short)numOffset.Value, tbxDataInput.Text.Trim(), out sb);
            if (sb != null) lbAlterInfor.Text = sb.ToString();
            if (result)
            {
                lbAlterInfor.Text = "写卡操作成功!";
            }
        }
    }
}

-------------------------------

using System;
using System.Collections.Generic;
using System.Text;

namespace DataCool.BLL.CardBLL
{
    public class DeviceICFactory
    {
        private string _CardTypeFlag = string.Empty;
        public DeviceICFactory(string cardTypeFlag)
        {
            _CardTypeFlag = cardTypeFlag;
        }

        public MWICDevice GetICDeviceBLL()
        {
            MWICDevice icDeviceBLL;
            switch (_CardTypeFlag)
            {
                case "SLE4442":
                    icDeviceBLL = new MWICDevice(new MWICDevice4442());
                    break;
                case "SLE4432":             
                    icDeviceBLL = new MWICDevice(new MWICDevice4432());
                    break;
                case "SLE4404":               
                    icDeviceBLL = new MWICDevice(new MWICDevice4404());
                    break;
                case "SLE4406":                   
                    icDeviceBLL = new MWICDevice(new MWICDevice4406());
                    break;
                case "SLE4428":
                    icDeviceBLL = new MWICDevice(new MWICDevice4428());                
                    break;               
                case "SLE4418":                  
                    icDeviceBLL = new MWICDevice(new MWICDevice4418());  
                    break;
                default:
                    icDeviceBLL = new MWICDevice();
                    break;           
            }
            return icDeviceBLL;
        }
    }
}
---------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace DataCool.BLL.CardBLL
{
    /// <summary>
    /// SLE4442卡操作及设备操作
    /// </summary>
    public class MWICDevice4442:MWICDevice, DataCool.BLL.CardBLL.ICardOperate
    {
        [DllImport("Mwic_32.dll", EntryPoint = "swr_4442", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        //说明:    向指定地址写数据
        //调用:    icdev:    通讯设备标识符  offset:   偏移地址,其值范围0~255
        //          len:      字符串长度,其值范围1~256 w_string: 写入数据  [MarshalAs(UnmanagedType.LPArray)] byte[] StringBuilder
        //返回:     <0   错误 =0  正确
        public static extern Int16 swr_4442(int icdev, Int16 offset, Int16 len, [MarshalAs(UnmanagedType.LPArray)]byte[] w_string);

        [DllImport("Mwic_32.dll", EntryPoint = "srd_4442", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        //说明:    从指定地址读数据 
        //     调用:    icdev:    通讯设备标识符 offset:   偏移地址,其值范围0~255
        //    len:      字符串长度,其值范围1~256    r_string: 读出数据所存放地址指针
        //返回:     <>0   错误  =0 正确
        public static extern Int16 srd_4442(int icdev, Int16 offset, Int16 len, [MarshalAs(UnmanagedType.LPArray)]byte[] r_string);


        [DllImport("Mwic_32.dll", EntryPoint = "chk_4442", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        // 说明:    检查卡型是否正确 
        //调用:    icdev:   通讯设备标识符
        //返回:     <0   错误   =0   正确
        public static extern Int16 chk_4442(int icdev);


        [DllImport("Mwic_32.dll", EntryPoint = "csc_4442", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.Winapi)]
        //说明:    核对卡密码 
        //调用:    icdev:    通讯设备标识符  len:      密码个数,其值为3 p_string: 密码字符串指针
        //返回:     <0   错误    =0   密码正确
        public static extern Int16 csc_4442(int icdev, Int16 len, [MarshalAs(UnmanagedType.LPArray)]byte[] p_string);


        [DllImport("Mwic_32.dll", EntryPoint = "wsc_4442", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        //说明:    改写卡密码
        //调用:    icdev:    通讯设备标识符 len: 密码个数,其值为3 p_string: 新密码地址指针
        //返回:    <0   错误   =0   正确
        public static extern Int16 wsc_4442(int icdev, Int16 len, [MarshalAs(UnmanagedType.LPArray)]byte[] p_string);

        [DllImport("Mwic_32.dll", EntryPoint = "rsc_4442", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        //说明:    读出卡密码 
        //调用:    icdev:    通讯设备标识符  len:      密码个数,其值为3  p_string: 存放密码地址指针
        // 返回:    <>0   错误   =0   正确 
        public static extern Int16 rsc_4442(int icdev, Int16 len, [MarshalAs(UnmanagedType.LPArray)]byte[] p_string);

        [DllImport("Mwic_32.dll", EntryPoint = "rsct_4442", SetLastError = true,
             CharSet = CharSet.Auto, ExactSpelling = false,
             CallingConvention = CallingConvention.StdCall)]
        //说明:    读出密码错误计数器值
        //调用:    icdev:    通讯设备标识符 counter:  密码错误记数值存放指针
        //返回:     <0   错误 >=0   正确
        public static extern Int16 rsct_4442(int icdev, out byte counter);

        private string _UserCompanyPassWord = "ffffff";
        /// <summary>
        /// 商家卡密码,出厂是ffffff
        /// </summary>
        public string UserCompanyPassWord
        {
            get { return _UserCompanyPassWord; }
            set { _UserCompanyPassWord = value; }
        }

        public MWICDevice4442()
        {
            //
        }

        /// <summary>
        /// 向卡写数据
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="BufferStr"></param>
        /// <param name="errorSB"></param>
        /// <returns></returns>
        public bool WriteCardData(short offset,string BufferStr,out StringBuilder errorSB)
        {
            errorSB = new StringBuilder();
            if (!InitDevice())
            {
                errorSB.AppendLine("打开读卡设备对应的计算机端口失败,请检测参数设置后重试!");
                return false;
            }
            else
            {
                bool IsWriteOK = false;
                byte[] key1 = new byte[20];
                byte[] key2 = new byte[3];               
                key1 = Encoding.ASCII.GetBytes(_UserCompanyPassWord);
                short result = MWIC.asc_hex(key1, key2, 6);
                result = csc_4442(icdev, 3, key2);
                if (result != 0) errorSB.AppendLine(string.Format("核对Hex密码'{0}'出错!", key2.ToString()));
                byte[] status_array = new byte[8];
                MWIC.get_status(icdev, status_array);
                if (status_array[0] == 0)
                {
                    DeviceBeep();
                    errorSB.AppendLine("读卡设备未检测到卡,请放置好后重试!");
                    DeviceBeep();
                    return false;
                }
                byte[] buffer = Encoding.Default.GetBytes(BufferStr);
                result = swr_4442(icdev, offset, (short)buffer.Length, buffer);
                IsWriteOK = (result==0);
                EndSession();
                return IsWriteOK;
            }
        }

        /// <summary>
        /// 读卡数据
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="len"></param>
        /// <param name="ResultStr"></param>
        /// <param name="errorSB"></param>
        /// <returns></returns>
        public bool ReadCardData(short offset,short len,out string ResultStr, out StringBuilder errorSB)
        {
            errorSB = new StringBuilder();
            if (!InitDevice())
            {
                errorSB.AppendLine("打开读卡设备对应的计算机端口失败,请检测参数设置后重试!");
                ResultStr = string.Empty;               
                return false;
            }
            else
            {
                byte[] status_array = new byte[8];
                MWIC.get_status(icdev, status_array);
                if (status_array[0] == 0)
                {
                    DeviceBeep();
                    errorSB.AppendLine("读卡设备未检测到卡,请放置好后重试!");
                    DeviceBeep();
                    ResultStr = string.Empty;
                    return false;
                }
                byte[] buffer = new byte[len * 2];
                short result = srd_4442(icdev, offset, len, buffer);
                ResultStr = Encoding.Default.GetString(buffer);
                EndSession();
                return (result == 0);
            }
        }

        /// <summary>
        /// 设置商户密码
        /// </summary>
        /// <param name="Password"></param>
        /// <param name="errorSB"></param>
        /// <returns></returns>
        public bool SetUserCompanyCardPassword(string Password, out StringBuilder errorSB)
        {
            errorSB = new StringBuilder();
            if (!InitDevice())
            {
                errorSB.AppendLine("打开读卡设备对应的计算机端口失败,请检测参数设置后重试!");
                return false;
            }
            else
            {
                bool IsWriteOK = false;
                byte[] key1 = new byte[32];
                byte[] key2 = new byte[3];
                key1 = Encoding.ASCII.GetBytes(_UserCompanyPassWord);
                short result = MWIC.asc_hex(key1, key2, 6);
                result = csc_4442(icdev, 3, key2);
                if (result != 0) errorSB.AppendLine(string.Format("核对Hex密码'{0}'出错!", key2.ToString()));
                byte[] status_array = new byte[8];
                MWIC.get_status(icdev, status_array);
                if (status_array[0] == 0)
                {
                    errorSB.AppendLine("读卡设备未检测到卡,请放置好后重试!");
                    return false;
                }
                key1 = Encoding.ASCII.GetBytes(Password);
                MWIC.asc_hex(key1, key2, 6);
                result = wsc_4442(icdev,3,key2);
                IsWriteOK = (result == 0);
                EndSession();
                return IsWriteOK;
            }
        }
        /// <summary>
        /// 核对用户密码
        /// </summary>
        /// <param name="Password"></param>
        /// <param name="errorSB"></param>
        /// <returns></returns>
        public bool CSCCompanyPassword(string Password, out StringBuilder errorSB)
        {
            errorSB = new StringBuilder();
            if (!InitDevice())
            {
                errorSB.AppendLine("打开读卡设备对应的计算机端口失败,请检测参数设置后重试!");
                return false;
            }
            else
            {
                bool IsWriteOK = false;
                byte[] key1 = new byte[32];
                byte[] key2 = new byte[3];
                key1 = Encoding.ASCII.GetBytes(Password);
                short result = MWIC.asc_hex(key1, key2, 6);
                result = csc_4442(icdev, 3, key2);
                if (result != 0) errorSB.AppendLine(string.Format("核对Hex密码'{0}'出错!", key2.ToString()));
                IsWriteOK = (result == 0);
                EndSession();
                return IsWriteOK;
            }
        }
    }
}
...

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"作者:" 数据酷软件工作室
"出处:" http://datacool.cnblogs.com
"专注于CMS(综合赋码系统),MES,WCS(智能仓储设备控制系统),WMS,商超,桑拿、餐饮、客房、足浴等行业收银系统的开发,15年+从业经验。因为专业,所以出色。"
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
原文地址:https://www.cnblogs.com/datacool/p/2014663.html