RFID读取数据

master
CZ 6 months ago
parent ce34b813c1
commit b16d6e6995

@ -1,6 +1,7 @@
using log4net; using log4net;
using Newtonsoft.Json; using Newtonsoft.Json;
using Service.Execute.Model; using Service.Execute.Model;
using Service.Rfid;
namespace Service.Execute.Api; namespace Service.Execute.Api;
@ -18,68 +19,21 @@ public class RfidApi
public static async Task<bool> BeginRead() public static async Task<bool> BeginRead()
{ {
Log.Info("BeginRead"); Log.Info("BeginRead");
/*string url = BASE_URL + "/Api/BeginRead";
try return RfidMgr.BeginRead();
{
string s = await _httpClient.GetStringAsync(url);
Log.Info($"BeginRead resp = {s}");
return bool.Parse(s);
}
catch (Exception e)
{
Console.WriteLine(e);
return false;
}*/
return true;
} }
public static async Task<bool> StopRead() public static async Task<bool> StopRead()
{ {
Log.Info("StopRead"); Log.Info("StopRead");
string url = BASE_URL + "/Api/StopRead"; string url = BASE_URL + "/Api/StopRead";
/*try return RfidMgr.StopRead();
{
string s = await _httpClient.GetStringAsync(url);
Log.Info($"StopRead resp = {s}");
return bool.Parse(s);
}
catch (Exception e)
{
Console.WriteLine(e);
return false;
}*/
return true;
} }
public static async Task<RfidReadModel?> ReadRifd() public static async Task<RfidReadModel?> ReadRifd()
{ {
Log.Info("ReadRifd"); Log.Info("ReadRifd");
/*string url = BASE_URL + "/Api/ReadRfidData"; return RfidMgr.ReadRifd();
try
{
string s = await _httpClient.GetStringAsync(url);
Log.Info($"ReadRifd resp={s}");
if (s != String.Empty)
{
RfidReadModel? model = JsonConvert.DeserializeObject<RfidReadModel>(s);
if (model != null)
{
return model;
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
return null;
}*/
return new RfidReadModel()
{
VelVin = "LC1HMYBF6R0004575",
VelNo = "LC1HMYBF6R0004575",
};
} }
} }

@ -0,0 +1,57 @@
using Service.Execute.Model;
namespace Service.Rfid;
public class RfidMgr
{
public static RfidReadWriteTool RfidReadWriteTool;
/// <summary>
/// 连接
/// </summary>
/// <returns></returns>
public static async Task<bool> Connected()
{
bool bResult = RfidReadWriteTool.ConnectRfid();
return bResult;
}
/// <summary>
/// 断开
/// </summary>
/// <returns></returns>
public static async Task<bool> CloseRfid()
{
bool bResult = RfidReadWriteTool.CloseRfid();
return bResult;
}
/// <summary>
/// 开始读
/// </summary>
/// <returns></returns>
public static bool BeginRead()
{
bool bResult = RfidReadWriteTool.ScanRfidUserDataTag();
return bResult;
}
/// <summary>
/// 停止读
/// </summary>
/// <returns></returns>
public static bool StopRead()
{
bool bResult = RfidReadWriteTool.StopRfidResult();
return bResult;
}
/// <summary>
/// 读取信息
/// </summary>
/// <returns></returns>
public static RfidReadModel? ReadRifd()
{
return RfidReadWriteTool.RfidReadModel;
}
}

@ -0,0 +1,408 @@
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;
}
}

@ -0,0 +1,21 @@
namespace Service.Rfid;
public class ViewEventArgs : EventArgs
{
/// <summary>
/// 连接状态
/// </summary>
public bool IsConnected { get; set; }
/// <summary>
/// 连接结果消息
/// </summary>
public string MsgContent { get; set; }
}
public class ViewMsgEventArgs : EventArgs
{
/// <summary>
/// 发送或接收消息
/// </summary>
public string MsgContent { get; set; }
}

@ -39,4 +39,10 @@
<ProjectReference Include="..\Repository\Repository.csproj" /> <ProjectReference Include="..\Repository\Repository.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Reference Include="SRGReaderAPI">
<HintPath>bin\Debug\net6.0-windows\SRGReaderAPI.dll</HintPath>
</Reference>
</ItemGroup>
</Project> </Project>

Loading…
Cancel
Save