From 2a708e8c30355ed8e83c3d215a2982751cd29e86 Mon Sep 17 00:00:00 2001 From: CaesarBao <445720029@qq.com> Date: Fri, 16 May 2025 16:52:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DeviceAdapter/DeviceAdapter.cs | 33 +- DeviceAdapter/Mesnac.IDeviceAdapter.csproj | 2 +- HighWay.APPDownload/HighWay.Download.csproj | 4 +- .../Properties/Resources.Designer.cs | 6 +- .../Properties/Settings.Designer.cs | 2 +- HighWay.APPDownload/app.config | 2 +- HighWayAssemble.sln | 11 +- HighWayAssemble/App.config | 32 +- HighWayAssemble/HighWayAssemble.csproj | 63 +- HighWayAssemble/Main/FrmDisplayModel.cs | 219 +-- .../Properties/Resources.Designer.cs | 2 +- .../Properties/Settings.Designer.cs | 2 +- HighWayAssemble/Protocol/ApiServer.cs | 96 ++ HighWayAssemble/Protocol/MesHttpClient.cs | 70 + HighWayAssemble/Protocol/MsgUtil.cs | 20 +- .../MyFixedHeaderCustomDataHandlingAdapter.cs | 76 - HighWayAssemble/packages.config | 18 +- Highway.Assemble.Common/ApiResponse.cs | 26 + .../Highway.Assemble.Common.csproj | 7 +- Highway.Assemble.Common/MesAlarmLightPara.cs | 12 + Highway.Assemble.Common/MesParaData.cs | 15 + Highway.Assemble.Common/MesReadPara.cs | 10 + Highway.Assemble.Common/MesReadRespon.cs | 10 + Highway.Assemble.EquipClient/EquipClient.cs | 130 +- .../Highway.Assemble.EquipClient.csproj | 2 +- Highway.Assemble.EquipClient/IEquipClient.cs | 9 +- Highway.Assemble.Sql/DbHandler.cs | 4 +- .../Highway.Assemble.Sql.csproj | 2 +- .../Highway.Assemble.SqlSugar.csproj | 3 +- Highway.Assemble.SqlSugar/app.config | 8 +- Mesnac.DeviceAdapter.Fuchs/FuchsDevice.cs | 13 +- .../Mesnac.DeviceAdapter.Fuchs.csproj | 2 +- .../HWKC_81600Device.cs | 12 +- .../Mesnac.DeviceAdapter.HWKC_81600.csproj | 2 +- Mesnac.DeviceAdapter.RFly_I160/BgTcpClient.cs | 628 ++++++++ .../ICommunicateService.cs | 17 + .../Mesnac.DeviceAdapter.RFly_I160.csproj | 73 + Mesnac.DeviceAdapter.RFly_I160/MessagePack.cs | 15 + .../Properties/AssemblyInfo.cs | 36 + .../Properties/_system~.ini | 0 .../RFly_I160Device.cs | 1326 +++++++++++++++++ .../StringChange.cs | 235 +++ Mesnac.DeviceAdapter.RFly_I160/_system~.ini | 0 43 files changed, 3014 insertions(+), 241 deletions(-) create mode 100644 HighWayAssemble/Protocol/ApiServer.cs create mode 100644 HighWayAssemble/Protocol/MesHttpClient.cs delete mode 100644 HighWayAssemble/Protocol/MyFixedHeaderCustomDataHandlingAdapter.cs create mode 100644 Highway.Assemble.Common/ApiResponse.cs create mode 100644 Highway.Assemble.Common/MesAlarmLightPara.cs create mode 100644 Highway.Assemble.Common/MesParaData.cs create mode 100644 Highway.Assemble.Common/MesReadPara.cs create mode 100644 Highway.Assemble.Common/MesReadRespon.cs create mode 100644 Mesnac.DeviceAdapter.RFly_I160/BgTcpClient.cs create mode 100644 Mesnac.DeviceAdapter.RFly_I160/ICommunicateService.cs create mode 100644 Mesnac.DeviceAdapter.RFly_I160/Mesnac.DeviceAdapter.RFly_I160.csproj create mode 100644 Mesnac.DeviceAdapter.RFly_I160/MessagePack.cs create mode 100644 Mesnac.DeviceAdapter.RFly_I160/Properties/AssemblyInfo.cs create mode 100644 Mesnac.DeviceAdapter.RFly_I160/Properties/_system~.ini create mode 100644 Mesnac.DeviceAdapter.RFly_I160/RFly_I160Device.cs create mode 100644 Mesnac.DeviceAdapter.RFly_I160/StringChange.cs create mode 100644 Mesnac.DeviceAdapter.RFly_I160/_system~.ini diff --git a/DeviceAdapter/DeviceAdapter.cs b/DeviceAdapter/DeviceAdapter.cs index 4b24c2f..a4a0bde 100644 --- a/DeviceAdapter/DeviceAdapter.cs +++ b/DeviceAdapter/DeviceAdapter.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; //using System.Collections.Generic; //using System.Linq; //using System.Text; @@ -6,7 +7,28 @@ namespace Mesnac.DeviceAdapter { - public delegate void RecvIdentifyData(ushort iLen, byte[] pData, byte Antenna, UInt16 iDeviceId, string strId, string sensorType); + public class TagInfo + { + public byte[] PC = new byte[2]; + + public int Count; + + public int RSSI; + + public int Antana; + + public byte[] EPC; + + public byte[] Data; + + public string PCstring = null; + + public string EPCstring = null; + + public DateTime Time; + } + //public delegate void RecvIdentifyData(ushort iLen, byte[] pData, byte Antenna, UInt16 iDeviceId, string strId, string sensorType); + public delegate void RecvIdentifyData(ushort iLen, List tagInfos, byte Antenna, UInt16 iDeviceId, string strId, string sensorType); public enum G2MemBank { @@ -141,6 +163,8 @@ namespace Mesnac.DeviceAdapter /// 失败重读次数 Byte Device_GetOneIdentifyData(ref Byte[] pReadData, Byte Antenna, UInt16 Timedout, int ReadCounts); + List Device_GetAllIdentifyData(Byte Antenna, UInt16 Timedout, int ReadCounts); + /// /// 根据天线号识别单个标签EPC数据,只返回一条 /// @@ -210,6 +234,13 @@ namespace Mesnac.DeviceAdapter /// 总长度,0为失败 /// 获得的自报数据,格式为长度,数据,。。。。。。长度,数据,其中长度占一个字节 UInt16 Device_GetReportData(ref byte[] pReadData, Byte Antenna, UInt32 Timedout); + + /// + /// 点亮报警灯 + /// + /// + /// + bool Device_WriteAlarmLight(int port, UInt16 Timedout); } } diff --git a/DeviceAdapter/Mesnac.IDeviceAdapter.csproj b/DeviceAdapter/Mesnac.IDeviceAdapter.csproj index 5eecc3c..dd8d87d 100644 --- a/DeviceAdapter/Mesnac.IDeviceAdapter.csproj +++ b/DeviceAdapter/Mesnac.IDeviceAdapter.csproj @@ -9,7 +9,7 @@ Properties Mesnac.DeviceAdapter DeviceAdapter - v4.5 + v4.6.1 512 diff --git a/HighWay.APPDownload/HighWay.Download.csproj b/HighWay.APPDownload/HighWay.Download.csproj index 541d185..84e6205 100644 --- a/HighWay.APPDownload/HighWay.Download.csproj +++ b/HighWay.APPDownload/HighWay.Download.csproj @@ -9,7 +9,7 @@ Properties HighWay.Download 自动更新 - v4.0 + v4.6.1 512 @@ -22,6 +22,7 @@ DEBUG;TRACE prompt 4 + false AnyCPU @@ -31,6 +32,7 @@ TRACE prompt 4 + false System_Update.ico diff --git a/HighWay.APPDownload/Properties/Resources.Designer.cs b/HighWay.APPDownload/Properties/Resources.Designer.cs index 2c64ad9..810e8b6 100644 --- a/HighWay.APPDownload/Properties/Resources.Designer.cs +++ b/HighWay.APPDownload/Properties/Resources.Designer.cs @@ -19,7 +19,7 @@ namespace HighWay.Download.Properties { // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen // (以 /str 作为命令选项),或重新生成 VS 项目。 - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] internal class Resources { @@ -47,8 +47,8 @@ namespace HighWay.Download.Properties { } /// - /// 重写当前线程的 CurrentUICulture 属性 - /// 重写当前线程的 CurrentUICulture 属性。 + /// 重写当前线程的 CurrentUICulture 属性,对 + /// 使用此强类型资源类的所有资源查找执行重写。 /// [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] internal static global::System.Globalization.CultureInfo Culture { diff --git a/HighWay.APPDownload/Properties/Settings.Designer.cs b/HighWay.APPDownload/Properties/Settings.Designer.cs index f880b6c..39f14af 100644 --- a/HighWay.APPDownload/Properties/Settings.Designer.cs +++ b/HighWay.APPDownload/Properties/Settings.Designer.cs @@ -12,7 +12,7 @@ namespace HighWay.Download.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.1.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.9.0.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); diff --git a/HighWay.APPDownload/app.config b/HighWay.APPDownload/app.config index fcd0c93..3dbff35 100644 --- a/HighWay.APPDownload/app.config +++ b/HighWay.APPDownload/app.config @@ -1,3 +1,3 @@ - + diff --git a/HighWayAssemble.sln b/HighWayAssemble.sln index 32e7f10..f8bb1d4 100644 --- a/HighWayAssemble.sln +++ b/HighWayAssemble.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.31515.178 +# Visual Studio Version 17 +VisualStudioVersion = 17.9.34723.18 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HighWayAssemble", "HighWayAssemble\HighWayAssemble.csproj", "{BD5F6AA1-B451-4D2B-BCD3-E69A2D1699EF}" EndProject @@ -25,6 +25,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DeviceAdapter", "DeviceAdap EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Highway.Assemble.SqlSugar", "Highway.Assemble.SqlSugar\Highway.Assemble.SqlSugar.csproj", "{59C06619-BED8-45D8-85CF-ED55D6516C65}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mesnac.DeviceAdapter.RFly_I160", "Mesnac.DeviceAdapter.RFly_I160\Mesnac.DeviceAdapter.RFly_I160.csproj", "{786B9D51-D67A-4235-B082-78E9CF4FCFB6}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -67,6 +69,10 @@ Global {59C06619-BED8-45D8-85CF-ED55D6516C65}.Debug|Any CPU.Build.0 = Debug|Any CPU {59C06619-BED8-45D8-85CF-ED55D6516C65}.Release|Any CPU.ActiveCfg = Release|Any CPU {59C06619-BED8-45D8-85CF-ED55D6516C65}.Release|Any CPU.Build.0 = Release|Any CPU + {786B9D51-D67A-4235-B082-78E9CF4FCFB6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {786B9D51-D67A-4235-B082-78E9CF4FCFB6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {786B9D51-D67A-4235-B082-78E9CF4FCFB6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {786B9D51-D67A-4235-B082-78E9CF4FCFB6}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -76,6 +82,7 @@ Global {A19A79D7-D43B-4284-9892-CA27B37AC32C} = {A015D4C2-0D7E-459A-87F0-40475DBF636B} {86E4F08E-6155-4130-A6C7-C13CFB30FFDC} = {A015D4C2-0D7E-459A-87F0-40475DBF636B} {4FADD98F-9C73-4D2F-9AC1-CF8993265D8E} = {5533A820-067D-4632-85DB-0D1D4610357A} + {786B9D51-D67A-4235-B082-78E9CF4FCFB6} = {A015D4C2-0D7E-459A-87F0-40475DBF636B} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {A184E143-CBFB-4809-9DE1-6FCD90C60186} diff --git a/HighWayAssemble/App.config b/HighWayAssemble/App.config index d5dd5d1..e2a7f9e 100644 --- a/HighWayAssemble/App.config +++ b/HighWayAssemble/App.config @@ -4,12 +4,12 @@
- + - - + + @@ -157,7 +157,31 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/HighWayAssemble/HighWayAssemble.csproj b/HighWayAssemble/HighWayAssemble.csproj index 6cac9fc..ef96079 100644 --- a/HighWayAssemble/HighWayAssemble.csproj +++ b/HighWayAssemble/HighWayAssemble.csproj @@ -9,7 +9,7 @@ Properties HighWayAssemble HighWayAssemble - v4.5.2 + v4.6.1 512 true @@ -60,8 +60,11 @@ False dll\log4net.dll - - ..\packages\Newtonsoft.Json.3.5.8\lib\35\Newtonsoft.Json.dll + + ..\packages\Microsoft.Bcl.AsyncInterfaces.8.0.0\lib\netstandard2.0\Microsoft.Bcl.AsyncInterfaces.dll + + + ..\packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll ..\dll\OPCControls.dll @@ -81,8 +84,33 @@ ..\Highway.Assemble.SqlSugar\bin\Debug\SqlSugar.dll + + ..\packages\System.Buffers.4.5.1\lib\netstandard1.1\System.Buffers.dll + + + ..\packages\System.Memory.4.5.5\lib\netstandard1.1\System.Memory.dll + + + + ..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll + + + ..\packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll + + + ..\packages\System.Text.Encodings.Web.8.0.0\lib\netstandard2.0\System.Text.Encodings.Web.dll + + + ..\packages\System.Text.Json.8.0.5\lib\netstandard2.0\System.Text.Json.dll + + + ..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\portable-net45+win8+wp8+wpa81\System.Threading.Tasks.Extensions.dll + + + ..\packages\System.ValueTuple.4.5.0\lib\netstandard1.0\System.ValueTuple.dll + @@ -93,11 +121,20 @@ - - ..\packages\TouchSocket.2.0.0-beta.130\lib\net45\TouchSocket.dll + + ..\packages\TouchSocket.3.1.0\lib\net45\TouchSocket.dll - - ..\packages\TouchSocket.Core.2.0.0-beta.130\lib\net45\TouchSocket.Core.dll + + ..\packages\TouchSocket.Core.3.1.0\lib\net45\TouchSocket.Core.dll + + + ..\packages\TouchSocket.Http.3.1.0\lib\net45\TouchSocket.Http.dll + + + ..\packages\TouchSocket.Rpc.3.1.0\lib\net45\TouchSocket.Rpc.dll + + + ..\packages\TouchSocket.WebApi.3.1.0\lib\net45\TouchSocket.WebApi.dll @@ -116,8 +153,9 @@ + + - Form @@ -181,6 +219,10 @@ {a19a79d7-d43b-4284-9892-ca27b37ac32c} Mesnac.DeviceAdapter.HWKC_81600 + + {786b9d51-d67a-4235-b082-78e9cf4fcfb6} + Mesnac.DeviceAdapter.RFly_I160 + @@ -188,6 +230,11 @@ + + + + + " 01 01" + { + string returnStr = ""; + if (bytes != null) + { + for (int i = 0; i < iLen; i++) + { + returnStr += bytes[i].ToString("X2"); + } + } + return returnStr; + } + } +} + diff --git a/Mesnac.DeviceAdapter.RFly_I160/ICommunicateService.cs b/Mesnac.DeviceAdapter.RFly_I160/ICommunicateService.cs new file mode 100644 index 0000000..9ba68c1 --- /dev/null +++ b/Mesnac.DeviceAdapter.RFly_I160/ICommunicateService.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Mesnac.DeviceAdapter.RFly_I160 +{ + public interface ICommunicateService + { + void Init(string strIp, int iPort, object objetcter, ushort iDeviceId); + bool SendMessage(MessagePack pMessagePack); + bool Connect(); + bool GetState(); + bool DisConnect(); + } + +} diff --git a/Mesnac.DeviceAdapter.RFly_I160/Mesnac.DeviceAdapter.RFly_I160.csproj b/Mesnac.DeviceAdapter.RFly_I160/Mesnac.DeviceAdapter.RFly_I160.csproj new file mode 100644 index 0000000..f7d3987 --- /dev/null +++ b/Mesnac.DeviceAdapter.RFly_I160/Mesnac.DeviceAdapter.RFly_I160.csproj @@ -0,0 +1,73 @@ + + + + + Debug + AnyCPU + {786B9D51-D67A-4235-B082-78E9CF4FCFB6} + Library + Properties + Mesnac.DeviceAdapter.RFly_I160 + Mesnac.DeviceAdapter.RFly_I160 + v4.6.1 + 512 + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + false + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + false + + + + False + ..\dll\log4net.dll + + + C:\Users\Administrator\Desktop\中间件及动态库设计\Mesnac读写器\动态库\Mesnac.Log.dll + + + + + + + + + + + + + + + + + + + + {4c8c8a86-cdd8-49ac-add0-18bf89c46f88} + Mesnac.IDeviceAdapter + + + + + \ No newline at end of file diff --git a/Mesnac.DeviceAdapter.RFly_I160/MessagePack.cs b/Mesnac.DeviceAdapter.RFly_I160/MessagePack.cs new file mode 100644 index 0000000..36a5d5d --- /dev/null +++ b/Mesnac.DeviceAdapter.RFly_I160/MessagePack.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +//using System.Threading.Tasks; + +namespace Mesnac.DeviceAdapter.RFly_I160 +{ + public class MessagePack + { + //public byte m_beginChar1 = 0xBB; //开始包 + public byte[] m_pData = null; //发送数据 + //public byte m_EndChar1 = 0x0D; //结束包 + } +} diff --git a/Mesnac.DeviceAdapter.RFly_I160/Properties/AssemblyInfo.cs b/Mesnac.DeviceAdapter.RFly_I160/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..2c65cbd --- /dev/null +++ b/Mesnac.DeviceAdapter.RFly_I160/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("Mesnac.DeviceAdapter.RFly_I160")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Microsoft")] +[assembly: AssemblyProduct("Mesnac.DeviceAdapter.RFly_I160")] +[assembly: AssemblyCopyright("Copyright © Microsoft 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] +[assembly: log4net.Config.XmlConfigurator()] +//将 ComVisible 设置为 false 将使此程序集中的类型 +//对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型, +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("a305667a-a8e7-4954-8a36-48269280e30a")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, +// 方法是按如下所示使用“*”: : +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Mesnac.DeviceAdapter.RFly_I160/Properties/_system~.ini b/Mesnac.DeviceAdapter.RFly_I160/Properties/_system~.ini new file mode 100644 index 0000000..e69de29 diff --git a/Mesnac.DeviceAdapter.RFly_I160/RFly_I160Device.cs b/Mesnac.DeviceAdapter.RFly_I160/RFly_I160Device.cs new file mode 100644 index 0000000..1428862 --- /dev/null +++ b/Mesnac.DeviceAdapter.RFly_I160/RFly_I160Device.cs @@ -0,0 +1,1326 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +//using System.Threading.Tasks; +using System.Runtime.InteropServices; +using System.Threading; +using System.Configuration; +using System.Collections; + +namespace Mesnac.DeviceAdapter.RFly_I160 +{ + [ClassInterface(ClassInterfaceType.None)] + public class RFly_I160Adapter : IDeviceAdapter + { + public log4net.ILog LogInfo = log4net.LogManager.GetLogger("RollingLogFileAppender"); //Logging 名字要在 App.config 中能找到 + + #region 全局变量声明 + private DeviceType m_iDeviceType = DeviceType.RFly_I160; + ICommunicateService m_ICommunicateService = null; + public event RecvIdentifyData RecvIdentifyDataEvent = null; + public event RecvIdentifyData RecvIdentifyData_ToMES_Event; + + public UInt16 m_iDeviceId = 0; + private string m_strIp; //读写器IP或串口号 + private int m_iPort; //读写器端口号或波特率 + private readonly string m_ReadDbm = ConfigurationManager.AppSettings["ReadDbm"]; + private readonly string m_WriteDbm = ConfigurationManager.AppSettings["WriteDbm"]; + private readonly string m_AnalysisFlag = ConfigurationManager.AppSettings["AnalysisFlag"]; //1:按照次数最多返回条码;2:按照平均功率最大返回条码 + //private Mutex mut = new Mutex(); + private ManualResetEvent BeginEvent = new ManualResetEvent(true); + + public bool TagInventory_Lable = true;//连续盘点标签标志 + + private Semaphore m_GlobalSem = new Semaphore(1, 1); + private bool m_GetHeartSuccessful = false; + private Semaphore m_GetHeartSem = new Semaphore(0, 100000); + private Semaphore m_MulEpcSem = new Semaphore(0, 100000); + private Semaphore m_StopSem = new Semaphore(0, 100000); + + private bool m_OneEpcSemSuccessful = false; + private Semaphore m_OneEpcSem = new Semaphore(0, 100000); + + private bool m_WriteAlarmSemSuccessful = false; + private Semaphore m_WriteAlarmSem = new Semaphore(0, 100000); + + private bool m_ReadDataSuccessful = false; + private bool m_Device_ReadSuccessful = false; + private byte m_ReadDataLen = 0; + private byte[] m_ReadData = null; + private Semaphore m_ReadSem = new Semaphore(0, 100000); + + private int m_BarcodeGroupCount = 0; + private byte[] m_MulAllData = null; + private byte[] m_AutoReadEPC = null; + private int m_readEPCDataLen = 0; + private byte m_OneEpcDataLen = 0; + private byte m_OneEpcAntenna = 254; + private byte[] m_OneEpcData = null; + private bool m_GetReadNoDataSuccessful; + + List tagInfos = null; + #endregion + + public int AutoReport + { + get { return AutoReport; } + set { AutoReport = value; } + } + + public int Filter + { + get { return Filter; } + set { Filter = value; } + } + + #region 设备连接部分 + public bool Device_Init(CommType iCommType, string pUrl, DeviceType iDeviceType) + { + try + { + m_iDeviceType = iDeviceType; + // LogInfo.Info("函数调用:Device_Init Start: "); + //if (iDeviceType == DeviceType.Mesnac_GRUV100) + { + if (iCommType == CommType.RJ45) //网口 + { + if (m_ICommunicateService == null) + { + m_ICommunicateService = new BgTcpClient(); + } + + string[] split = pUrl.Split(new Char[] { ':' }); + m_strIp = split[0]; + string strTemp = split[1]; + m_iPort = Convert.ToInt32(strTemp); + m_ICommunicateService.Init(m_strIp, m_iPort, this, m_iDeviceId); + // LogInfo.Info("设备初始化成功,IP:" + m_strIp + "端口号:" + m_iPort); + } + else //串口,代用串口号和波特率 + { + if (m_ICommunicateService == null) + { + return false; + } + + string[] split = pUrl.Split(new Char[] { ':' }); + m_strIp = split[0]; + string strTemp = split[1]; + m_iPort = Convert.ToInt32(strTemp); + m_ICommunicateService.Init(m_strIp, m_iPort, this, m_iDeviceId); + LogInfo.Info("设备初始化成功,串口号:" + m_strIp + "波特率:" + m_iPort); + } + } + } + catch (Exception ex) + { + LogInfo.Info("连接读写器异常:" + ex.Message); + return false; + } + return true; + } + + public bool Device_Init_Id(CommType iCommType, string pUrl, ushort iDeviceId) + { + m_iDeviceId = iDeviceId; + Device_Init(iCommType, pUrl, (DeviceType)1); + return true; + } + + public bool Device_Connect() + { + try + { + if (m_ICommunicateService != null) + { + if (m_ICommunicateService.Connect()) + { + // LogInfo.Info("Device_Connect:连接成功"); + return true; + } + else + { + LogInfo.Info("Device_Connect:连接失败"); + return false; + } + } + else + { + return false; + } + } + catch (Exception ex) + { + LogInfo.Info("Device_Connect异常:" + ex.Message); + return false; + } + } + + public void Device_Destroy() + { + m_ICommunicateService.DisConnect(); + } + + public bool Device_GetState() + { + return m_ICommunicateService.GetState(); + } + + public bool Device_ReConnect() + { + LogInfo.Info("Device_Connect 调用重连函数!"); + return Device_Connect(); + } + #endregion + public bool Device_DealValidPack(byte[] ValidData) + { + byte bAntana = 0; //读写器天线号 默认0x04 + UInt16 iReadCount = 0; //标签读取次数 + UInt16 iRSSI = 0; //标签信号强度 + byte[] bResultEPC_Data = new byte[12]; + byte[] bNoData = new byte[12]; + // LogInfo.Info("----函数调用:Device_DealValidPack 开始!"); + try + { + switch (ValidData[3]) //心跳 + { + case 0X00: + if (ValidData[2] == 0X01 && ValidData[3] == 0X00) //成功 + { + m_GetHeartSuccessful = true; + } + else + { + m_GetHeartSuccessful = false; + } + m_GetHeartSem.Release(); + break; + //GPIO返回 + case 0X51: + m_WriteAlarmSemSuccessful = true; + m_WriteAlarmSem.Release(); + //this.RecvIdentifyDataEvent(1, null, bAntana, m_iDeviceId, m_strIp, "1"); + break; + } + if (ValidData[0] == 0XBB && ValidData[1] == 0XDD && ValidData[2] == 0X00 && ValidData[3] == 0x02) //超时 02 4E 6F 52 65 61 64 03 + { + // LogInfo.Info("----函数调用:Device_DealValidPack:NoRead!"); + m_BarcodeGroupCount = 0; + m_GetReadNoDataSuccessful = true; + + //m_MulEpcSem.Release(); + if (m_GetReadNoDataSuccessful && m_ReadDataSuccessful == false) + { + bAntana = 4; + m_AutoReadEPC = null; + m_GetReadNoDataSuccessful = false; + this.RecvIdentifyDataEvent(0, null, bAntana, m_iDeviceId, m_strIp,"1"); + } + else + { + m_OneEpcDataLen = 0; + m_OneEpcSem.Release(); + } + } + if (ValidData[0] == 0XBB && ValidData[1] == 0XDD && ValidData[2] != 0X00 && ValidData[3] != 0XBF) //正常有数据 + { + + // LogInfo.Info("----函数调用:Device_DealValidPack 有数据!"); + + m_BarcodeGroupCount = Convert.ToInt32(ValidData[5].ToString("X2")); //标签组数TagCount + m_MulAllData = new byte[ValidData.Length]; + Array.Clear(m_MulAllData, 0, ValidData.Length); + Array.Copy(ValidData, 0, m_MulAllData, 0, ValidData.Length); + + Array.Copy(m_MulAllData, 11, bResultEPC_Data, 0, 12); + + + + bAntana = ValidData[8]; + m_OneEpcAntenna = ValidData[8]; + iReadCount = ValidData[6]; + iRSSI = ValidData[7]; + + //Device_AutoDealContent(m_MulAllData); + tagInfos = GetTagInfos(m_MulAllData); + m_ReadDataLen = (byte)tagInfos.Count; + m_OneEpcDataLen = (byte)tagInfos.Count; + //m_OneEpcData = new byte[m_OneEpcDataLen]; + if (RecvIdentifyDataEvent != null) + { + if (m_ReadDataSuccessful == false) + { + + //this.RecvIdentifyDataEvent.Invoke(Convert.ToUInt16(Convert.ToUInt16(m_readEPCDataLen)), tagInfos, bAntana, m_iDeviceId, m_strIp,"1"); + this.RecvIdentifyDataEvent.Invoke(Convert.ToUInt16(Convert.ToUInt16(tagInfos.Count)), tagInfos, bAntana, m_iDeviceId, m_strIp,"1"); + } + else + { + m_OneEpcSem.Release(); + m_ReadSem.Release(); + } + } + } + } + catch (Exception ex) + { + LogInfo.Info("Device_DealValidPack异常:" + ex.Message); + return false; + } + return true; + } + + public byte Device_SendHeartPack() + { + byte iResult = 0; + try + { + // LogInfo.Info("函数调用:Device_SendHeartPack"); + m_GlobalSem.WaitOne(-1, false); + MessagePack pMessagePack = new MessagePack(); + pMessagePack.m_pData = new byte[9]; + Array.Clear(pMessagePack.m_pData, 0, 1);//清空为0 + //获取温度 + pMessagePack.m_pData[0] = 0xAA; + pMessagePack.m_pData[1] = 0x55; + pMessagePack.m_pData[2] = 0x00; + pMessagePack.m_pData[3] = 0x90; + pMessagePack.m_pData[4] = 0x90; + pMessagePack.m_pData[5] = 0x0D; + //pMessagePack.m_pData[0] = 0xAA; + //pMessagePack.m_pData[1] = 0x55; + //pMessagePack.m_pData[2] = 0x03; + //pMessagePack.m_pData[3] = 0xBF; + //pMessagePack.m_pData[4] = 0x01; + //pMessagePack.m_pData[5] = 0x0A; + //pMessagePack.m_pData[6] = 0x01; + //pMessagePack.m_pData[7] = 0xB6; + //pMessagePack.m_pData[8] = 0x0D; + m_GetHeartSem.WaitOne(1, false); + if (m_ICommunicateService != null) + { + if (m_ICommunicateService.SendMessage(pMessagePack)) //发送报文成功 + { + if (m_GetHeartSem.WaitOne(2000, false)) //等待结果,并取结果返回。 + { + // LogInfo.Info("发送心跳报文正常。"); + iResult = 1; //通讯连接和读写器都正常 + } + else //超时 + { + LogInfo.Info("发送心跳报文超时。"); + iResult = 2; //通讯连接器正常,读写器故障 + }; + } + else + { + //通讯连接器失败或网络故障 + LogInfo.Info("发送心跳报文失败,通讯故障。"); + iResult = 3; + } + } + else + { + return 3; + } + } + catch (Exception ex) + { + LogInfo.Info("Device_SendHeartPack:" + ex.Message); + iResult = 3; + } + finally + { + m_GlobalSem.Release(); + } + return iResult; + } + public ushort Device_GetReportData(ref byte[] pReadData, Byte Antenna, UInt32 Timedout) + { + byte[] pTemp = null; + byte iReadLen = 0; + if ((iReadLen = Device_GetOneIdentifyData(ref pTemp, Antenna, (UInt16)Timedout,0)) > 0) + { + LogInfo.Info("Device_GetReportData获取自报数据" + "数据长度" + iReadLen); + pReadData = new byte[iReadLen + 1]; + pReadData[0] = iReadLen; + Array.Copy(pTemp, 0, pReadData, 1, iReadLen); + return (ushort)(iReadLen + 1); + } + else + { + return 0; + } + } + + /// + /// 根据天线号识别单个标签EPC数据,只返回读到的第一条数据 + /// + /// 识别的标签EPC长度,0为识别失败 + /// 识别到的数据缓存区 + /// 天线号,0为本机,255为所有天线 + /// 超时时间,单位毫秒,识别到立即返回,未识别到等待超时返回 + public Byte Device_GetOneIdentifyData(ref Byte[] pReadData, Byte Antenna, UInt16 Timedout, int ReadCounts) + { + byte[] u16byte = new byte[2]; + byte iResult = 0; + byte[] bCRC = new byte[4]; + string s_CompareData1 = ""; + try + { + m_GlobalSem.WaitOne(-1, false); + // LogInfo.Info("函数调用:Device_GetOneIdentifyData"); + if (Antenna == 0) //此版本4为主机 + { + Antenna = (byte)(Antenna + 4); + } + + MessagePack pMessagePack = new MessagePack(); + + //A5 5A 00 0A 80 00 64 EE 0D 0A //100毫秒的示例 + pMessagePack.m_pData = new byte[8]; + pMessagePack.m_pData[0] = 0xAA; + pMessagePack.m_pData[1] = 0x55; + pMessagePack.m_pData[2] = 0x02; + pMessagePack.m_pData[3] = 0x02; + //pMessagePack.m_pData[4] = 0x03; + //pMessagePack.m_pData[5] = 0xE8; + u16byte = BitConverter.GetBytes(Timedout); //超时时间 + u16byte = StringChange.Swap16Bytes(u16byte); //协议里为大端在前 + // LogInfo.Info("u16byte:" + u16byte); + Array.Copy(u16byte, 0, pMessagePack.m_pData, 4, 2); + Array.Copy(pMessagePack.m_pData, 2, bCRC, 0, 4); + pMessagePack.m_pData[6] = StringChange.CalculateVerify(bCRC, bCRC.Length); + pMessagePack.m_pData[7] = 0x0D; + // LogInfo.Info("pMessagePack.m_pData:" + pMessagePack.m_pData); + //int i = m_OneEpcSem.Release(1); + m_OneEpcSem.WaitOne(1, false); + m_OneEpcDataLen = 0; + if (m_ICommunicateService.SendMessage(pMessagePack)) //发送报文成功 + { + m_ReadDataSuccessful = true; + if (m_OneEpcSem.WaitOne(Timedout + 500, false)) //等待结果,并取结果返回。 + { + if (m_OneEpcDataLen > 0) //有数据,正常 + { + pReadData = new byte[m_OneEpcDataLen]; + Array.Copy(m_AutoReadEPC, 0, pReadData, 0, m_OneEpcDataLen); + // LogInfo.Info("Device_GetOneIdentifyData:" + StringChange.bytesToHexStr(pReadData, pReadData.Length)); + iResult = m_OneEpcDataLen; + m_OneEpcDataLen = 0; + m_OneEpcAntenna = 254; + m_ReadDataSuccessful = false; + //开始对比过滤数据 + //s_CompareData1 = Encoding.ASCII.GetString(pReadData, 0, FilterData.Length); + + //if (s_CompareData1 == FilterData) + //{ + // iResult = m_OneEpcDataLen; + // m_OneEpcDataLen = 0; + // m_OneEpcAntenna = 254; + // m_ReadDataSuccessful = false; + //} + //else + //{ + // LogInfo.Info("Device_GetOneIdentifyData对比数据不匹配"); + // iResult = 0; + // m_OneEpcDataLen = 0; + // m_OneEpcAntenna = 254; + // m_ReadDataSuccessful = false; + //} + } + else + { + LogInfo.Info("Device_GetOneIdentifyData长度不正确"); + m_OneEpcDataLen = 0; + m_OneEpcAntenna = 254; + iResult = 0; + m_ReadDataSuccessful = false; + } + } + else //超时 + { + iResult = 0; + m_OneEpcDataLen = 0; + m_OneEpcAntenna = 254; + m_ReadDataSuccessful = false; + LogInfo.Info("Device_GetOneIdentifyData超时"); + } + } + else + { + m_OneEpcDataLen = 0; + m_OneEpcAntenna = 254; + LogInfo.Info("Device_GetOneIdentifyData发送识别单条EPC命令失败:"); + iResult = 0; + m_ReadDataSuccessful = false; + } + } + catch (Exception ex) + { + LogInfo.Info("Device_GetOneIdentifyData识别单条EPC数据异常:" + ex.Message); + iResult = 0; + m_ReadDataSuccessful = false; + } + finally + { + m_GlobalSem.Release(); + } + return iResult; + } + + public List Device_GetAllIdentifyData(Byte Antenna, UInt16 Timedout, int ReadCounts) + { + List tagInfo = new List(); + byte[] u16byte = new byte[2]; + byte iResult = 0; + byte[] bCRC = new byte[4]; + string s_CompareData1 = ""; + try + { + m_GlobalSem.WaitOne(-1, false); + // LogInfo.Info("函数调用:Device_GetOneIdentifyData"); + if (Antenna == 0) //此版本4为主机 + { + Antenna = (byte)(Antenna + 4); + } + + MessagePack pMessagePack = new MessagePack(); + + //A5 5A 00 0A 80 00 64 EE 0D 0A //100毫秒的示例 + pMessagePack.m_pData = new byte[8]; + pMessagePack.m_pData[0] = 0xAA; + pMessagePack.m_pData[1] = 0x55; + pMessagePack.m_pData[2] = 0x02; + pMessagePack.m_pData[3] = 0x02; + //pMessagePack.m_pData[4] = 0x03; + //pMessagePack.m_pData[5] = 0xE8; + u16byte = BitConverter.GetBytes(Timedout); //超时时间 + u16byte = StringChange.Swap16Bytes(u16byte); //协议里为大端在前 + // LogInfo.Info("u16byte:" + u16byte); + Array.Copy(u16byte, 0, pMessagePack.m_pData, 4, 2); + Array.Copy(pMessagePack.m_pData, 2, bCRC, 0, 4); + pMessagePack.m_pData[6] = StringChange.CalculateVerify(bCRC, bCRC.Length); + pMessagePack.m_pData[7] = 0x0D; + // LogInfo.Info("pMessagePack.m_pData:" + pMessagePack.m_pData); + //int i = m_OneEpcSem.Release(1); + m_OneEpcSem.WaitOne(1, false); + m_OneEpcDataLen = 0; + if (m_ICommunicateService.SendMessage(pMessagePack)) //发送报文成功 + { + m_ReadDataSuccessful = true; + if (m_OneEpcSem.WaitOne(Timedout + 500, false)) //等待结果,并取结果返回。 + { + if (m_OneEpcDataLen > 0) //有数据,正常 + { + tagInfo = tagInfos; + //Array.Copy(m_AutoReadEPC, 0, pReadData, 0, m_OneEpcDataLen); + // LogInfo.Info("Device_GetOneIdentifyData:" + StringChange.bytesToHexStr(pReadData, pReadData.Length)); + iResult = m_OneEpcDataLen; + m_OneEpcDataLen = 0; + m_OneEpcAntenna = 254; + m_ReadDataSuccessful = false; + return tagInfo; + } + else + { + LogInfo.Info("Device_GetOneIdentifyData长度不正确"); + m_OneEpcDataLen = 0; + m_OneEpcAntenna = 254; + iResult = 0; + m_ReadDataSuccessful = false; + } + } + else //超时 + { + iResult = 0; + m_OneEpcDataLen = 0; + m_OneEpcAntenna = 254; + m_ReadDataSuccessful = false; + LogInfo.Info("Device_GetOneIdentifyData超时"); + } + } + else + { + m_OneEpcDataLen = 0; + m_OneEpcAntenna = 254; + LogInfo.Info("Device_GetOneIdentifyData发送识别单条EPC命令失败:"); + iResult = 0; + m_ReadDataSuccessful = false; + } + } + catch (Exception ex) + { + LogInfo.Info("Device_GetOneIdentifyData识别单条EPC数据异常:" + ex.Message); + iResult = 0; + m_ReadDataSuccessful = false; + } + finally + { + m_GlobalSem.Release(); + } + return tagInfo; + } + + + + public UInt16 Device_Read(G2MemBank filterMembank, UInt16 filterWordPtr, UInt16 filterWordCnt, Byte[] filterData, + G2MemBank Membank, UInt16 WordPtr, UInt16 WordCnt, ref Byte[] pReadData, byte Antenna, int ReadCounts) + { + byte[] u16byte = new byte[2]; + byte iResult = 0; + try + { + m_GlobalSem.WaitOne(-1, false); + LogInfo.Info("函数调用:Device_Read"); + + MessagePack pMessagePack = new MessagePack(); + pMessagePack.m_pData = new byte[8]; + Array.Clear(pMessagePack.m_pData, 0, pMessagePack.m_pData.Length);//清空为0 + + pMessagePack.m_pData[0] = 0xAA; + pMessagePack.m_pData[1] = 0x55; + pMessagePack.m_pData[2] = 0x02; + pMessagePack.m_pData[3] = 0x02; + pMessagePack.m_pData[4] = 0x03; + pMessagePack.m_pData[5] = 0xE8; + pMessagePack.m_pData[6] = 0xEB; + pMessagePack.m_pData[7] = 0x0D; + //m_ReadSem.Release(1); + m_ReadSem.WaitOne(1, false); + m_ReadDataLen = 0; + if (m_ICommunicateService.SendMessage(pMessagePack)) //发送报文成功 + { + m_ReadDataSuccessful = true; + if (m_ReadSem.WaitOne(2000, false)) //等待结果,并取结果返回。 + { + if (m_ReadDataLen > 0) //有数据,正常 + { + pReadData = new byte[m_ReadDataLen]; + Array.Copy(m_OneEpcData, 0, pReadData, 0, m_ReadDataLen); + iResult = m_ReadDataLen; + LogInfo.Info("Device_Read:" + StringChange.bytesToHexStr(pReadData, pReadData.Length)); + + m_ReadDataLen = 0; + } + else + { + m_ReadDataLen = 0; + LogInfo.Info("Device_Read失败,返回长度为0"); + iResult = 0; + } + } + else //超时 + { + LogInfo.Info("Device_Read失败,超时未返回"); + iResult = 0; + } + } + else + { + LogInfo.Info("发送读取命令失败"); + iResult = 0; + } + } + catch (Exception ex) + { + LogInfo.Info("读取数据异常:" + ex.Message); + iResult = 0; + } + finally + { + m_GlobalSem.Release(); + } + return iResult; + + } + public UInt16 Device_Write(G2MemBank filterMembank, UInt16 filterWordPtr, UInt16 filterWordCnt, Byte[] filterData, + G2MemBank Membank, UInt16 WordPtr, UInt16 WordCnt, Byte[] pWriteData, byte Antenna) + { + return 1; + } + public bool Device_WriteAlarmLight(int port, UInt16 Timedout) + { + bool iflag = false; + byte[] u16byte = new byte[2]; + byte iResult = 0; + byte[] bCRC = new byte[6]; + string s_CompareData1 = ""; + try + { + m_GlobalSem.WaitOne(-1, false); + // LogInfo.Info("函数调用:Device_GetOneIdentifyData"); + + MessagePack pMessagePack = new MessagePack(); + + //AA 55 04 51 02 00 00 00 57 0D //100毫秒的示例 + pMessagePack.m_pData = new byte[10]; + pMessagePack.m_pData[0] = 0xAA; + pMessagePack.m_pData[1] = 0x55; + pMessagePack.m_pData[2] = 0x04; + pMessagePack.m_pData[3] = 0x51; + pMessagePack.m_pData[4] = (byte)port; + pMessagePack.m_pData[5] = 0x00; + //pMessagePack.m_pData[6] = 0x00; + //pMessagePack.m_pData[7] = 0x00; + u16byte = BitConverter.GetBytes(Timedout); //超时时间 + u16byte = StringChange.Swap16Bytes(u16byte); //协议里为大端在前 + // LogInfo.Info("u16byte:" + u16byte); + Array.Copy(u16byte, 0, pMessagePack.m_pData, 6, 2); + Array.Copy(pMessagePack.m_pData, 2, bCRC, 0, 6); + pMessagePack.m_pData[8] = StringChange.CalculateVerify(bCRC, bCRC.Length); + pMessagePack.m_pData[9] = 0x0D; + m_WriteAlarmSem.WaitOne(1, false); + if (m_ICommunicateService.SendMessage(pMessagePack)) //发送报文成功 + { + if (m_WriteAlarmSem.WaitOne(500, false)) //等待结果,并取结果返回。 + { + if (m_WriteAlarmSemSuccessful) + { + iflag = true; + } + } + else //超时 + { + LogInfo.Info("超时"); + } + } + else + { + LogInfo.Info("发送识别命令失败:"); + m_WriteAlarmSemSuccessful = false; + } + } + catch (Exception ex) + { + LogInfo.Info("识别数据异常:" + ex.Message); + m_WriteAlarmSemSuccessful = false; + } + finally + { + m_GlobalSem.Release(); + } + return iflag; + } + /// + /// 返回读写器获取的条码中最好的一条 1:按照读取次数最多返回条码;2:按照功率最大返回条码 + /// + /// 要处理的数据 + /// 1:按照读取次数最多返回条码;2:按照平均功率最大返回条码 + /// 1:打印log;0:不打印Log + /// + public byte[] CommandAnalysisBarcode(List cBarcodeObjList, int iFlag, int iPrintLogFlag) + { + byte[] tempBarcode = null; + if (iFlag == 1) //按次数最多获取 + { + if (iPrintLogFlag == 1) + { + LogInfo.Info("----调用通用函数:CommandAnalysisBarcode Flag=1 按照最大次数选择条码,如果次数相同 ,按照平均功率最大的选择条码:"); + } + #region 按照最大次数选择条码,如果次数相同 ,按照平均功率最大的选择条码 + #region 求出最大次数 + int iOrderMaxCount = cBarcodeObjList[0].Count; + for (int i = 0; i < cBarcodeObjList.Count; i++) + { + if (iPrintLogFlag == 1) + { + #region 打印所有条码 次数和平均功率 + LogInfo.Info("条码:[ " + StringChange.bytesToHexStr(cBarcodeObjList[i].EPC, cBarcodeObjList[i].EPC.Length) + " ] 次数:[ " + cBarcodeObjList[i].Count.ToString() + " ] 平均最大功率: [" + cBarcodeObjList[i].RSSI.ToString() + " ]"); + #endregion + } + + if (cBarcodeObjList[i].Count > iOrderMaxCount) + { + iOrderMaxCount = cBarcodeObjList[i].Count; + } + } + if (iPrintLogFlag == 1) + { + LogInfo.Info("按照次数优先,最大次数为:" + iOrderMaxCount.ToString()); + } + #endregion + #region 求出等于最大次数的所有条码对象 + List cBarcodeObjListCount = new List(); + foreach (TagInfo itemMax in cBarcodeObjList) + { + if (itemMax.Count == iOrderMaxCount) + { + cBarcodeObjListCount.Add(itemMax); + LogInfo.Info("按照次数优先,等于最大次数条码为:" + StringChange.bytesToHexStr(itemMax.EPC, itemMax.EPC.Length)); + } + } + if (iPrintLogFlag == 1) + { + LogInfo.Info("按照次数优先,等于最大次数条码数量为:" + cBarcodeObjListCount.Count.ToString()); + } + #endregion + #region 求出最优条码 + + if (cBarcodeObjListCount.Count == 1) //如果只有一条 + { + + foreach (TagInfo itemCount in cBarcodeObjListCount) + { + if (itemCount.Count == iOrderMaxCount) + { + if (iPrintLogFlag == 1) + { + LogInfo.Info("---- 取出的最优条码是: " + StringChange.bytesToHexStr(itemCount.EPC, itemCount.EPC.Length)); + } + tempBarcode = new byte[itemCount.EPC.Length]; + Array.Copy(itemCount.EPC, 0, tempBarcode, 0, itemCount.EPC.Length); + } + } + //Add By baogq 2019年5月24日 15:29:20 + //tempBarcode = new byte[cBarcodeObjListCount[0].EPC.Length]; + //tempBarcode = cBarcodeObjListCount[0].EPC; + //Array.Copy(cBarcodeObjListCount[0].EPC, 0, tempBarcode, 0, cBarcodeObjListCount[0].EPC.Length); + cBarcodeObjListCount.Clear(); + return tempBarcode; + } + else //如果有多条 + { + #region 求出最大次数相同下条码的强度最强的条码 + #region 求出最大次数相同的条码中,强度的最大值 + float iOrderAvgMaxPower = cBarcodeObjListCount[0].RSSI; + for (int i = 0; i < cBarcodeObjListCount.Count; i++) + { + if (cBarcodeObjListCount[i].RSSI > iOrderAvgMaxPower) + { + iOrderAvgMaxPower = cBarcodeObjListCount[i].RSSI; + } + } + if (iPrintLogFlag == 1) + { + LogInfo.Info("按照次数优先,等于最大次数条码中平均功率最大值为:" + iOrderAvgMaxPower.ToString()); + } + + #endregion + + foreach (TagInfo itemAvg in cBarcodeObjListCount) + { + if (itemAvg.RSSI == iOrderAvgMaxPower) + { + if (iPrintLogFlag == 1) + { + LogInfo.Info("---- 取出的最优条码是: " + StringChange.bytesToHexStr(itemAvg.EPC, itemAvg.EPC.Length)); + } + + tempBarcode = new byte[itemAvg.EPC.Length]; + Array.Copy(itemAvg.EPC, 0, tempBarcode, 0, itemAvg.EPC.Length); + } + } + #endregion + } + #endregion + #endregion + return tempBarcode; + } + else if (iFlag == 2)//按照功率最大获取 + { + if (iPrintLogFlag == 1) + { + LogInfo.Info("----调用通用函数:CommandAnalysisBarcode Flag=2 按照最大平均功率取出条码,如果最大平均功率相同,则按照次数最大取值最优条码:"); + } + #region 按照最大平均功率取出条码,如果最大功率相同,则按照次数最大取值最优条码 + #region 求出最大功率 + int iMaxAvgPow = cBarcodeObjList[0].RSSI; + for (int i = 0; i < cBarcodeObjList.Count; i++) + { + if (iPrintLogFlag == 1) + { + #region 打印所有条码 次数和平均功率 + LogInfo.Info("条码:[ " + StringChange.bytesToHexStr(cBarcodeObjList[i].EPC, cBarcodeObjList[i].EPC.Length) + " ] 平均最大功率: [" + cBarcodeObjList[i].RSSI.ToString() + " ] 次数:[ " + cBarcodeObjList[i].Count.ToString() + " ] "); + #endregion + } + + if (cBarcodeObjList[i].RSSI > iMaxAvgPow) + { + iMaxAvgPow = cBarcodeObjList[i].RSSI; + } + } + if (iPrintLogFlag == 1) + { + LogInfo.Info("按照平均功率最大,最大平均功率为:" + iMaxAvgPow.ToString()); + } + #endregion + #region 求出等于最大功率的所有条码对象 + List cBarcodeObjListCount = new List(); + foreach (TagInfo itemMax in cBarcodeObjList) + { + if (itemMax.RSSI == iMaxAvgPow) + { + cBarcodeObjListCount.Add(itemMax); + } + } + if (iPrintLogFlag == 1) + { + LogInfo.Info("按照平均功率最大,最大平均功率相同的条码数量为:" + cBarcodeObjListCount.Count.ToString()); + } + #endregion + #region 求出最优条码 + //如果只有一条 + if (cBarcodeObjListCount.Count == 1) + { + if (iPrintLogFlag == 1) + { + LogInfo.Info("---- 取出的最优条码是: " + StringChange.bytesToHexStr(cBarcodeObjList[0].EPC, cBarcodeObjList[0].EPC.Length)); + } + + tempBarcode = new byte[cBarcodeObjListCount[0].EPC.Length]; + Array.Copy(cBarcodeObjListCount[0].EPC, 0, tempBarcode, 0, cBarcodeObjListCount[0].EPC.Length); + } + else //如果有多条 + { + #region 求出平均功率最大相同时,条码读到次数最多的一个条码 + #region 求出平均功率最大相同条码中,最大的次数 + int iReadMaxCount = cBarcodeObjListCount[0].Count; + for (int i = 0; i < cBarcodeObjListCount.Count; i++) + { + if (cBarcodeObjListCount[i].Count > iReadMaxCount) + { + iReadMaxCount = cBarcodeObjListCount[i].Count; + } + } + if (iPrintLogFlag == 1) + { + LogInfo.Info("在最大平均功率相同的条码中,读取次数最多为:" + iReadMaxCount.ToString()); + } + #endregion + foreach (TagInfo itemReadCount in cBarcodeObjListCount) + { + if (itemReadCount.Count == iReadMaxCount) + { + if (iPrintLogFlag == 1) + { + LogInfo.Info("---- 取出的最优条码是: " + StringChange.bytesToHexStr(itemReadCount.EPC, itemReadCount.EPC.Length)); + } + + tempBarcode = new byte[itemReadCount.EPC.Length]; + Array.Copy(itemReadCount.EPC, 0, tempBarcode, 0, itemReadCount.EPC.Length); + } + } + #endregion + } + #endregion + #endregion + return tempBarcode; + } + else + { + return tempBarcode; + } + + } + #region 处理自动上传的函数 + private Mutex mutauto = new Mutex(); + private List GetTagInfos(byte[] AutoDealReportData) + { + List tagInfoList = new List(); + try + { + mutauto.WaitOne(); + int iFirstCountPos = 6; //第一次读取标签次数位置 + int iFirstRSSIPos = 7; //第一次读取标签强度位置 + int iFirstAnt = 8; + int iFirstPC = 9; //第一次读取标签天线位置 + int iFirstLeftBarcketPos = 11;//EPC数据起始位置 + UInt16 tempDataCount = 0; + int tempDataRSSI = 0; + UInt16 tempDataANT = 0; + int iBarcodeGroupCount = Convert.ToInt32(AutoDealReportData[5].ToString()); //标签组数 + int iBarcodeLength = 16; //标签长度 + int iCommonSecondFlag = 0; + int dataLength = Convert.ToInt32(AutoDealReportData[2].ToString()); + for (int j = 0; j < iBarcodeGroupCount; j++) + { + //int EPCLength = dataLength - (6 * iBarcodeGroupCount); + + TagInfo tag = new TagInfo(); + byte[] tempPCByte = new byte[2]; //取出PC + Array.Clear(tempPCByte, 0, 2); + Array.Copy(AutoDealReportData, iFirstPC, tempPCByte, 0, 2); + + //PC转二进制取前五位转十进制 + int epcLength = Convert.ToInt32(Convert.ToString(Convert.ToInt64(tempPCByte[0].ToString("X"), 16), 2).PadLeft(8, '0').Substring(0, 5), 2) * 2; + + //int pc = Convert.ToInt32(tempPCByte[0].ToString()); + //int epcLength = EPCLengthByPC(pc); + iBarcodeLength = epcLength; + + byte[] tempDataByte = new byte[epcLength]; + Array.Clear(tempDataByte, 0, iBarcodeLength); + Array.Copy(AutoDealReportData, iFirstLeftBarcketPos, tempDataByte, 0, iBarcodeLength); + + byte[] tempCountByte = new byte[1]; //取出标签次数 + Array.Clear(tempCountByte, 0, 1); + Array.Copy(AutoDealReportData, iFirstCountPos, tempCountByte, 0, 1); + tempDataCount = tempCountByte[0]; + + byte[] tempRSSIByte = new byte[1]; //取出标签强度 + Array.Clear(tempRSSIByte, 0, 1); + Array.Copy(AutoDealReportData, iFirstRSSIPos, tempRSSIByte, 0, 1); + + tempDataRSSI = StringChange.HexStringToNegative(StringChange.bytesToHexStr(tempRSSIByte, 1)); + + #region add by wenjy 20220829 取出天线号 + byte[] tempAntByte = new byte[1]; //取出天线号 + Array.Clear(tempAntByte, 0, 1); + Array.Copy(AutoDealReportData, iFirstAnt, tempAntByte, 0, 1); + tempDataANT = tempAntByte[0]; + #endregion + + tag.Count = tempDataCount; + tag.RSSI = tempDataRSSI; + tag.EPC = tempDataByte; + + //if (pc == 24) + //{ + // tag.EPCstring = StringChange.bytesToHexStr(tempDataByte, tempDataByte.Length).Substring(0, 7); + //} + //else + //{ + // tag.EPCstring = System.Text.Encoding.ASCII.GetString(tempDataByte); + //} + tag.EPCstring = System.Text.Encoding.ASCII.GetString(tempDataByte); + + tag.PC = tempPCByte; + tag.Antana = tempDataANT; + tagInfoList.Add(tag); + int iBarcodeListLen = tagInfoList.Count; //特别注意,必须这样,要不然会多一条数据 + + iFirstCountPos = iFirstCountPos + iBarcodeLength + 5; //次数 + iFirstRSSIPos = iFirstCountPos + 1; //强度 + iFirstAnt = iFirstRSSIPos + 1; //天线 + iFirstPC = iFirstAnt + 1; + iFirstLeftBarcketPos = iFirstLeftBarcketPos + iBarcodeLength + 5; + + LogInfo.Info("----函数调用:Device_AutoDealContent 第[" + (iCommonSecondFlag + 1) + "]次数据解析为:" + StringChange.bytesToHexStr(tempDataByte, tempDataByte.Length) + ",读取标签次数:[" + tempDataCount + "],标签信号强度:[" + tempDataRSSI + "],天线号:[" + tempDataANT + "]"); + iCommonSecondFlag++; + if (iCommonSecondFlag == iBarcodeGroupCount) + { + mutauto.ReleaseMutex(); + return tagInfoList; + } + } + } + catch (Exception ex) + { + LogInfo.Info("----函数调用:Device_AutoDealContent 自动处理函数异常:" + ex.ToString()); + mutauto.ReleaseMutex(); + } + return tagInfoList; + + + } + + public byte[] Device_AutoDealContent(byte[] AutoDealReportData) + { + List tagInfoList = new List(); + + byte[] bResultEPC_Data = new byte[12]; + m_AutoReadEPC = null; + m_readEPCDataLen = 0; + // LogInfo.Info("----函数调用:Device_AutoDealContent 开始!"); + try + { + mutauto.WaitOne(); + int iFirstCountPos = 6; //第一次读取标签次数位置 + int iFirstRSSIPos = 7; //第一次读取标签强度位置 + int iFirstAnt = 8; //第一次读取标签天线位置 + int iFirstLeftBarcketPos = 11;//EPC数据起始位置 + UInt16 tempDataCount = 0; + UInt16 tempDataRSSI = 0; + UInt16 tempDataANT = 0; + int iBarcodeGroupCount = Convert.ToInt32(AutoDealReportData[5].ToString("X2")); //标签组数 + int iBarcodeLength = 12; //标签长度 + int iCommonSecondFlag = 0; + for (int j = 0; j < iBarcodeGroupCount; j++) + { + TagInfo tag = new TagInfo(); + + byte[] tempDataByte = new byte[iBarcodeLength]; + Array.Clear(tempDataByte, 0, iBarcodeLength); + Array.Copy(AutoDealReportData, iFirstLeftBarcketPos, tempDataByte, 0, iBarcodeLength); + + byte[] tempCountByte = new byte[1]; //取出标签次数 + Array.Clear(tempCountByte, 0, 1); + Array.Copy(AutoDealReportData, iFirstCountPos, tempCountByte, 0, 1); + tempDataCount = tempCountByte[0]; + + byte[] tempRSSIByte = new byte[1]; //取出标签强度 + Array.Clear(tempRSSIByte, 0, 1); + Array.Copy(AutoDealReportData, iFirstRSSIPos, tempRSSIByte, 0, 1); + tempDataRSSI = tempRSSIByte[0]; + + byte[] tempAntByte = new byte[1]; //取出天线号 + Array.Clear(tempAntByte, 0, 1); + Array.Copy(AutoDealReportData, iFirstAnt, tempAntByte, 0, 1); + tempDataANT = tempAntByte[0]; + + tag.Count = tempDataCount; + tag.RSSI = tempDataRSSI; + tag.EPC = tempDataByte; + tag.EPCstring = StringChange.bytesToHexStr(tempDataByte, tempDataByte.Length); + tag.Antana = tempDataANT; + tagInfoList.Add(tag); + int iBarcodeListLen = tagInfoList.Count; //特别注意,必须这样,要不然会多一条数据 + + iFirstCountPos = iFirstCountPos + 17; + iFirstRSSIPos = iFirstCountPos + 1; + iFirstAnt = iFirstRSSIPos + 1; + iFirstLeftBarcketPos = iFirstLeftBarcketPos + 17; + + LogInfo.Info("----函数调用:Device_AutoDealContent 第[" + (iCommonSecondFlag + 1) + "]次数据解析为:" + StringChange.bytesToHexStr(tempDataByte, tempDataByte.Length) + ",读取标签次数:[" + tempDataCount + "],标签信号强度:[" + tempDataRSSI + "],天线号:[" + tempDataANT + "]"); + iCommonSecondFlag++; + if (iCommonSecondFlag == iBarcodeGroupCount) + { + byte[] bResult = new byte[12]; + bResult = CommandAnalysisBarcode(tagInfoList, 1, 1); + m_readEPCDataLen = tempDataByte.Length; + m_AutoReadEPC = new byte[m_readEPCDataLen]; + Array.Copy(bResult, 0, m_AutoReadEPC, 0, m_readEPCDataLen); + LogInfo.Info("----函数调用:Device_GetOneIdentifyData_Finish for End!"); + LogInfo.Info("------------------------------------------------------------------------------------------------------"); + LogInfo.Info("\r\n"); + mutauto.ReleaseMutex(); + return m_AutoReadEPC; + } + } + } + catch (Exception ex) + { + LogInfo.Info("----函数调用:Device_AutoDealContent 自动处理函数异常:" + ex.ToString()); + mutauto.ReleaseMutex(); + } + return m_AutoReadEPC; + } + #endregion + + + #region 该函数不实现了 + public bool Device_SetRf(int iDbi, byte Antenna, WriteOrRead RorW) + { + bool bResult = false; + try + { + //bool Set_OK; + //if (RorW == WriteOrRead.Read) + //{ + // Set_OK = MyReader.SetPower(iDbi, Convert.ToSingle(m_WriteDbm)); + // if (Set_OK) //设置功率工程 + // { + // LogInfo.Info("设置天线读功率成功!"); + // bResult = true; + // } + // else + // { + // LogInfo.Info("设置天线读功率失败;"); + // bResult = false; + // } + //} + //else + //{ + // Set_OK = MyReader.SetPower(Convert.ToSingle(m_ReadDbm), iDbi); + // if (Set_OK) //设置功率工程 + // { + // LogInfo.Info("设置天线写功率成功!"); + // bResult = true; + // } + // else + // { + // LogInfo.Info("设置天线写功率失败;"); + // bResult = false; + // } + //} + } + catch (Exception ex) + { + LogInfo.Info("Device_SetRf异常:" + ex.Message); + bResult = false; + } + return bResult; + } + + public string Device_GetOneIdentifyData(Byte Antenna, UInt16 Timedout) + { + byte[] u16byte = new byte[2]; + byte[] bCRC = new byte[4]; + string strResult = ""; + try + { + m_GlobalSem.WaitOne(-1, false); + LogInfo.Info("函数调用:Device_GetOneIdentifyData"); + if (Antenna == 0) //此版本1为主机 + { + Antenna = (byte)(Antenna + 1); + } + + MessagePack pMessagePack = new MessagePack(); + pMessagePack.m_pData = new byte[8]; + pMessagePack.m_pData[0] = 0xAA; + pMessagePack.m_pData[1] = 0x55; + pMessagePack.m_pData[2] = 0x02; + pMessagePack.m_pData[3] = 0x02; + //pMessagePack.m_pData[4] = 0x03; + //pMessagePack.m_pData[5] = 0xE8; + u16byte = BitConverter.GetBytes(Timedout); //超时时间 + u16byte = StringChange.Swap16Bytes(u16byte); //协议里为大端在前 + LogInfo.Info("u16byte:" + u16byte); + Array.Copy(u16byte, 0, pMessagePack.m_pData, 4, 2); + Array.Copy(pMessagePack.m_pData, 2, bCRC, 0, 4); + pMessagePack.m_pData[6] = StringChange.CalculateVerify(bCRC, bCRC.Length); + pMessagePack.m_pData[7] = 0x0D; + + //m_OneEpcSem.Release(1); + m_OneEpcSem.WaitOne(1, false); + m_OneEpcDataLen = 0; + if (m_ICommunicateService.SendMessage(pMessagePack)) //发送报文成功 + { + if (m_OneEpcSem.WaitOne(Timedout + 500, false)) //等待结果,并取结果返回。 + { + if ((m_OneEpcDataLen >= 1 && m_OneEpcAntenna == Antenna) || Antenna == 255) //有数据,正常 + { + strResult = StringChange.bytesToHexStr(m_OneEpcData, m_OneEpcDataLen); + //pReadData = new byte[m_OneEpcDataLen]; + //Array.Copy(m_OneEpcData, 0, pReadData, 0, m_OneEpcDataLen); + LogInfo.Info("Device_GetOneIdentifyData:" + StringChange.bytesToHexStr(m_OneEpcData, m_OneEpcDataLen)); + + m_OneEpcDataLen = 0; + m_OneEpcAntenna = 254; + } + else + { + strResult = StringChange.bytesToHexStr(m_OneEpcData, m_OneEpcDataLen); + LogInfo.Info("Device_GetOneIdentifyData:" + StringChange.bytesToHexStr(m_OneEpcData, m_OneEpcDataLen)); + + m_OneEpcDataLen = 0; + m_OneEpcAntenna = 254; + strResult = ""; + } + } + else //超时 + { + m_OneEpcDataLen = 0; + m_OneEpcAntenna = 254; + LogInfo.Info("Device_GetOneIdentifyData超时未返回"); + strResult = ""; + } + } + else + { + m_OneEpcDataLen = 0; + m_OneEpcAntenna = 254; + LogInfo.Info("Device_GetOneIdentifyData发送识别单条EPC命令超时"); + strResult = ""; + } + } + catch (Exception ex) + { + LogInfo.Info("Device_GetOneIdentifyData识别单条EPC数据异常:" + ex.Message); + strResult = ""; + } + finally + { + m_GlobalSem.Release(); + } + return strResult; + } + + public string Device_GetOneIdentifyData_Finish(byte Antenna, ushort Timedout) + { + return ""; + } + public Byte Device_GetOneIdentifyData_Finish(ref byte[] pReadData, byte Antenna, ushort Timedout) + { + return 0; + } + public bool Device_BeginIdentify() + { + byte[] u16byte = new byte[2]; + bool bResult = false; + try + { + LogInfo.Info("函数调用:Device_BeginIdentify"); + MessagePack pMessagePack = new MessagePack(); + + pMessagePack.m_pData = new byte[1]; + + if (m_ICommunicateService.SendMessage(pMessagePack)) //发送报文成功 + { + if (BeginEvent.WaitOne(200, false)) //等待结果,并取结果返回。 + { + bResult = true; + } + else //超时 + { + bResult = false; + } + } + else + { + LogInfo.Info("发送开始连续识别命令失败:"); + bResult = false; + } + } + catch (Exception ex) + { + LogInfo.Info("发送开始连续识别命令异常:" + ex.Message); + bResult = false; + } + return bResult; + } + + public bool Device_StopIdentify() + { + bool bResult = false; + try + { + LogInfo.Info("函数调用:Device_StopIdentify"); + MessagePack pMessagePack = new MessagePack(); + + pMessagePack.m_pData = new byte[1]; + if (m_ICommunicateService.SendMessage(pMessagePack)) //发送报文成功 + { + if (m_StopSem.WaitOne(200, false)) //等待结果,并取结果返回。 + { + bResult = true; + } + else //超时 + { + bResult = false; + } + } + else + { + LogInfo.Info("发送停止连续识别命令失败:"); + bResult = false; + } + } + catch (Exception ex) + { + LogInfo.Info("发送停止连续识别命令异常:" + ex.Message); + bResult = false; + } + return bResult; + } + + public ushort Device_GetIdentifyData(ref byte[] pReadData, byte Antenna) + { + return 0; + } + + public bool Device_BeginIdentify(byte Antenna, int timeout, bool AutoRead, int num) + { + throw new NotImplementedException(); + } + + public bool Device_StopIdentify(byte Antenna) + { + throw new NotImplementedException(); + } + #endregion + } +} diff --git a/Mesnac.DeviceAdapter.RFly_I160/StringChange.cs b/Mesnac.DeviceAdapter.RFly_I160/StringChange.cs new file mode 100644 index 0000000..a9af65c --- /dev/null +++ b/Mesnac.DeviceAdapter.RFly_I160/StringChange.cs @@ -0,0 +1,235 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Mesnac.DeviceAdapter.RFly_I160 +{ + public static class StringChange + { + //CRC异或校验 + public static byte CalculateVerify(byte[] pMessage, int iLength) + { + UInt16 i; + byte iVerify = 0; + + iVerify = pMessage[0]; + for (i = 1; i < iLength; i++) + { + iVerify = (byte)(iVerify ^ pMessage[i]); + } + return iVerify; + } + public static byte[] Swap16Bytes(byte[] OldU16) + { + byte[] ReturnBytes = new byte[2]; + ReturnBytes[1] = OldU16[0]; + ReturnBytes[0] = OldU16[1]; + return ReturnBytes; + } + + public static bool CompareBytes(byte[] byteA, byte[] byteB, int iLen) + { + for (int i = 0; i < iLen; i++) + { + if (byteA[i] != byteB[i]) + { + return false; + } + } + return true; + } + + /// + /// 将byte数组转换成十六进制字符串 //e.g. { 0x01, 0x01} ---> " 01 01" + /// + /// byte数组 + /// 数组长度 + /// 十六进制字符串 + public static string bytesToHexStr(byte[] bytes , int iLen) + { + string returnStr = ""; + if (bytes != null) + { + for (int i = 0; i < iLen; i++) + { + returnStr += bytes[i].ToString("X2"); + } + } + return returnStr; + } + + public static byte[] HexStrTorbytes(string strHex)//e.g. " 01 01" ---> { 0x01, 0x01} + { + strHex = strHex.Replace(" ", ""); + if ((strHex.Length % 2) != 0) + strHex += " "; + byte[] returnBytes = new byte[strHex.Length / 2]; + for (int i = 0; i < returnBytes.Length; i++) + returnBytes[i] = Convert.ToByte(strHex.Substring(i * 2, 2), 16); + return returnBytes; + } + public static string StringToHexString(string s, Encoding encode) + { + byte[] b = encode.GetBytes(s); //按照指定编码将string编程字节数组 + string result = string.Empty; + for (int i = 0; i < b.Length; i++) //逐字节变为16进制字符,以%隔开 + { + result += "%" + Convert.ToString(b[i], 16); + } + return result; + } + public static string HexStringToString(string hs, Encoding encode) + { + //以%分割字符串,并去掉空字符 + string[] chars = hs.Split(new char[] { '%' }, StringSplitOptions.RemoveEmptyEntries); + byte[] b = new byte[chars.Length]; + //逐个字符变为16进制字节数据 + for (int i = 0; i < chars.Length; i++) + { + b[i] = Convert.ToByte(chars[i], 16); + } + //按照指定编码将字节数组变为字符串 + return encode.GetString(b); + } + + public static short SwapInt16(this short n) + { + return (short)(((n & 0xff) << 8) | ((n >> 8) & 0xff)); + } + + public static ushort SwapUInt16(this ushort n) + { + return (ushort)(((n & 0xff) << 8) | ((n >> 8) & 0xff)); + } + + public static int SwapInt32(this int n) + { + return (int)(((SwapInt16((short)n) & 0xffff) << 0x10) | + (SwapInt16((short)(n >> 0x10)) & 0xffff)); + } + + public static uint SwapUInt32(this uint n) + { + return (uint)(((SwapUInt16((ushort)n) & 0xffff) << 0x10) | + (SwapUInt16((ushort)(n >> 0x10)) & 0xffff)); + } + + public static long SwapInt64(this long n) + { + return (long)(((SwapInt32((int)n) & 0xffffffffL) << 0x20) | + (SwapInt32((int)(n >> 0x20)) & 0xffffffffL)); + } + + public static ulong SwapUInt64(this ulong n) + { + return (ulong)(((SwapUInt32((uint)n) & 0xffffffffL) << 0x20) | + (SwapUInt32((uint)(n >> 0x20)) & 0xffffffffL)); + } + public class IOInfo + { + public int id; + public byte[] state; + public DateTime time; + } + public class MethodFilterData + { + private static List IOInfolist = new List(); + /// + /// 消除抖动,删除短暂时间内的 + /// + /// + public static bool EliminatingJitter(int id, byte[] data) + { + var index = IOInfolist.FindIndex((x) => { return x.id == id; }); + if (index == -1 || Array.Equals(IOInfolist[index].state, data)) + { + IOInfolist.Add(new IOInfo + { + id = id, + state = data, + time = DateTime.Now + }); + } + else + { + TimeSpan ts1 = new TimeSpan(IOInfolist[index].time.Ticks); + TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks); + TimeSpan ts3 = ts2.Subtract(ts1); //ts2-ts1 + double sumSeconds = Convert.ToDouble(ts3.TotalSeconds.ToString()); //得到相差秒数 + + if (sumSeconds < 5) + { + return true; + } + else + { + IOInfolist[index] = new IOInfo + { + id = id, + state = data, + time = DateTime.Now + }; + } + } + return false; + } + /// + /// 屏蔽重复条码 + /// + /// 传感器编号 + /// + /// + public static bool IsRepeat(int id, byte[] data) + { + var index = IOInfolist.FindIndex((x) => { return x.id == id; }); + if (index == -1) + { + IOInfolist.Add(new IOInfo + { + id = id, + state = data, + time = DateTime.Now + }); + return false; + } + if (string.Equals(bytesToHexStr(IOInfolist[index].state, IOInfolist[index].state.Length), bytesToHexStr(data, data.Length))) + { + IOInfolist[index].time = DateTime.Now; + return true; + } + else + { + IOInfolist[index].state = data; + return false; + } + } + } + public static int HexStringToNegative(string strNumber) + { + + int iNegate = 0; + int iNumber = Convert.ToInt32(strNumber, 16); + if (iNumber > 127) + { + int iComplement = iNumber - 1; + string strNegate = string.Empty; + char[] binchar = Convert.ToString(iComplement, 2).PadLeft(8, '0').ToArray(); + foreach (char ch in binchar) + { + if (Convert.ToInt32(ch) == 48) + { + strNegate += "1"; + } + else + { + strNegate += "0"; + } + } + iNegate = -Convert.ToInt32(strNegate, 2); + } + return iNegate; + } + } + +} diff --git a/Mesnac.DeviceAdapter.RFly_I160/_system~.ini b/Mesnac.DeviceAdapter.RFly_I160/_system~.ini new file mode 100644 index 0000000..e69de29