Commit f51fe40e LN

rfid代码更新

1 个父辈 a07426ab
...@@ -82,7 +82,6 @@ ...@@ -82,7 +82,6 @@
<Compile Include="bean\StoreStep.cs"> <Compile Include="bean\StoreStep.cs">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Include="RFID\RFID.cs" />
<Compile Include="RFID\RFIDManager.cs" /> <Compile Include="RFID\RFIDManager.cs" />
<Compile Include="RFID\RFIDReader.cs" /> <Compile Include="RFID\RFIDReader.cs" />
</ItemGroup> </ItemGroup>
......
...@@ -17,7 +17,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -17,7 +17,7 @@ namespace OnlineStore.DeviceLibrary
{ {
try try
{ {
RFIDReader.Open(null, iparray); RFIDReader.Open( iparray);
}catch(Exception ex) }catch(Exception ex)
{ {
LogUtil.error("Open 出错:" + ex.ToString()); LogUtil.error("Open 出错:" + ex.ToString());
...@@ -31,14 +31,8 @@ namespace OnlineStore.DeviceLibrary ...@@ -31,14 +31,8 @@ namespace OnlineStore.DeviceLibrary
if (String.IsNullOrEmpty(ip).Equals(false)) if (String.IsNullOrEmpty(ip).Equals(false))
{ {
byte[] bdata = null; byte[] bdata = null;
if (isClear) bdata = RFIDReader.Read(ip, isClear);
{
bdata = RFIDReader.ReadAndClear(ip);
}
else
{
bdata = RFIDReader.Read(ip);
}
if (bdata == null) if (bdata == null)
{ {
LogUtil.error("RFID [ " + ip + " ] 读到数据=null"); LogUtil.error("RFID [ " + ip + " ] 读到数据=null");
......
...@@ -8,73 +8,368 @@ using static Asa.RFID.RFID; ...@@ -8,73 +8,368 @@ using static Asa.RFID.RFID;
namespace Asa.RFID namespace Asa.RFID
{ {
public class RFIDReader public class RFID
{ {
public static readonly ILog LOGGER = LogManager.GetLogger("TheRFID"); public static readonly ILog LOGGER = LogManager.GetLogger("TheRFID");
private static Dictionary<string, RFID> rfidMap = new Dictionary<string, RFID>(); // private System.Threading.Thread tScan;
private bool loop;
private string IP = "";
private byte addr;
private int portIndex;
private bool IsConnect = false;
private byte[] lastData = new byte[8];
internal byte[] _buff; //8字节缓存
private byte[] _uid; //卡片ID
public bool IsExist = false;
public int ErrCode = 0;
/// <summary> /// <summary>
/// 打开所有 /// 接收事件
/// </summary> /// </summary>
/// <param name="ipArr"></param>
/// <param name="OnReceive"></param>
/// <param name="ip"></param> /// <param name="ip"></param>
public static void Open(Received_Event OnReceive, params string[] ipArr) /// <param name="buff"></param>
public delegate void Received_Event(string ip, byte[] buff);
/// <summary>
/// 接收数据
/// </summary>
public event Received_Event Received;
public RFID(string ip)
{ {
foreach(var ip in ipArr) this.IP = ip;
}
public int Close()
{ {
if (rfidMap.ContainsKey(ip)) try
{
loop = false;
IsConnect = false;
try
{ {
rfidMap[ip].StartAutoScan(OnReceive); // tScan.Abort();
} }
else catch (Exception ex)
{ {
RFID rfid = new RFID(ip); LOGGER.Info("Close tScan[" + IP + "] error :" + ex.ToString());
rfid.StartAutoScan(OnReceive); }
rfidMap.Add(ip, rfid); int ErrCode = ReaderA.StaticClassReaderA.CloseNetPort(portIndex);
LOGGER.Info("Close RFID [" + IP + "]:" + ErrCode);
return ErrCode;
} }
catch (Exception e)
{
LOGGER.Error("Close RFID[" + IP + "] has error", e);
} }
return -1;
}
public int Open()
{
if (IsConnect)
{
LOGGER.Warn(" RFID [" + IP + "] is already connected, no need to start again");
return 0;
}
//IP合法
string pattern = @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$";
bool rtn = System.Text.RegularExpressions.Regex.IsMatch(IP, pattern);
if (!rtn)
{
LOGGER.Error(IP + " is error");
return -1;
}
string[] arr = IP.Split('.');
if (arr.Length != 4)
{
LOGGER.Error(IP + " length is not 4");
return -1;
}
addr = Convert.ToByte(arr[3]);
int port = 6000 + Convert.ToInt32(arr[3]);
portIndex = 0;
int ErrCode = ReaderA.StaticClassReaderA.OpenNetPort(port, IP, ref addr, ref portIndex);
LOGGER.Info("Connect [" + IP + "] :" + ErrCode);
// 在 OpenNetPort 后面使用
System.Threading.Thread.Sleep(100);
OpenCloseRF(false);
System.Threading.Thread.Sleep(100);
OpenCloseRF(true);
return ErrCode;
}
public void FindMode()
{
loop = true;
//tScan = new System.Threading.Thread(new System.Threading.ThreadStart(Scan));
//tScan.Start();
Task.Factory.StartNew(Scan);
}
private void Scan()
{
while (loop)
{
bool rtn = FindRFID();
if (rtn)
{
int result = ReadRFID();
if (result.Equals(0))
{
byte[] bb = new byte[] { _buff[0], _buff[1], _buff[2] };
string dataStr = byteToStr(bb, bb.Length);
LOGGER.Info(IP + " Scan Data:" + dataStr);
Received?.Invoke(IP, bb);
}
else if (result.Equals(14))
{
LOGGER.Error(IP + " ReadRFID 返回 " + result + ", 需要重连,调用close ,等待100后重连");
Close();
System.Threading.Thread.Sleep(100);
Open();
FindMode();
break;
}
}
System.Threading.Thread.Sleep(100);
}
}
private void OpenCloseRF(bool rtn)
{
if (rtn)
ErrCode = ReaderA.StaticClassReaderA.OpenRf(ref addr, portIndex);
else
ErrCode = ReaderA.StaticClassReaderA.CloseRf(ref addr, portIndex);
} }
/// <summary> /// <summary>
/// 获取最后读到的标签并清理 /// 查找电子标签,扫描模式不能使用
/// </summary> /// </summary>
/// <param name="ip"></param>
/// <returns></returns> /// <returns></returns>
public static byte[] ReadAndClear(string ip) public bool FindRFID()
{
try
{
//0 不带AFI
//1 带AFI
//2 不带AFI,继续查询
//3 带AFI,继续查询
//6 不带AFI,新的查询
//7 带AFI,新的查询
byte state = 0;
//Select模式需要AFI
byte AFI = 0;
//输出,1字节DSFID,8字节UID
byte[] DSFIDAndUID = new byte[9];
//输出,标签数量
byte cardNumber = 0;
if (_uid == null)
{
_uid = new byte[8];
}
ErrCode = ReaderA.StaticClassReaderA.Inventory(ref addr, ref state, ref AFI, DSFIDAndUID, ref cardNumber, portIndex);
if (ErrCode == 0)
{
//查询时间
//fCmdRet = StaticClassReaderA.GetInventoryTime(ref n_time, portIndex);
Array.Copy(DSFIDAndUID, 1, _uid, 0, 8);
IsExist = true;
return true;
}
else
{
for (int i = 0; i < _uid.Length; i++)
_uid[i] = 0;
IsExist = false;
}
}
catch (Exception ex)
{
LOGGER.Error(IP + "FindRFID Error:" + ex.ToString());
}
return false;
}
//public byte[] Read(bool isNeedFind = false)
//{
// if (isNeedFind)
// {
// FindRFID();
// }
// if (IsExist)
// {
// ReadRFID();
// return _buff;
// }
// else
// {
// LOGGER.Info(IP + " Read: IsExist=false ");
// }
// return null;
//}
private int ReadRFID()
{
try
{
//0 不带AFI
//1 带AFI
//2 不带AFI,继续查询
//3 带AFI,继续查询
//6 不带AFI,新的查询
//7 带AFI,新的查询
byte state = 0;
//起始块号
byte blockNum = 0;
//块数量
byte blockCount = 2;
//输出,块内安全信息,长度为BlockCount个字节
byte[] blockSecStatus = new byte[blockCount];
//输出,块内数据信息,长度为块的大小(4或8字节)乘以BlockCount个字节
byte[] data = new byte[4 * blockCount];
//错误代码
byte errorCode = 0;
if (_buff == null)
{
_buff = new byte[8];
}
ErrCode = ReaderA.StaticClassReaderA.ReadMultipleBlock(ref addr, ref state, _uid, blockNum, blockCount, blockSecStatus, data, ref errorCode, portIndex);
if (ErrCode == 0)
{
Array.Copy(data, 0, _buff, 0, data.Length);
string dataStr = byteToStr(data, data.Length);
LOGGER.Info(IP + " ReadMultipleBlock: Length: " + data.Length + " Data:" + dataStr);
}
else if (ErrCode == 14)
{
LOGGER.Error(IP + " ReadMultipleBlock: ErrCode: " + ErrCode + ",需要重连");
}
else
{
LOGGER.Info(IP + " ReadMultipleBlock: ErrCode: " + ErrCode);
}
return ErrCode;
}
catch (Exception ex)
{
LOGGER.Error(IP + "ReadRFID Error:" + ex.ToString());
}
return -1;
}
private string byteToStr(byte[] data, int len)
{
string s = "";
for (int i = 0; i < len; i++)
{
s += data[i].ToString("X2") + " ";
}
return s;
}
}
public class RFIDReader
{
public static readonly ILog LOGGER = LogManager.GetLogger("TheRFID");
private static Dictionary<string, RFID> rfidMap = new Dictionary<string, RFID>();
private static Dictionary<string, byte[]> LastRfidData = new Dictionary<string, byte[]>();
public static void Open(params string[] ipArr)
{
try
{
foreach (var ip in ipArr)
{ {
if (rfidMap.ContainsKey(ip)) if (rfidMap.ContainsKey(ip))
{ {
return rfidMap[ip].ReadAndClear(); rfidMap[ip].Open();
rfidMap[ip].FindMode();
}
else
{
RFID rfid = new RFID(ip);
rfid.Received += Rfid_Received;
rfid.Open();
rfid.FindMode();
rfidMap.Add(ip, rfid);
}
}
}
catch (Exception ex)
{
LOGGER.Error("RFIDReader Open Error:" + ex.ToString());
} }
return null;
} }
/// <summary> private static void Rfid_Received(string ip, byte[] buff)
/// 获取最后读到的标签 {
/// </summary> if (buff == null)
/// <param name="ip"></param> {
/// <returns></returns> return;
public static byte[] Read(string ip) }
string dataStr = byteToStr(buff, buff.Length);
LOGGER.Info(ip + " Rfid_Received 保存数据:" + dataStr);
if (LastRfidData.ContainsKey(ip))
{
LastRfidData[ip] = buff;
}
else
{
LastRfidData.Add(ip, buff);
}
}
//public static bool FindRFID(string ip)
//{
// if (rfidMap.ContainsKey(ip))
// {
// return rfidMap[ip].FindRFID();
// }
// return false;
//}
public static byte[] Read(string ip, bool isClear = false)
{ {
if (rfidMap.ContainsKey(ip)) if (rfidMap.ContainsKey(ip))
{ {
return rfidMap[ip].Read(); if (LastRfidData.ContainsKey(ip))
{
byte[] data = LastRfidData[ip];
if (isClear)
{
LastRfidData.Remove(ip);
}
return data;
}
// return rfidMap[ip].Read(isNeedFind);
} }
return null; return null;
} }
/// <summary>
/// 关闭所有
/// </summary>
public static void CloseAll() public static void CloseAll()
{ {
foreach(var rfid in rfidMap.Values) foreach (RFID rfid in rfidMap.Values)
{
rfid.Close();
}
}
private static string byteToStr(byte[] data, int len)
{
string s = "";
for (int i = 0; i < len; i++)
{ {
rfid.StopAutoScan(); s += data[i].ToString("X2") + " ";
} }
return s;
} }
} }
} }
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!