| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Core = Invengo.NetAPI.Core;
- using IRP1 = Invengo.NetAPI.Protocol.IRP1;
- using Invengo.NetAPI.Protocol.IRP1;
- using Invengo.NetAPI.Core;
- using System.Configuration;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading;
- using System.Windows.Forms;
- using System.Net.NetworkInformation;
- namespace CarLocalMeter
- {
- public class RfIdCarNo
- {
- Log lg = Log.GetInstance();
- IRP1.Reader reader = new IRP1.Reader("Reader1", "TCPIP_Client", $"{AppConfigCache.rfidIP}:{AppConfigCache.rfidPort}");//网口
- IRP1.ReadTag scanMsg = new IRP1.ReadTag(IRP1.ReadTag.ReadMemoryBank.EPC_6C);//扫描消息
- bool blThreadFlag = true;
- object lockobj = new object();//显示数据锁定
- public RfIdCarNo()
- {
- IRP1.Reader.OnApiException += new Core.ApiExceptionHandle(Reader_OnApiException);
- }
- public void Start()
- {
- if (reader.Connect())
- {
- //注册接收读写器消息事件
- reader.OnMessageNotificationReceived += new Invengo.NetAPI.Core.MessageNotificationReceivedHandle(reader_OnMessageNotificationReceived);
- lg.WriteLog(LogType.RfidLoc, "连接成功");
- if (reader != null && reader.IsConnected)
- {
- if (reader.Send(scanMsg))
- {
- lg.WriteLog(LogType.RfidLoc, "正在读卡...");
- }
- }
- }
- else
- {
- lg.WriteLog(LogType.RfidLoc, "连接失败");
- }
- }
- public void ClosingCollect()
- {
- if (reader != null)
- {
- if (reader.IsConnected)
- {
- if (reader.Send(new IRP1.PowerOff()))//发送关功放消息
- {
- lg.WriteLog(LogType.RfidLoc, "停止读卡");
- }
- }
- reader.OnMessageNotificationReceived -= new Invengo.NetAPI.Core.MessageNotificationReceivedHandle(reader_OnMessageNotificationReceived);
- reader.Disconnect();
- }
- lg.WriteLog(LogType.RfidLoc, "关闭连接");
- }
- void reader_OnMessageNotificationReceived(Invengo.NetAPI.Core.BaseReader reader, Invengo.NetAPI.Core.IMessageNotification msg)
- {
- if (msg.StatusCode != 0)
- {
- lg.WriteLog(LogType.RfidLoc, msg.ErrInfo);
- return;
- }
- String msgType = msg.GetMessageType();
- msgType = msgType.Substring(msgType.LastIndexOf('.') + 1);
- switch (msgType)
- {
- case "RXD_TagData":
- {
- IRP1.RXD_TagData m = (IRP1.RXD_TagData)msg;
- string tagType = m.ReceivedMessage.TagType;
- display(m);
- }
- break;
- case "RXD_IOTriggerSignal_800":
- {
- IRP1.RXD_IOTriggerSignal_800 m = (IRP1.RXD_IOTriggerSignal_800)msg;
- if (m.ReceivedMessage.IsStart)
- {
- lg.WriteLog(LogType.RfidLoc, "I/O 触发,正在读卡...");
- }
- else
- {
- lg.WriteLog(LogType.RfidLoc, "I/O 触发,停止读卡");
- }
- }
- break;
- }
- }
- private void display(IRP1.RXD_TagData msg)
- {
- lock (lockobj)
- {
- string epc = Core.Util.ConvertByteArrayToHexString(msg.ReceivedMessage.EPC);
- string tid = Core.Util.ConvertByteArrayToHexString(msg.ReceivedMessage.TID);
- string userDb = Core.Util.ConvertByteArrayToHexString(msg.ReceivedMessage.UserData);
- if (string.IsNullOrEmpty(CacleCls.rfidCarNo))
- {
- CacleCls.rfidEpc = "";
- }
- if (CacleCls.rfidEpc != epc)
- {
- CacleCls.rfidEpc = epc;
- if (CacleCls.rifdInfo != null && CacleCls.rifdInfo.Count() > 0)
- {
- foreach (KeyValuePair<string, string> rfid in CacleCls.rifdInfo)
- {
- if (rfid.Key == epc)
- {
- CacleCls.rfidCarNo = rfid.Value;
- break;
- }
- }
- }
- }
- }
- }
- bool isTryReconnNet = false;
- void Reader_OnApiException(Core.ErrInfo e)
- {
- if (e.Ei.ErrCode == "FF22") //断网连接尝试
- {
- if (isTryReconnNet)
- ReConn();
- lg.WriteLog(LogType.RfidLoc, e.Ei.ErrMsg);
- }
- else if (e.Ei.ErrCode == "FF24")//发现连接作废,不作断网恢复尝试
- {
- isTryReconnNet = false;
- lg.WriteLog(LogType.RfidLoc, e.Ei.ErrMsg);
- }
- }
- #region 重连接
- int tryReconnNetTimeSpan = AppConfigCache.rfidConnTime;
- private void ReConn()
- {
- bool isSuc = false;
- using (Ping ping = new Ping())
- {
- for (int i = 0; i < tryReconnNetTimeSpan * 60; i++)
- {
- PingReply pingReply = null;
- try
- {
- pingReply = ping.Send(reader.ConnString.Substring(0, reader.ConnString.IndexOf(':')), 1000);//超时为1秒
- if (pingReply.Status != IPStatus.Success)
- {
- lg.WriteLog(LogType.RfidLoc, "Ping不通");
- continue;
- }
- else
- {
- isSuc = true;
- break;
- }
- }
- catch (Exception ex)
- {
- lg.WriteLog(LogType.RfidLoc, $"尝试自动恢复连接失败:{ex.Message}");
- return;
- }
- }
- }
- //建立连接
- if (isSuc)
- {
- isSuc = false;
- for (int i = 0; i < 3; i++)//尝试3次
- {
- if (reader.Connect())
- {
- lg.WriteLog(LogType.RfidLoc, $"尝试自动恢复连接成功");
- isSuc = true;
- break;
- }
- else
- {
- Thread.Sleep(2000);
- continue;
- }
- }
- if (!isSuc)
- lg.WriteLog(LogType.RfidLoc, $"尝试自动恢复连接失败");
- }
- }
- #endregion
- }
- }
|