From b16d6e699582ad7e3fd3560d18f3b50302158777 Mon Sep 17 00:00:00 2001 From: CZ Date: Sun, 2 Jun 2024 11:21:29 +0800 Subject: [PATCH] =?UTF-8?q?RFID=E8=AF=BB=E5=8F=96=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Service/Execute/Api/RfidApi.cs | 56 +--- Service/Rfid/RfidMgr.cs | 57 +++++ Service/Rfid/RfidReadWriteTool.cs | 408 ++++++++++++++++++++++++++++++ Service/Rfid/ViewEventArgs.cs | 21 ++ Service/Service.csproj | 6 + 5 files changed, 497 insertions(+), 51 deletions(-) create mode 100644 Service/Rfid/RfidMgr.cs create mode 100644 Service/Rfid/RfidReadWriteTool.cs create mode 100644 Service/Rfid/ViewEventArgs.cs diff --git a/Service/Execute/Api/RfidApi.cs b/Service/Execute/Api/RfidApi.cs index a8922ef..a926b42 100644 --- a/Service/Execute/Api/RfidApi.cs +++ b/Service/Execute/Api/RfidApi.cs @@ -1,6 +1,7 @@ using log4net; using Newtonsoft.Json; using Service.Execute.Model; +using Service.Rfid; namespace Service.Execute.Api; @@ -18,68 +19,21 @@ public class RfidApi public static async Task BeginRead() { Log.Info("BeginRead"); - /*string url = BASE_URL + "/Api/BeginRead"; - try - { - 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; + + return RfidMgr.BeginRead(); } public static async Task StopRead() { Log.Info("StopRead"); string url = BASE_URL + "/Api/StopRead"; - /*try - { - 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; + return RfidMgr.StopRead(); } public static async Task ReadRifd() { Log.Info("ReadRifd"); - /*string url = BASE_URL + "/Api/ReadRfidData"; - try - { - string s = await _httpClient.GetStringAsync(url); - Log.Info($"ReadRifd resp={s}"); - if (s != String.Empty) - { - RfidReadModel? model = JsonConvert.DeserializeObject(s); - if (model != null) - { - return model; - } - } - } - catch (Exception e) - { - Console.WriteLine(e); - return null; - }*/ - - return new RfidReadModel() - { - VelVin = "LC1HMYBF6R0004575", - VelNo = "LC1HMYBF6R0004575", - - }; + return RfidMgr.ReadRifd(); } } \ No newline at end of file diff --git a/Service/Rfid/RfidMgr.cs b/Service/Rfid/RfidMgr.cs new file mode 100644 index 0000000..a0db982 --- /dev/null +++ b/Service/Rfid/RfidMgr.cs @@ -0,0 +1,57 @@ +using Service.Execute.Model; + +namespace Service.Rfid; + +public class RfidMgr +{ + public static RfidReadWriteTool RfidReadWriteTool; + + /// + /// 连接 + /// + /// + public static async Task Connected() + { + bool bResult = RfidReadWriteTool.ConnectRfid(); + return bResult; + } + + /// + /// 断开 + /// + /// + public static async Task CloseRfid() + { + bool bResult = RfidReadWriteTool.CloseRfid(); + return bResult; + } + + /// + /// 开始读 + /// + /// + public static bool BeginRead() + { + bool bResult = RfidReadWriteTool.ScanRfidUserDataTag(); + return bResult; + } + + /// + /// 停止读 + /// + /// + public static bool StopRead() + { + bool bResult = RfidReadWriteTool.StopRfidResult(); + return bResult; + } + + /// + /// 读取信息 + /// + /// + public static RfidReadModel? ReadRifd() + { + return RfidReadWriteTool.RfidReadModel; + } +} \ No newline at end of file diff --git a/Service/Rfid/RfidReadWriteTool.cs b/Service/Rfid/RfidReadWriteTool.cs new file mode 100644 index 0000000..77585bb --- /dev/null +++ b/Service/Rfid/RfidReadWriteTool.cs @@ -0,0 +1,408 @@ +using System.Text; +using Service.Execute.Model; +using SRGReaderAPI; + +namespace Service.Rfid; + +public class RfidReadWriteTool +{ + #region 字段属性 + + /// + /// IP地址 + /// + private string _ipaddr = "172.0.30.9"; + + /// + /// RFID连接IP地址 + /// + public string IpAddr + { + get { return _ipaddr; } + set { _ipaddr = value; } + } + + /// + /// 子网掩码 + /// + private string _subnet = "255.255.255.0"; + + /// + /// RFID连接的子网掩码 + /// + public string Subnet + { + get { return _subnet; } + set { _subnet = value; } + } + + /// + /// 网关 + /// + private string _gateway = "172.0.30.1"; + + /// + /// RFID连接的网关 + /// + public string Gateway + { + get { return _gateway; } + set { _gateway = value; } + } + + /// + /// 端口 + /// + private int _port = 9090; + + /// + /// RFID连接端口 + /// + public int Port + { + get { return _port; } + set { _port = value; } + } + + /// + /// 连接超时时间(ms) + /// + private int _timeout = 1000; + + /// + /// 连接超时时间(ms) + /// + public int TimeOut + { + get { return _timeout; } + set { _timeout = value; } + } + + /// + /// 读写器 + /// + private SReader _rfid_reader = null; + + /// + /// RFID连接客户端 + /// + public SReader RfidReader + { + get { return _rfid_reader; } + set { _rfid_reader = value; } + } + + /// + /// 客户端目前连接状态 + /// + private bool _isconnected = false; + + /// + /// 通讯是否连接 + /// + public bool IsConnected + { + get { return _isconnected; } + set { _isconnected = value; } + } + + /// + /// UserData + /// + private byte[] _user_datas = null; + + #endregion 字段属性 + + #region 事件定义 + + /// + /// 连接状态 + /// + public event EventHandler ConnectedChanged; + + /// + /// 数据已接收 + /// + public event EventHandler DataReceived; + + /// + /// 已发送的数据 + /// + public event EventHandler DataSended; + + + /// + /// 设置监听事件 + /// + /// 是否监听 + /// 监听消息事件 + private void SetConnectEvent(bool isConnected, string msgContent) + { + EventHandler ConnectedHandler; + ConnectedHandler = ConnectedChanged; + if (ConnectedHandler != null) + { + ConnectedHandler.Invoke(this, new ViewEventArgs() { IsConnected = isConnected, MsgContent = msgContent }); + } + } + + /// + /// 设置接收消息事件 + /// + /// 消息内容 + private void SetRecvMsgEvent(string msgContent) + { + EventHandler RecvedHandler; + RecvedHandler = DataReceived; + if (RecvedHandler != null) + { + RecvedHandler.Invoke(this, new ViewMsgEventArgs() { MsgContent = msgContent }); + } + } + + /// + /// 设置发送消息事件 + /// + /// 消息内容 + private void SetSendMsgEvent(string msgContent) + { + EventHandler 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; + + /// + /// RFID读写器连接 + /// + /// + /// + /// -1:初始默认值;0:连接成功;1:超时;2:无响应 + 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; + } + + /// + /// 标签触发读取方法 + /// + /// + /// + 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(); + } + } + + /// + /// RFID读写器关闭 + /// + /// -1:初始默认值;0:连接成功;1:超时;2:无响应 + 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; + } + + /// + /// 写入单个标签值 + /// + 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; + } + } + + /// + /// 盘点电子标签用户区数据 + /// + 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; + } + } + + /// + /// 停读 + /// + 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; + } + } + + /// + /// 获取16进制字符串的字节数组 + /// + /// hexString 16进制字符串 + /// 字节数组 + 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; + } +} \ No newline at end of file diff --git a/Service/Rfid/ViewEventArgs.cs b/Service/Rfid/ViewEventArgs.cs new file mode 100644 index 0000000..f81b526 --- /dev/null +++ b/Service/Rfid/ViewEventArgs.cs @@ -0,0 +1,21 @@ +namespace Service.Rfid; + +public class ViewEventArgs : EventArgs +{ + /// + /// 连接状态 + /// + public bool IsConnected { get; set; } + /// + /// 连接结果消息 + /// + public string MsgContent { get; set; } +} + +public class ViewMsgEventArgs : EventArgs +{ + /// + /// 发送或接收消息 + /// + public string MsgContent { get; set; } +} \ No newline at end of file diff --git a/Service/Service.csproj b/Service/Service.csproj index 2f92105..5c2870a 100644 --- a/Service/Service.csproj +++ b/Service/Service.csproj @@ -39,4 +39,10 @@ + + + bin\Debug\net6.0-windows\SRGReaderAPI.dll + + +