C# RFID windows 服务 串口方式
2020-12-13 01:33
标签:style blog class c code tar 话说RFID以前很火所以整理一下一年前自己处理的RFID程序,放源码. 一开始觉得他是个很神奇的东西。 包含串口通讯和网络通讯。 由于网络通讯设备太贵,所以国内的设备基本上都是在外置一个比较便宜的模块在里面。
本案例应该适用于大多数的RFID模块。 首先我们先放上RFID API:如下 我们看到OpenComm他还是一串口方式打开的。 我们要记录每个设备的信息所以我们需要一个设备类
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Runtime.InteropServices;
namespace
Rfid
{
public
class
EPCSDKHelper
{
[DllImport(
"EPCSDK.dll"
)]
public
static
extern
IntPtr OpenComm(
int
portNo);
[DllImport(
"EPCSDK.dll"
)]
public
static
extern
void
CloseComm(IntPtr hCom);
[DllImport(
"EPCSDK.dll"
)]
public
static
extern
bool
ReadFirmwareVersion(IntPtr hCom,
out
int
main,
out
int
sub,
byte
ReaderAddr);
[DllImport(
"EPCSDK.dll"
)]
public
static
extern
bool
GetReaderParameters(IntPtr hCom,
int
addr,
int
paramNum,
byte
[] parms,
byte
ReaderAddr);
[DllImport(
"EPCSDK.dll"
)]
public
static
extern
bool
SetReaderParameters(IntPtr hCom,
int
addr,
int
paramNum,
byte
[] parms,
byte
ReaderAddr);
[DllImport(
"EPCSDK.dll"
)]
public
static
extern
bool
StopReading(IntPtr hCom,
byte
ReaderAddr);
[DllImport(
"EPCSDK.dll"
)]
public
static
extern
bool
ResumeReading(IntPtr hCom,
byte
ReaderAddr);
[DllImport(
"EPCSDK.dll"
)]
public
static
extern
bool
IdentifySingleTag(IntPtr hCom,
byte
[] tagId,
byte
[] antennaNo,
byte
ReaderAddr);
[DllImport(
"EPCSDK.dll"
)]
public
static
extern
bool
IdentifyUploadedSingleTag(IntPtr hCom,
byte
[] tagId,
byte
[] devNos,
byte
[] antennaNo);
[DllImport(
"EPCSDK.dll"
)]
public
static
extern
bool
IdentifyUploadedMultiTags(IntPtr hCom,
out
byte
tagNum,
byte
[] tagIds,
byte
[] devNos,
byte
[] antennaNos);
[DllImport(
"EPCSDK.dll"
)]
public
static
extern
bool
ReadTag(IntPtr hCom,
byte
memBank,
byte
address,
byte
length,
byte
[] data,
byte
ReaderAddr);
[DllImport(
"EPCSDK.dll"
)]
public
static
extern
bool
WriteTagSingleWord(IntPtr hCom,
byte
memBank,
byte
address,
byte
data1,
byte
data2,
byte
ReaderAddr);
[DllImport(
"EPCSDK.dll"
)]
public
static
extern
bool
FastWriteTagID(IntPtr hCom,
int
bytesNum,
byte
[] bytes,
byte
ReaderAddr);
[DllImport(
"EPCSDK.dll"
)]
public
static
extern
bool
FastWriteTagID_Lock(IntPtr hCom,
int
bytesNum,
byte
[] bytes,
byte
ReaderAddr);
[DllImport(
"EPCSDK.dll"
)]
public
static
extern
bool
InitializeTag(IntPtr hCom,
byte
ReaderAddr);
[DllImport(
"EPCSDK.dll"
)]
public
static
extern
bool
LockPassWordTag(IntPtr hCom,
byte
passwd1,
byte
passwd2,
byte
passwd3,
byte
passwd4,
byte
lockType,
byte
ReaderAddr);
[DllImport(
"EPCSDK.dll"
)]
public
static
extern
bool
UnlockPassWordTag(IntPtr hCom,
byte
passwd1,
byte
passwd2,
byte
passwd3,
byte
passwd4,
byte
lockType,
byte
ReaderAddr);
[DllImport(
"EPCSDK.dll"
)]
public
static
extern
bool
KillTag(IntPtr hCom,
byte
passwd1,
byte
passwd2,
byte
passwd3,
byte
passwd4,
byte
ReaderAddr);
}
}
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading;
using
RfidService.Common;
namespace
Rfid
{
public
class
RfidDevice
{
///
/// 开启监听现成
///
private
Thread _mThread =
null
;
///
/// 暂停事件
///
private
readonly
ManualResetEvent _mManualReset =
null
;
///
/// 串口号
///
private
readonly
int
_comNo = 0;
///
/// 时间间隔
///
private
readonly
int
_timeTick = 0;
///
/// 是否多卡读取
///
private
bool
_multiFlag =
false
;
///
/// RFID数据
///
private
readonly
List
byte
[]> _data =
new
List
byte
[]>();
///
/// 数据锁
///
private
readonly
object
_dataLock =
new
object
();
///
/// 错误数量
///
private
int
_errorCount = 0;
///
/// 只读 串口号
///
public
int
ComNo
{
get
{
return
_comNo;
}
}
///
/// 串口句柄
///
public
IntPtr ComHadle {
set
;
get
; }
///
/// 只读 时间间隔 毫秒级
///
public
int
TimeTick
{
get
{
return
_timeTick;
}
}
///
/// 是否多卡标志
///
public
bool
MultiFlag {
set
{ _multiFlag = value; }
get
{
return
_multiFlag; } }
///
/// 暂停读取标志
///
public
bool
StopReadFlag {
set
;
get
; }
///
/// 出入串口
///
public
PassCom PassCom {
set
;
get
; }
///
/// 构造函数
///
///
///
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 +
"失败!"
);
}
}
///
/// 构造函数
///
///
///
///
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 +
"失败!"
);
}
}
///
/// 构造函数
///
///
///
///
///
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 +
"失败!"
);
}
}
///
/// 关闭串口
///
public
void
CloseComm()
{
EPCSDKHelper.CloseComm(
this
.ComHadle);
LogInfo.Info(
"关闭串口:"
+
this
.ComNo );
}
///
/// 开始读取
///
public
void
Start()
{
if
(_mThread !=
null
)
return
;
_mThread =
new
Thread(GetRfidTag) {IsBackground =
true
};
_mThread.Start();
}
///
/// 暂停
///
public
void
ReStart()
{
_mManualReset.Set();
}
///
/// 继续
///
public
void
Stop()
{
_mManualReset.Reset();
}
///
/// 获取RFID标签现成
///
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)
{
//设备复位
}