using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading;
using
RfidService.Common;
namespace
Rfid
{
public
class RfidDevice
{
/// <summary>
/// 开启监听现成
/// </summary>
private
Thread _mThread = null ;
/// <summary>
/// 暂停事件
/// </summary>
private
readonly ManualResetEvent _mManualReset = null ;
/// <summary>
/// 串口号
/// </summary>
private
readonly int _comNo = 0;
/// <summary>
/// 时间间隔
/// </summary>
private
readonly int _timeTick = 0;
/// <summary>
/// 是否多卡读取
/// </summary>
private
bool _multiFlag = false ;
/// <summary>
/// RFID数据
/// </summary>
private
readonly List< byte []> _data = new
List< byte []>();
/// <summary>
/// 数据锁
/// </summary>
private
readonly object _dataLock = new
object ();
/// <summary>
/// 错误数量
/// </summary>
private
int _errorCount = 0;
/// <summary>
/// 只读 串口号
/// </summary>
public
int ComNo
{
get
{
return
_comNo;
}
}
/// <summary>
/// 串口句柄
/// </summary>
public
IntPtr ComHadle { set ; get ; }
/// <summary>
/// 只读 时间间隔 毫秒级
/// </summary>
public
int TimeTick
{
get
{
return
_timeTick;
}
}
/// <summary>
/// 是否多卡标志
/// </summary>
public
bool MultiFlag { set
{ _multiFlag = value; } get
{ return
_multiFlag; } }
/// <summary>
/// 暂停读取标志
/// </summary>
public
bool StopReadFlag { set ; get ; }
/// <summary>
/// 出入串口
/// </summary>
public
PassCom PassCom { set ; get ; }
/// <summary>
/// 构造函数
/// </summary>
/// <param name="comNo"></param>
/// <param name="sleepTime"></param>
public
RfidDevice( int
comNo, int
sleepTime)
{
_comNo = comNo;
_timeTick = sleepTime;
_mManualReset = new
ManualResetEvent( true );
ComHadle = EPCSDKHelper.OpenComm(_comNo);
if
(ComHadle == new
IntPtr())
{
//输出系统日志
//throw new Exception("打开串口失败!");
LogInfo.Error( "打开串口:"
+ comNo + "失败!"
);
}
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="comNo"></param>
/// <param name="sleepTime"></param>
/// <param name="multiFlag"></param>
public
RfidDevice( int
comNo, int
sleepTime, bool
multiFlag)
{
_comNo = comNo;
_timeTick = sleepTime;
MultiFlag = multiFlag;
_mManualReset = new
ManualResetEvent( true );
ComHadle = EPCSDKHelper.OpenComm(_comNo);
if
(ComHadle == new
IntPtr())
{
//输出系统日志
//throw new Exception("打开串口失败!");
LogInfo.Error( "打开串口:"
+ comNo + "失败!" );
}
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="comNo"></param>
/// <param name="sleepTime"></param>
/// <param name="multiFlag"></param>
/// <param name="passCom"></param>
public
RfidDevice( int
comNo, int
sleepTime, bool
multiFlag,PassCom passCom)
{
_comNo = comNo;
_timeTick = sleepTime;
_multiFlag = multiFlag;
MultiFlag = multiFlag;
_mManualReset = new
ManualResetEvent( true );
this .PassCom = passCom;
ComHadle = EPCSDKHelper.OpenComm(_comNo);
#if DEBUG
Console.WriteLine( "串口号:"
+ this .ComNo.ToString() + " - "
+ ComHadle.ToString());
#endif
if
(ComHadle == new
IntPtr())
{
//输出系统日志
//throw new Exception("打开串口失败!");
LogInfo.Error( "打开串口:"
+ comNo + "失败!" );
}
}
/// <summary>
/// 关闭串口
/// </summary>
public
void CloseComm()
{
EPCSDKHelper.CloseComm( this .ComHadle);
LogInfo.Info( "关闭串口:"
+ this .ComNo );
}
/// <summary>
/// 开始读取
/// </summary>
public
void Start()
{
if
(_mThread != null ) return ;
_mThread = new
Thread(GetRfidTag) {IsBackground = true };
_mThread.Start();
}
/// <summary>
/// 暂停
/// </summary>
public
void ReStart()
{
_mManualReset.Set();
}
/// <summary>
/// 继续
/// </summary>
public
void Stop()
{
_mManualReset.Reset();
}
/// <summary>
/// 获取RFID标签现成
/// </summary>
private
void GetRfidTag()
{
while ( true )
{
GcCollect();
try
{
Monitor.Enter( this ._dataLock);
_mManualReset.WaitOne();
byte [] ids;
byte [] devNos;
byte [] antennaNos;
if
( this ._multiFlag)
{
ids = new
byte [12 * 200];
devNos = new
byte [200];
antennaNos = new
byte [200];
//处理多卡读取模式
byte
idNum = 0;
if
(EPCSDKHelper.IdentifyUploadedMultiTags( this .ComHadle, out
idNum, ids, devNos, antennaNos))
{
_errorCount = 0;
var
tmpids = new
byte [idNum * 12];
Array.Copy(ids, 0, tmpids, 0, tmpids.Length);
this ._data.Add(tmpids);
#if DEBUG
Console.WriteLine( "串口号:" + this .ComNo.ToString() + " - "
+ DateTime.Now.ToString( "yyyy-MM-dd HH:mm:ss.yyy" ) + " - 02 - "
+ TextEncoder.ByteArrayToHexString(ids));
LogInfo.Info( "串口号:"
+ this .ComNo.ToString() + " - "
+ DateTime.Now.ToString( "yyyy-MM-dd HH:mm:ss.yyy" ) + " - 02 - "
+ TextEncoder.ByteArrayToHexString(ids));
#endif
}
}
else
{
ids = new
byte [12];
devNos = new
byte [1];
antennaNos = new
byte [1];
//处理单卡读取模式
if
(EPCSDKHelper.IdentifyUploadedSingleTag( this .ComHadle, ids, devNos, antennaNos))
{
_errorCount = 0;
this ._data.Add(ids);
#if DEBUG
Console.WriteLine( "串口号:"
+ this .ComNo.ToString() + " - "
+ DateTime.Now.ToString( "yyyy-MM-dd HH:mm:ss.yyy" ) + " - 01 - "
+ TextEncoder.ByteArrayToHexString(ids));
LogInfo.Info( "串口号:"
+ this .ComNo.ToString() + " - "
+ DateTime.Now.ToString( "yyyy-MM-dd HH:mm:ss.yyy" ) + " - 01 - "
+ TextEncoder.ByteArrayToHexString(ids));
#endif
}
}
}
catch
(Exception er)
{
#if DEBUG
Console.WriteLine( "串口号:"
+ this .ComNo.ToString() + " - "
+ DateTime.Now.ToString( "yyyy-MM-dd HH:mm:ss.yyy" ) + " Error: "
+ er.Message);
LogInfo.Error( "串口号:"
+ this .ComNo.ToString() + " - "
+ DateTime.Now.ToString( "yyyy-MM-dd HH:mm:ss.yyy" ) + " Error: "
+ er.Message);
#endif
_errorCount++;
if
(_errorCount > 10)
{
//设备复位
}
}
finally
{
Monitor.Exit( this ._dataLock);
}
Thread.Sleep( this ._timeTick);
}
}
/// <summary>
/// 获取RFID数据标签
/// </summary>
/// <returns></returns>
public
IList< byte []> GetData()
{
try
{
Monitor.Enter( this ._dataLock);
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
var
tmpData = new
List< byte []>();
tmpData.AddRange(_data);
_data.Clear();
#if DEBUG
Console.WriteLine( "串口号:"
+ this .ComNo.ToString() + " - "
+ "_tmpData:"
+ tmpData.Count + " _data:"
+ _data.Count);
LogInfo.Info( "串口号:"
+ this .ComNo.ToString() + " - "
+ "_tmpData:"
+ tmpData.Count + " _data:"
+ _data.Count);
#endif
return
tmpData;
}
finally
{
Monitor.Exit( this ._dataLock);
}
}
/// <summary>
/// 数据回收
/// </summary>
private
static void GcCollect()
{
GC.WaitForFullGCComplete();
GC.Collect();
GC.WaitForFullGCComplete();
}
}
}
|