C# RFID windows 服务 网络协议方式
2020-12-13 01:33
标签:blog class c code tar color 上篇话说是串口方式操作RFID设备。 下面介绍网络协议方式。 设备支持断线重连。 那我们的服务也不能差了不是。
所以这个服务类也是支持的哦。 不解释上代码: 上面介绍了网络通讯的设备类当然也少不了Socket通讯类。 不然没法通许不是
namespace
Rfid
{
///
/// 获取Vip数据
///
///
public
delegate
void
GetVipEventHandle(
object
obj, MarkArgs vip);
///
/// 识别标签
///
///
public
delegate
void
IdentificationMarksEventHandle(
object
obj, MarkArgs idMark);
///
/// 写标签
///
public
delegate
void
WriteMarksEventHandle(
object
obj, MarkArgs writeMark);
public
class
RfidSocket
{
public
event
GetVipEventHandle GetVip;
public
event
IdentificationMarksEventHandle IdentificationMarks;
public
event
WriteMarksEventHandle WriteMarks;
///
/// 识别标签协议
///
public
readonly
byte
[] MReadMarks =
new
byte
[5] { 0xA0, 0x03, 0x82, 0x00, 0xDB };
///
/// 接受失败协议
///
public
readonly
byte
[] MErrorHeader =
new
byte
[3] { 0xE4, 0x04, 0x82 };
///
/// 接受成功协议
///
public
readonly
byte
[] MSucessHeader =
new
byte
[3] { 0xE0, 0x10, 0x82 };
///
/// Vip数据长度
///
public
const
int
MDataLength = 12;
///
/// 发送读取协议证
///
public
System.Windows.Forms.Timer MTimer =
new
System.Windows.Forms.Timer();
///
/// 链接状态
///
public
bool
Connected =
false
;
///
/// Vip编号
///
public
string
VipId =
string
.Empty;
///
/// 全局锁
///
public
object
MLock =
new
object
();
///
/// Socket通讯
///
public
SocketClient Client =
new
SocketClient();
///
/// 连接错误次数
///
public
int
MConnectErrorCount = 0;
public
string
Ip {
get
;
set
; }
public
int
Port {
get
;
set
; }
public
int
ErrorCount {
get
;
set
; }
public
RfidSocket(
string
ip,
int
port,
int
errorCount)
{
this
.Ip = ip;
this
.Port = port;
this
.ErrorCount = errorCount;
}
void
client_OnDisconnected(
object
sender)
{
Client.mOnConnected -= client_mOnConnected;
Client.mOnError -= client_mOnError;
Client.OnDataIn -= client_OnDataIn;
Client.OnDisconnected -= client_OnDisconnected;
}
void
client_OnDataIn(
object
sender,
byte
[] data)
{
try
{
GC.Collect();
GC.WaitForFullGCComplete();
GC.Collect();
//当前不考虑数据协议失败情况
Monitor.Enter(MLock);
//判断消息头
if
(data.Length > 3)
{
IdentificationTag(data);
WriteTag(data);
}
ReaderTag(data);
}
finally
{
Monitor.Exit(MLock);
}
}
/// 识别标签协
private
void
IdentificationTag(
byte
[] data)
{
//识别标签协议成功
if
(data[0] == 0xE0 && data[1] == 0x10 && data[2] == 0x82)
{
//解析标签成功
if
(data.Length >= 18)
{
var
vipByte =
new
byte
[4];
Array.Copy(data, 13, vipByte, 0, vipByte.Length);
var
vipId = ((vipByte[0]) + (vipByte[1]
if
(IdentificationMarks !=
null
)
{
var
markArgs =
new
MarkArgs(vipId,
string
.Empty,
true
);
IdentificationMarks(
this
, markArgs);
}
}
}
else
if
(data[0] == 0xE4 && data[1] == 0x04 && data[2] == 0x82)
{
if
(IdentificationMarks !=
null
)
{
var
markArgs =
new
MarkArgs(
string
.Empty,
string
.Empty,
false
);
IdentificationMarks(
this
, markArgs);
}
}
}
/// 快写标签
private
void
WriteTag(
byte
[] data)
{
if
(data[0] == 0xE0 && data[1] == 0x04 && data[2] == 0x9C)
{
if
(data.Length >= 6)
{
if
(data[4] == 0x0)
{
//写标签成功
if
(WriteMarks !=
null
)
{
var
markArgs =
new
MarkArgs(
string
.Empty,
string
.Empty,
true
);
WriteMarks(
this
, markArgs);
}
}
else
{
//写标签失败
if
(WriteMarks !=
null
)
{
var
markArgs =
new
MarkArgs(
string
.Empty,
string
.Empty,
false
);
WriteMarks(
this
, markArgs);
}
}
}
}
}
/// 定时模式读取标签
private
void
ReaderTag(
byte
[] data)
{
if
(data.Length >= 17)
{
if
(data[0] == 0x00 && data[1] == 0x00 && data[16] == 0xFF)
{
var
bytCheck =
new
byte
[15];
Array.Copy(data, 0, bytCheck, 0, 15);
var
checkSum = EPCSDKHelper.CheckSum(bytCheck) & 0xFF;
//校验和
if
(data[15] == checkSum)
{
var
vipByte =
new
byte
[4];
Array.Copy(bytCheck, 10, vipByte, 0, 4);
var
vipId = ((vipByte[0]) + (vipByte[1]
this
.VipId = vipId;
if
(GetVip !=
null
)
{
var
markArgs =
new
MarkArgs(
this
.VipId,
string
.Empty,
true
);
GetVip(
this
, markArgs);
}
}
}
}
}
private
void
client_mOnError(
object
sender, SocketException error)
{
if
(!Client.ConnectFlag)
{
ReConnect();
}
}
private
void
client_mOnConnected(
object
sender)
{
MConnectErrorCount = 0;
}
private
void
ReConnect()
{
if
(Client !=
null
)
{
Client.mOnConnected -= client_mOnConnected;
Client.mOnError -= client_mOnError;
Client.OnDataIn -= client_OnDataIn;
Client.OnDisconnected -= client_OnDisconnected;
}
Client =
new
SocketClient();
Client.Connect(
this
.Ip,
this
.Port);
Client.mOnConnected += client_mOnConnected;
Client.mOnError += client_mOnError;
Client.OnDataIn += client_OnDataIn;
Client.OnDisconnected += client_OnDisconnected;
}
private
void
mTimer_Tick(
object
sender, EventArgs e)
{
try
{
MTimer.Enabled =
false
;
if
(Client.ConnectFlag)
{
Client.Send(TagProtocol.MIdentificationMarks);
}
else
{
MConnectErrorCount++;
if
(MConnectErrorCount >
this
.ErrorCount)
{
ReConnect();
}
}
}
finally
{
MTimer.Enabled =
true
;
}
}
public
void
Start()
{
Client =
new
SocketClient();
Client.Connect(
this
.Ip,
this
.Port);
Client.mOnConnected += client_mOnConnected;
Client.mOnError += client_mOnError;
Client.OnDataIn += client_OnDataIn;
Client.OnDisconnected += client_OnDisconnected;
MTimer.Interval = 1000;
MTimer.Enabled =
true
;
MTimer.Tick += mTimer_Tick;
}
public
void
ClearVipId()
{
try
{
Monitor.Enter(MLock);
this
.VipId =
string
.Empty;
}
finally
{
Monitor.Exit(MLock);
}
}
public
void
WriteMark(
int
mark)
{
if
(mark 0xffffffff)
{
throw
new
Exception(
"超出写标签范围!"
);
}
var
markByte = mark.ToString(
"x"
).PadLeft(8,
‘0‘
);
var
byt =
new
byte
[4];
for
(
var
i = 0; i
{
byt[i / 2] = (
byte
)Convert.ToInt32(markByte[i].ToString() + markByte[i + 1].ToString(), 16);
}
var
writeMarkData =
new
byte
[10];
Array.Copy(TagProtocol.MWriteMark, 0, writeMarkData, 0, TagProtocol.MWriteMark.Length);
Array.Copy(byt, 0, writeMarkData, TagProtocol.MWriteMark.Length, byt.Length);
//写校验和
writeMarkData[9] = (
byte
)(EPCSDKHelper.CheckSum(writeMarkData) & 0xFF);
Client.Send(writeMarkData);
GC.Collect();
GC.WaitForFullGCComplete();
GC.Collect();
}
}
namespace
Rfid
{
public
delegate
void
ClientErrorEvent(
object
sender, SocketException error);
public
delegate
void
ClientOnDataInHandler(
object
sender,
byte
[] data);
public
delegate
void
ClientEvent(
object
sender);
public
class
SocketClient
{
public
event
ClientEvent mOnConnected =
null
;
public
event
ClientEvent OnDisconnected =
null
;
public
event
ClientOnDataInHandler OnDataIn =
null
;
public
event
ClientErrorEvent mOnError =
null
;
private
Socket cli =
null
;
private
byte
[] databuffer;
private
int
buffersize = 64 * 1024;
public
bool
ConnectFlag
{
get
{
if
(cli ==
null
)
{