|
|
|
@ -1,408 +0,0 @@
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Service.Execute.Model;
|
|
|
|
|
using SRGReaderAPI;
|
|
|
|
|
|
|
|
|
|
namespace Service.Rfid;
|
|
|
|
|
|
|
|
|
|
public class RfidReadWriteTool
|
|
|
|
|
{
|
|
|
|
|
#region 字段属性
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// IP地址
|
|
|
|
|
/// </summary>
|
|
|
|
|
private string _ipaddr = "172.0.30.9";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// RFID连接IP地址
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string IpAddr
|
|
|
|
|
{
|
|
|
|
|
get { return _ipaddr; }
|
|
|
|
|
set { _ipaddr = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 子网掩码
|
|
|
|
|
/// </summary>
|
|
|
|
|
private string _subnet = "255.255.255.0";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// RFID连接的子网掩码
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string Subnet
|
|
|
|
|
{
|
|
|
|
|
get { return _subnet; }
|
|
|
|
|
set { _subnet = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 网关
|
|
|
|
|
/// </summary>
|
|
|
|
|
private string _gateway = "172.0.30.1";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// RFID连接的网关
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string Gateway
|
|
|
|
|
{
|
|
|
|
|
get { return _gateway; }
|
|
|
|
|
set { _gateway = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 端口
|
|
|
|
|
/// </summary>
|
|
|
|
|
private int _port = 9090;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// RFID连接端口
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int Port
|
|
|
|
|
{
|
|
|
|
|
get { return _port; }
|
|
|
|
|
set { _port = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 连接超时时间(ms)
|
|
|
|
|
/// </summary>
|
|
|
|
|
private int _timeout = 1000;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 连接超时时间(ms)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int TimeOut
|
|
|
|
|
{
|
|
|
|
|
get { return _timeout; }
|
|
|
|
|
set { _timeout = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 读写器
|
|
|
|
|
/// </summary>
|
|
|
|
|
private SReader _rfid_reader = null;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// RFID连接客户端
|
|
|
|
|
/// </summary>
|
|
|
|
|
public SReader RfidReader
|
|
|
|
|
{
|
|
|
|
|
get { return _rfid_reader; }
|
|
|
|
|
set { _rfid_reader = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 客户端目前连接状态
|
|
|
|
|
/// </summary>
|
|
|
|
|
private bool _isconnected = false;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 通讯是否连接
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsConnected
|
|
|
|
|
{
|
|
|
|
|
get { return _isconnected; }
|
|
|
|
|
set { _isconnected = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// UserData
|
|
|
|
|
/// </summary>
|
|
|
|
|
private byte[] _user_datas = null;
|
|
|
|
|
|
|
|
|
|
#endregion 字段属性
|
|
|
|
|
|
|
|
|
|
#region 事件定义
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 连接状态
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event EventHandler<ViewEventArgs> ConnectedChanged;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 数据已接收
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event EventHandler<ViewMsgEventArgs> DataReceived;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 已发送的数据
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event EventHandler<ViewMsgEventArgs> DataSended;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置监听事件
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="isConnected">是否监听</param>
|
|
|
|
|
/// <param name="msgContent">监听消息事件</param>
|
|
|
|
|
private void SetConnectEvent(bool isConnected, string msgContent)
|
|
|
|
|
{
|
|
|
|
|
EventHandler<ViewEventArgs> ConnectedHandler;
|
|
|
|
|
ConnectedHandler = ConnectedChanged;
|
|
|
|
|
if (ConnectedHandler != null)
|
|
|
|
|
{
|
|
|
|
|
ConnectedHandler.Invoke(this, new ViewEventArgs() { IsConnected = isConnected, MsgContent = msgContent });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置接收消息事件
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="msgContent">消息内容</param>
|
|
|
|
|
private void SetRecvMsgEvent(string msgContent)
|
|
|
|
|
{
|
|
|
|
|
EventHandler<ViewMsgEventArgs> RecvedHandler;
|
|
|
|
|
RecvedHandler = DataReceived;
|
|
|
|
|
if (RecvedHandler != null)
|
|
|
|
|
{
|
|
|
|
|
RecvedHandler.Invoke(this, new ViewMsgEventArgs() { MsgContent = msgContent });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置发送消息事件
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="msgContent">消息内容</param>
|
|
|
|
|
private void SetSendMsgEvent(string msgContent)
|
|
|
|
|
{
|
|
|
|
|
EventHandler<ViewMsgEventArgs> SendedHandler;
|
|
|
|
|
SendedHandler = DataSended;
|
|
|
|
|
if (SendedHandler != null)
|
|
|
|
|
{
|
|
|
|
|
SendedHandler.Invoke(this, new ViewMsgEventArgs() { MsgContent = msgContent });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion 事件定义
|
|
|
|
|
|
|
|
|
|
#region 结构体
|
|
|
|
|
|
|
|
|
|
public RfidReadWriteTool()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RfidReadWriteTool(string ipaddr, int port)
|
|
|
|
|
{
|
|
|
|
|
_ipaddr = ipaddr;
|
|
|
|
|
_port = port;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RfidReadWriteTool(string ipaddr, string subnet, string gateway, int port)
|
|
|
|
|
{
|
|
|
|
|
_ipaddr = ipaddr;
|
|
|
|
|
_subnet = subnet;
|
|
|
|
|
_gateway = gateway;
|
|
|
|
|
_port = port;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion 结构体
|
|
|
|
|
|
|
|
|
|
public RfidReadModel RfidReadModel;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// RFID读写器连接
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ipAddress"></param>
|
|
|
|
|
/// <param name="nPort"></param>
|
|
|
|
|
/// <returns>-1:初始默认值;0:连接成功;1:超时;2:无响应</returns>
|
|
|
|
|
public bool ConnectRfid()
|
|
|
|
|
{
|
|
|
|
|
string addr = "tcp://172.0.30.100";
|
|
|
|
|
_rfid_reader = SReader.Create(addr);
|
|
|
|
|
_rfid_reader.Connect();
|
|
|
|
|
_isconnected = true;
|
|
|
|
|
SetConnectEvent(_isconnected, "RFID读写器连接成功!");
|
|
|
|
|
_rfid_reader.TagRead += _rfid_reader_TagRead;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 标签触发读取方法
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void _rfid_reader_TagRead(object sender, TagReadDataEventArgs tagData)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
byte[] userDatas = tagData.TagData.Data;
|
|
|
|
|
|
|
|
|
|
string strResult = "";
|
|
|
|
|
string[] strTemps = null;
|
|
|
|
|
string strVelNo = "";
|
|
|
|
|
if (userDatas != null)
|
|
|
|
|
{
|
|
|
|
|
string strUser = Encoding.ASCII.GetString(userDatas);
|
|
|
|
|
if (!string.IsNullOrEmpty(strUser))
|
|
|
|
|
{
|
|
|
|
|
strResult = strUser.Replace("\0", "").Replace(" ", "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
strTemps = strResult.Split(';');
|
|
|
|
|
|
|
|
|
|
if (strTemps.Length == 4)
|
|
|
|
|
{
|
|
|
|
|
if (strTemps[3] != "")
|
|
|
|
|
{
|
|
|
|
|
byte[] bytesVelNo = ToByteByHexStr(strTemps[3]);
|
|
|
|
|
Encoding gbEcoding = Encoding.GetEncoding("gb2312");
|
|
|
|
|
strVelNo = gbEcoding.GetString(bytesVelNo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
strResult = "VIN码:" + strTemps[0] + ";车型号:" + strTemps[1] + ";车辆MAC:" + strTemps[2] + ";车牌号:" + strVelNo;
|
|
|
|
|
SetRecvMsgEvent(strResult);
|
|
|
|
|
|
|
|
|
|
RfidReadModel = new RfidReadModel()
|
|
|
|
|
{
|
|
|
|
|
Result = "成功",
|
|
|
|
|
VelVin = strTemps[0],
|
|
|
|
|
VelMac = strTemps[2],
|
|
|
|
|
VelNo = strTemps[3],
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ex.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// RFID读写器关闭
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>-1:初始默认值;0:连接成功;1:超时;2:无响应</returns>
|
|
|
|
|
public bool CloseRfid()
|
|
|
|
|
{
|
|
|
|
|
if (_rfid_reader != null)
|
|
|
|
|
{
|
|
|
|
|
_rfid_reader.ShutDown();
|
|
|
|
|
_rfid_reader.TagRead -= _rfid_reader_TagRead; //删除事件
|
|
|
|
|
_isconnected = false;
|
|
|
|
|
SetConnectEvent(true, "RFID读写器断开成功!");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 写入单个标签值
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool WriteRfidSingleTagResult(byte[] writeResult)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (writeResult != null)
|
|
|
|
|
{
|
|
|
|
|
StopRfidResult();
|
|
|
|
|
|
|
|
|
|
//标签内存区。RESERVED (0x0),EPC (0x1),TID (0x2),USER (0x3);
|
|
|
|
|
Gen2.Bank userBank = Gen2.Bank.USER;
|
|
|
|
|
//起始地址,单位:字
|
|
|
|
|
int wordPtrUser = 0;
|
|
|
|
|
//字节长度,单位:字
|
|
|
|
|
int wordLenUser = 0;
|
|
|
|
|
//要写入的数据
|
|
|
|
|
_user_datas = writeResult;
|
|
|
|
|
//访问密码,若没有锁定,可为0
|
|
|
|
|
int accessPsd = 0;
|
|
|
|
|
wordLenUser = _user_datas.Length / 2; //长度必须偶数
|
|
|
|
|
|
|
|
|
|
//写数据对象
|
|
|
|
|
Gen2.WriteData dataWrtUser =
|
|
|
|
|
new Gen2.WriteData(userBank, wordPtrUser, wordLenUser, _user_datas, accessPsd);
|
|
|
|
|
|
|
|
|
|
//天线
|
|
|
|
|
UInt16 ant = 0x01;
|
|
|
|
|
_rfid_reader.WriteMemory(ant, dataWrtUser, null);
|
|
|
|
|
SetRecvMsgEvent("RFID电子标签USER区:写入成功!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (ReaderCommException e)
|
|
|
|
|
{
|
|
|
|
|
SetRecvMsgEvent(e.ToString());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 盘点电子标签用户区数据
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool ScanRfidUserDataTag()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_rfid_reader != null)
|
|
|
|
|
{
|
|
|
|
|
UInt16 ant = 0x01;
|
|
|
|
|
//标签内存区。RESERVED (0x0),EPC (0x1),TID (0x2),USER (0x3);
|
|
|
|
|
Gen2.Bank userBank = Gen2.Bank.USER;
|
|
|
|
|
//读取地址,单位:字
|
|
|
|
|
int readAdrUser = 0;
|
|
|
|
|
//读取长度,单位:字
|
|
|
|
|
int readLenUser = 36;
|
|
|
|
|
//访问密码,若没有锁定,可为0
|
|
|
|
|
int accessPsd = 0;
|
|
|
|
|
Gen2.EmbedTagOp mixEmbedTag = new Gen2.EmbedTagOp(userBank, readAdrUser, readLenUser, accessPsd);
|
|
|
|
|
//_rfid_reader.Inventry(ant, null);
|
|
|
|
|
//Thread.Sleep(10);
|
|
|
|
|
_rfid_reader.Inventry_mix(ant, mixEmbedTag, null);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ex.ToString();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 停读
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool StopRfidResult()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_rfid_reader != null)
|
|
|
|
|
{
|
|
|
|
|
_rfid_reader.Inventry_stop();
|
|
|
|
|
SetRecvMsgEvent("RFID电子标签:停止扫读成功");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
SetRecvMsgEvent("RFID电子标签:停止扫读失败");
|
|
|
|
|
ex.ToString();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取16进制字符串的字节数组
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="hexString">hexString 16进制字符串</param>
|
|
|
|
|
/// <returns>字节数组</returns>
|
|
|
|
|
public byte[] ToByteByHexStr(string hexString)
|
|
|
|
|
{
|
|
|
|
|
if (hexString == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
hexString = hexString.Replace(" ", "");
|
|
|
|
|
if ((hexString.Length % 2) != 0)
|
|
|
|
|
hexString += " ";
|
|
|
|
|
byte[] returnBytes = new byte[hexString.Length / 2];
|
|
|
|
|
for (int i = 0; i < returnBytes.Length; i++)
|
|
|
|
|
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
|
|
|
|
|
return returnBytes;
|
|
|
|
|
}
|
|
|
|
|
}
|