//财源广进微信会员卡管理系统
internal partial class V6 : IReader
{
/// <summary>
/// 自动获取端口号
/// </summary>
/// <returns></returns>
public int GetPort()
{
for (int i = 0; i < 10; i++)
{
int s = ic_init(i, 9600);
if (s > 0)
{
ic_exit(s); ic_exit(0);
return i;
}
}
return -1;
}
/// <summary>
/// 连接设备
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="success"></param>
/// <param name="dev"></param>
/// <returns></returns>
public T Connect<T>(Device dev, Func<int, T> success) where T : Result, new()
{
if (dev.Port < 0)
return new T { ErrMsg = $"读卡器连接失败,端口{dev.Port}错误" };
int icdev = ic_init(dev.Port, dev.Rate);
if (icdev == -149)
return new T { ErrMsg = $"读卡器连接失败,端口{dev.Port + 1}占用" };
if (icdev < 0)
return new T { ErrMsg = "读卡器连接失败,检查是否设置或者连接好" };
//蜂鸣提示
//dv_beep(icdev, 10);
short status = 0;
get_status(icdev, ref status);
T res;
if (status == 0)
{
res = new T { ErrMsg = "请注意:未插入卡片" };
}
else
{
//验证密码是否正确
var pwd = Encoding.Default.GetBytes(dev.newpwd);
var i = csc_4442(icdev, 3, ref pwd[0]);
if (i != 0)
{
res = new T { ErrMsg = "请注意:密码验证失败" };
}
else
{
//res = readData(icdev) as T;
res = success.Invoke(icdev);
}
}
//关闭设备
ic_exit(icdev);
ic_exit(0);
return res;
}
/// <summary>
/// 读卡
/// </summary>
/// <param name="dev"></param>
/// <returns></returns>
public Card Read(Device dev)
{
return Connect(dev, icdev => IC_readData(icdev));
}
/// <summary>
/// 写卡
/// </summary>
/// <param name="dev"></param>
/// <param name="data"></param>
/// <param name="WriteType"></param>
/// <returns></returns>
public Result Write(Device dev, Card data, WriteType WriteType)
{
return Connect(dev, icdev =>
{
//写卡操作
if (WriteType == WriteType.验证写卡)
{
var card = IC_readData(icdev);
if (card.IsSuccess == false)
{
return card;
}
else if (card.CardId != data.CardId || card.CardNumber != data.CardNumber)
{
return new Card { ErrMsg = "卡号与芯片不匹配" };
}
else
{
//正式写入卡内数据
return new Result { ErrMsg = "写入芯片数据", IsSuccess = IC_writeData(icdev, data) };
}
}
else if (WriteType == WriteType.新卡写卡)
{
return new Result { ErrMsg = "写入芯片数据", IsSuccess = IC_writeData(icdev, data) };
}
return new Result { ErrMsg = "暂未支持此功能" };
});
}
/// <summary>
/// 根据对象写入芯片数据
/// </summary>
/// <param name="icdev"></param>
/// <param name="data"></param>
/// <returns></returns>
private bool IC_writeData(int icdev, Card data)
{
byte[] buffer = new byte[223];
int START = 32;
Encoding.Default.GetBytes(data.CardId).CopyTo(buffer, 位置- START);
var i = swr_4442(icdev, 32, buffer.Length, ref buffer[0]);
return (i == 0);
}
/// <summary>
/// 获取字符串
/// </summary>
/// <param name="buffer"></param>
/// <param name="start">开始</param>
/// <param name="len">长度</param>
/// <returns></returns>
private string GetStrByBuffer(byte[] buffer, int start, int len)
{
byte[] data = new byte[len];
Buffer.BlockCopy(buffer, start - 32, data, 0, len);
bool zero = false;
for (int i = 0; i < data.Length; i++)
{
if (data[i] == '\0') zero = true;
if (zero) data[i] = (byte)'\0';
}
var result = Encoding.Default.GetString(data.ToArray()).Trim();
result = result.Replace("\0", "");
return result;
}
/// <summary>
/// 读取芯片数据
/// </summary>
/// <param name="icdev"></param>
/// <returns></returns>
private Card IC_readData(int icdev)
{
Card res = new Card();
//一次读取所有数据
var buffer = new byte[223];
srd_4442(icdev, 32, buffer.Length, ref buffer[0]);
var s1 = GetStrByBuffer(buffer, XX, 20);
var s2 = GetStrByBuffer(buffer, XX, 20);
if (s1.Length == 0 || s2.Length == 0)
{
res = new Card { ErrMsg = "芯片读取失败,建议重新读卡" };
}
else
{
res = new Card
{
CardId = GetStrByBuffer(buffer, 位置, 10),
Balance = double.Parse(s1),
Point = double.Parse(s2),
IsSuccess = true,
};
//writeData(icdev, res, buffer);
}
return res;
}
}
public class Device
{
public int Rate { get; set; } = 9600;
public int Port { get; set; }
public string Model { get; set; }
public string newpwd { get; set; };
public string oldpwd { get; set; };
}
internal class Result
{
public bool IsSuccess { get; set; }
public string ErrMsg { get; set; }
}