From a55ed808bb8dd49633a715f7ba72bdd32b4b3358 Mon Sep 17 00:00:00 2001 From: SoulStar Date: Wed, 15 Jan 2025 21:01:15 +0800 Subject: [PATCH] =?UTF-8?q?feat=20-=20=E6=B7=BB=E5=8A=A0RFIDClientSocket?= =?UTF-8?q?=20=E8=A7=A3=E6=9E=90RFID=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HighWayIot.Common/GeneralUtils.cs | 52 + HighWayIot.Common/HighWayIot.Common.csproj | 1 + HighWayIot.Plc/HighWayIot.Plc.csproj | 4 + HighWayIot.Plc/PlcEntity/RgvStationEnum.cs | 29 + HighWayIot.Plc/PlcHelper/BasePlcHelper.cs | 12 + HighWayIot.Plc/PlcHelper/RfidResult.cs | 13 + HighWayIot.Plc/PlcHelper/WorkStationWrite.cs | 22 + HighWayIot.Rfid/BaseRFIDDataAnalyse.cs | 103 ++ HighWayIot.Rfid/Dto/MessagePack.cs | 15 - .../Entity/BaseReciveDataEntity.cs | 39 + HighWayIot.Rfid/Entity/BaseSendDataEntity.cs | 24 + HighWayIot.Rfid/ICommunicateService.cs | 22 - HighWayIot.Rfid/IDeviceAdapter.cs | 216 --- HighWayIot.Rfid/Impl/BgTcpClient.cs | 831 --------- HighWayIot.Rfid/Impl/RFLY_I160ADAPTER.CS | 1486 ----------------- .../TouchSocketTcpClient.cs | 87 + .../TouchSocketTcpServer.cs | 58 - HighWayIot.Winform/Business/GeneralUtils.cs | 21 - .../MainForm/BaseForm.Designer.cs | 18 +- HighWayIot.Winform/MainForm/BaseForm.cs | 10 + HighWayIot.Winform/MainForm/BaseForm.resx | 3 + 21 files changed, 411 insertions(+), 2655 deletions(-) create mode 100644 HighWayIot.Common/GeneralUtils.cs create mode 100644 HighWayIot.Plc/PlcEntity/RgvStationEnum.cs create mode 100644 HighWayIot.Plc/PlcHelper/BasePlcHelper.cs create mode 100644 HighWayIot.Plc/PlcHelper/RfidResult.cs create mode 100644 HighWayIot.Plc/PlcHelper/WorkStationWrite.cs create mode 100644 HighWayIot.Rfid/BaseRFIDDataAnalyse.cs delete mode 100644 HighWayIot.Rfid/Dto/MessagePack.cs create mode 100644 HighWayIot.Rfid/Entity/BaseReciveDataEntity.cs create mode 100644 HighWayIot.Rfid/Entity/BaseSendDataEntity.cs delete mode 100644 HighWayIot.Rfid/ICommunicateService.cs delete mode 100644 HighWayIot.Rfid/IDeviceAdapter.cs delete mode 100644 HighWayIot.Rfid/Impl/BgTcpClient.cs delete mode 100644 HighWayIot.Rfid/Impl/RFLY_I160ADAPTER.CS create mode 100644 HighWayIot.TouchSocket/TouchSocketTcpClient.cs delete mode 100644 HighWayIot.TouchSocket/TouchSocketTcpServer.cs diff --git a/HighWayIot.Common/GeneralUtils.cs b/HighWayIot.Common/GeneralUtils.cs new file mode 100644 index 0000000..5c7395f --- /dev/null +++ b/HighWayIot.Common/GeneralUtils.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace HighWayIot.Common +{ + /// + /// 通用工具类 + /// + public class GeneralUtils + { + /// + /// 获取枚举类值的description元数据(没有Des返回名字) + /// + /// + public static string GetEnumStringDescription(Enum enumValue) + { + string value = enumValue.ToString(); + FieldInfo field = enumValue.GetType().GetField(value); + object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false); //获取描述属性 + if (objs.Length == 0) //当描述属性没有时,直接返回名称 + return value; + DescriptionAttribute descriptionAttribute = (DescriptionAttribute)objs[0]; + return descriptionAttribute.Description; + } + + /// + /// 获取枚举类的键值对 + /// + /// + /// + public static IEnumerable> GetEnumKeyValuePairs() where T : Enum + { + var enumType = typeof(T); + var fields = enumType.GetFields(); + + foreach (var fi in fields) + { + if (fi.FieldType != enumType || !fi.IsLiteral) + continue; + + var name = fi.Name; + var value = (int)enumType.GetField(name).GetValue(null); + yield return new KeyValuePair(name, value); + } + } + } +} diff --git a/HighWayIot.Common/HighWayIot.Common.csproj b/HighWayIot.Common/HighWayIot.Common.csproj index 077edf4..bd3af4d 100644 --- a/HighWayIot.Common/HighWayIot.Common.csproj +++ b/HighWayIot.Common/HighWayIot.Common.csproj @@ -47,6 +47,7 @@ + diff --git a/HighWayIot.Plc/HighWayIot.Plc.csproj b/HighWayIot.Plc/HighWayIot.Plc.csproj index 0dcc92f..330708d 100644 --- a/HighWayIot.Plc/HighWayIot.Plc.csproj +++ b/HighWayIot.Plc/HighWayIot.Plc.csproj @@ -48,6 +48,10 @@ + + + + diff --git a/HighWayIot.Plc/PlcEntity/RgvStationEnum.cs b/HighWayIot.Plc/PlcEntity/RgvStationEnum.cs new file mode 100644 index 0000000..3f039e4 --- /dev/null +++ b/HighWayIot.Plc/PlcEntity/RgvStationEnum.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HighWayIot.Plc.PlcEntity +{ + public enum RgvStationEnum + { + RgvRingInstallStation = 1, + Rgv1Station, + Rgv2Station, + Rgv3Station, + Rgv4Station, + Rgv5Station, + RgvTireUnloadingStation, + RgvBendWaitStation, + Rgv3WeighCalibrationStation, + Rgv4WeighCalibrationStation, + Rgv5WeighCalibrationStation, + RgvMainLineEntranceStation, + RgvMainLineExitStation, + RgvAuxline1Station, + RgvAuxline2Station, + RgvAuxline3Station, + RgvAuxline4Station, + } +} diff --git a/HighWayIot.Plc/PlcHelper/BasePlcHelper.cs b/HighWayIot.Plc/PlcHelper/BasePlcHelper.cs new file mode 100644 index 0000000..023b5fe --- /dev/null +++ b/HighWayIot.Plc/PlcHelper/BasePlcHelper.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HighWayIot.Plc.PlcHelper +{ + public class BasePlcHelper + { + } +} diff --git a/HighWayIot.Plc/PlcHelper/RfidResult.cs b/HighWayIot.Plc/PlcHelper/RfidResult.cs new file mode 100644 index 0000000..259d9fa --- /dev/null +++ b/HighWayIot.Plc/PlcHelper/RfidResult.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HighWayIot.Plc.PlcHelper +{ + public class RfidResult + { + + } +} diff --git a/HighWayIot.Plc/PlcHelper/WorkStationWrite.cs b/HighWayIot.Plc/PlcHelper/WorkStationWrite.cs new file mode 100644 index 0000000..3453205 --- /dev/null +++ b/HighWayIot.Plc/PlcHelper/WorkStationWrite.cs @@ -0,0 +1,22 @@ +using HighWayIot.Plc.PlcEntity; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HighWayIot.Plc.PlcHelper +{ + public class WorkStationWrite + { + /// + /// RFID工位识别 + /// + /// + /// + public bool WriteStationSingal(RgvStationEnum rgvStation) + { + + } + } +} diff --git a/HighWayIot.Rfid/BaseRFIDDataAnalyse.cs b/HighWayIot.Rfid/BaseRFIDDataAnalyse.cs new file mode 100644 index 0000000..f1df7da --- /dev/null +++ b/HighWayIot.Rfid/BaseRFIDDataAnalyse.cs @@ -0,0 +1,103 @@ +using HighWayIot.Log4net; +using HighWayIot.Rfid.Entity; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HighWayIot.Rfid +{ + /// + /// RFID基础数据分析 + /// + public class BaseRFIDDataAnalyse + { + private static LogHelper _logHelper = LogHelper.Instance; + + /// + /// 基础接收数据解析 + /// + /// + /// + public BaseReciveDataEntity BaseAnalyse(byte[] data) + { + BaseReciveDataEntity result = new BaseReciveDataEntity(); + + int index = 0; + index += 2; + + //取Length + result.DataLength = data[index]; + index++; + + //取Code + result.Code = data[index]; + index++; + + //取Status + result.Status = data[index]; + index++; + + //取Data + Array.Copy(data, 5, result.Data, 0, result.DataLength); + index += result.DataLength; + + //算Xor + result.Xor = data[index]; + int xor = 0; + for (int i = 0; i < 3 + result.DataLength; i++) + { + xor ^= data[i]; + } + if(xor != data[data.Length - 1]) + { + _logHelper.Error("数据校验和未通过"); + return new BaseReciveDataEntity(); + } + + return result; + } + + /// + /// 发送数据封装 + /// + /// + /// + public byte[] BaseSendDataAnalyse(BaseSendDataEntity entity) + { + byte[] result = new byte[entity.Data.Length + 6]; + + int index = 2; + + //指令头 + result[0] = 0xBB; + result[1] = 0xDD; + + //数据长度 + result[index] = (byte)entity.Data.Length; + index++; + + //指令编号 + result[index] = entity.Code; + + //数据 + Array.Copy(entity.Data, 0, result, 4, entity.Data.Length); + index += entity.Data.Length; + + //校验和 + int xor = 0; + for (int i = 2; i < 2 + entity.Data.Length; i++) + { + xor ^= result[i]; + } + result[index] = (byte)xor; + index++; + + //帧尾 + result[index] = 0x0D; + + return result; + } + } +} diff --git a/HighWayIot.Rfid/Dto/MessagePack.cs b/HighWayIot.Rfid/Dto/MessagePack.cs deleted file mode 100644 index def98b6..0000000 --- a/HighWayIot.Rfid/Dto/MessagePack.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace HighWayIot.Rfid.Dto -{ - public class MessagePack - { - //public byte m_beginChar1 = 0xBB; //开始包 - public byte[] m_pData = null; //发送数据 - //public byte m_EndChar1 = 0x0D; //结束包 - } -} diff --git a/HighWayIot.Rfid/Entity/BaseReciveDataEntity.cs b/HighWayIot.Rfid/Entity/BaseReciveDataEntity.cs new file mode 100644 index 0000000..d4469d2 --- /dev/null +++ b/HighWayIot.Rfid/Entity/BaseReciveDataEntity.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HighWayIot.Rfid.Entity +{ + /// + /// 接收数据基础类 + /// + public class BaseReciveDataEntity + { + /// + /// 数据长度2 + /// + public byte DataLength { get; set; } + + /// + /// 指令编号 + /// + public byte Code { get; set; } + + /// + /// 状态码 + /// + public byte Status { get; set; } + + /// + /// 数据 + /// + public byte[] Data { get; set; } + + /// + /// 校验位 + /// + public byte Xor { get; set; } + } +} diff --git a/HighWayIot.Rfid/Entity/BaseSendDataEntity.cs b/HighWayIot.Rfid/Entity/BaseSendDataEntity.cs new file mode 100644 index 0000000..ebde34c --- /dev/null +++ b/HighWayIot.Rfid/Entity/BaseSendDataEntity.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HighWayIot.Rfid.Entity +{ + /// + /// 发送数据基础类 + /// + public class BaseSendDataEntity + { + /// + /// 指令编号 + /// + public byte Code { get; set; } + + /// + /// 数据 + /// + public byte[] Data { get; set; } + } +} diff --git a/HighWayIot.Rfid/ICommunicateService.cs b/HighWayIot.Rfid/ICommunicateService.cs deleted file mode 100644 index cfb995b..0000000 --- a/HighWayIot.Rfid/ICommunicateService.cs +++ /dev/null @@ -1,22 +0,0 @@ -using HighWayIot.Rfid.Dto; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace HighWayIot.Rfid -{ - public interface ICommunicateService - { - void Init(string strIp, int iPort, object objetcter); - - bool SendMessage(MessagePack pMessagePack); - - bool Connect(); - - bool GetState(); - - bool DisConnect(); - } -} diff --git a/HighWayIot.Rfid/IDeviceAdapter.cs b/HighWayIot.Rfid/IDeviceAdapter.cs deleted file mode 100644 index 3e9945d..0000000 --- a/HighWayIot.Rfid/IDeviceAdapter.cs +++ /dev/null @@ -1,216 +0,0 @@ -using GRreader; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace HighWayIot.Rfid -{ - public delegate void RecvIdentifyData(ushort iLen, byte[] pData, byte Antenna, UInt16 iDeviceId, string strId); - - public enum G2MemBank - { - RESERVED = 0, //保留区 - EPC = 1, - TID = 2, - USER = 3, - }; - - public enum CommType - { - RJ45 = 1, //网口 - RS232 = 2, //com口 - RS485 = 3, //485接口 - }; - public enum DeviceType - { - Mesnac_PKRK = 1, //软控物联网读写器一体机_GRUI160 - Alien_9650 = 2, //Alien9650 - ThingMagic_Vega = 3, //ThingMagic车载读写器 - Mesnac_LD = 4, //软控磊德 - Mesnac_GRUV100 = 5, //软控物联网车载读写器_GRUV100 - Mesnac_GRUR445 = 6, //软控物联网四端口读写器 - GzgDevice = 7, //干燥柜 - - ZlanIO_101 = 101, //卓岚IO - Moxa_E1212 = 102, //摩砂E1212 - HwIo8 = 103, //海威 - RFU620 = 620, //SICK 读写器 - RFly_I160 = 160,//金瑞铭RFly-I160读写器 - HWKC_81600 = 81600,//海威IO设备 - Fuchs = 104 - }; - - public enum WriteOrRead - { - Write = 1, //写 - Read = 2, //读 - }; - - /// - /// 设备适配层接口 - /// - public interface IDeviceAdapter - { - event RecvIdentifyData RecvIdentifyDataEvent; - - /// - /// 设备初始化 - /// - /// true成功,false失败 - /// 通讯类型 1,RJ45 2,串口。 - /// 连接字符串 当iCommType为1时,pUrl格式“192.168.1.100:23”,为2时,pUrl格式为:“Com1:9600“ - /// 详见DeviceType - bool Device_Init(CommType iCommType, string pUrl, DeviceType iDeviceType); - - /// - /// 设备初始化 - /// - /// true成功,false失败 - /// 通讯类型 1,RJ45 2,串口。 - /// 连接字符串 当iCommType为1时,pUrl格式“192.168.1.100:23”,为2时,pUrl格式为:“Com1:9600“ - /// 详见DeviceType - bool Device_Init_Id(CommType iCommType, string pUrl, UInt16 iDeviceId); - - /// - /// 连接设备 - /// - /// true成功,false失败 - bool Device_Connect(); - - /// - /// 重新连接设备 - /// - /// true成功,false失败 - bool Device_ReConnect(); - - /// - /// 设备销毁 - /// - void Device_Destroy(); - - /// - /// 设备状态 - /// - /// true正常,false异常 - bool Device_GetState(); - - /// - /// 根据天线号读取单个标签数据,只返回一条 - /// //只有在天线号为0的时候可以读写其他区的数据 - /// - /// 实际读取到的长度,0为读取失败 - /// 过滤数据区域 1,保留区 2,TID区 3,EPC区 4,USER区 - /// 过滤写入起始偏移地址,单位byte - /// 过滤长度,单位byte - /// 过滤数据 - /// 数据区哉 1,保留区 2,TID区 3,EPC区 4,USER区 - /// 读取起始偏移地址,单位byte,必须为偶数。 - /// 读取长度,单位byte,必须为偶数。 - /// 读取数据存放区。 - /// 天线号,0为本机,255为所有天线 - UInt16 Device_Read(G2MemBank filterMembank, UInt16 filterWordPtr, UInt16 filterWordCnt, Byte[] filterData, G2MemBank Membank, UInt16 WordPtr, UInt16 WordCnt, ref Byte[] pReadData, byte Antenna); - - /// - /// 根据天线号写单个标签数据 - /// //只有在天线号为0的时候可以写其他区的数据 - /// - /// 0,写失败 1,写入成功 2,写入和读取的不一致 - /// 过滤数据区哉 1,保留区 2,TID区 3,EPC区 4,USER区 - /// 过滤写入起始偏移地址,单位byte - /// 过滤写入长度,单位byte - /// 过滤数据 - /// 数据区哉 1,保留区 2,TID区 3,EPC区 4,USER区 - /// 写入起始偏移地址,单位byte,必须为偶数。 - /// 写入长度,单位byte,必须为偶数。 - /// 待写入的数据 - /// 天线号,0为本机,255为所有天线 - UInt16 Device_Write(G2MemBank filterMembank, UInt16 filterWordPtr, UInt16 filterWordCnt, Byte[] filterData, - G2MemBank Membank, UInt16 WordPtr, UInt16 WordCnt, Byte[] pWriteData, byte Antenna); - - /// - /// 根据天线号识别单个标签EPC数据,只返回一条 - /// - /// 识别的标签EPC长度,0为识别失败 - /// 识别到的数据缓存区 - /// 天线号,0为本机,255为所有天线 - /// 超时时间,单位毫秒,识别到立即返回,未识别到等待超时返回 - Byte Device_GetOneIdentifyData(ref Byte[] pReadData, Byte Antenna, UInt16 Timedout); - - /// - /// 根据天线号识别单个标签EPC数据,只返回一条 - /// - /// 识别的标签EPC长度,0为识别失败 - /// 识别到的数据缓存区 - /// 天线号,0为本机,255为所有天线 - /// 超时时间,单位毫秒,统计时间内读到的次数,返回次数最多的一条 - Byte Device_GetOneIdentifyData_Finish(ref Byte[] pReadData, Byte Antenna, UInt16 Timedout); - - /// - /// 根据天线号识别单个标签EPC数据,只返回一条 - /// - /// EPC数据,例"4A474730303130323332",为""时失败 - /// 天线号,0为本机,255为所有天线 - /// 超时时间,单位毫秒,识别到立即返回,未识别到等待超时返回 - string Device_GetOneIdentifyData(Byte Antenna, UInt16 Timedout); - - /// - /// 根据天线号识别单个标签EPC数据,只返回一条 - /// - /// EPC数据,例"4A474730303130323332",为""时失败 - /// 天线号,0为本机,255为所有天线 - /// 超时时间,单位毫秒,统计时间内读到的次数,返回次数最多的一条 - string Device_GetOneIdentifyData_Finish(Byte Antenna, UInt16 Timedout); - - /// - /// 开始工作,读写器为开始识别,其他设备待定义 - /// - /// true正常,false异常 - /// 是否自动上报,1自动,0不自动,默认0 - /// 过滤规则,默认为0无规则, 后续待定 - bool Device_BeginIdentify(); - - /// - /// 停止识别,读写器为停止识别,其他设备待定义 - /// - /// true正常,false异常 - bool Device_StopIdentify(); - - /// - /// 获取工作期间的所有数据 - /// - /// 实际读取到数据的总长度包括每组数据所占的字节,0为读取失败 - /// 数据存放区,多组数据时格式为:1字节长度+天线号+每组数据... - /// 天线号,0为本机,255为读取所有天线 - UInt16 Device_GetIdentifyData(ref Byte[] pReadData, Byte Antenna); - - /// - /// 设置天线收发功率 - /// - /// true为设置成功,false为设置失败 - /// 识别到的数据缓存区 - /// 天线号,0为本机,255为所有天线 - /// Write为写,Read为读 - bool Device_SetRf(int iDbi, Byte Antenna, WriteOrRead RorW); - - /// - /// 发送心跳报文 - /// - /// 1成功,2为通讯成功,设备未返回,3为发送失败 - byte Device_SendHeartPack(); - - /// - /// 获取自报数据 - /// - /// 总长度,0为失败 - /// 获得的自报数据,格式为长度,数据,。。。。。。长度,数据,其中长度占一个字节 - UInt16 Device_GetReportData(ref byte[] pReadData, Byte Antenna, UInt32 Timedout); - /// - /// 四通道读写器按时间段读取数据 - /// - /// - /// - List Device_GetTagInfoList(DeviceType DeviceType,int time); - } -} \ No newline at end of file diff --git a/HighWayIot.Rfid/Impl/BgTcpClient.cs b/HighWayIot.Rfid/Impl/BgTcpClient.cs deleted file mode 100644 index 9ae91bc..0000000 --- a/HighWayIot.Rfid/Impl/BgTcpClient.cs +++ /dev/null @@ -1,831 +0,0 @@ -using HighWayIot.Common; -using HighWayIot.Log4net; -using HighWayIot.Rfid.Dto; -using HighWayIot.Rfid.Impl; -using MaterialTraceability.Rfid.Impl; -using System; -using System.Collections; -using System.ComponentModel; -using System.Linq; -using System.Net; -using System.Net.Sockets; -using System.Threading; - -namespace HighWayIot.Rfid.Impl -{ - public enum RecvState - { - //RFly-I160 返回数据 BB DD 00 01 40 41 0D - WaitingBeginChar1_State = 1, //等待接收帧同步字符1 0xBB - WaitingBeginChar2_State = 2, //等待接收帧同步字符2 0xDD - WaitingForBarcodeLength_State = 3, //等待条码长度不固定 - WaitingForCode_State = 4, //等待指令编号Code 0x02 - WaitingForStus_State = 5, //等待接受状态码 0x00 - WaitingForTagCount_State = 6, //等待接受标签组数不固定 - WaitingForCount_State = 7, //等待接收第一组标签读取次数 0x01 - WaitingForRSSI_State = 8, //等待接收读取信号强度 0xCB - WaitingForAnt_State = 9, //等待接收天线端口 0x01 - WaitingForPC1_State = 10, //等待接收EPC区域 0x00 - WaitingForPC2_State = 11, //等待接收EPC区域 0x00 - WaitingForData_State = 12, //等待接收数据字符 - WaitingForXor_State = 13, //等待比对校验位 - WaitingForEndChar_State = 14, //等待接收尾字符 0x0D - }; - - class BgTcpClient : ICommunicateService - { - - private LogHelper log = LogHelper.Instance; - - private StringChange stringChange = StringChange.Instance; - - RecvState enumRecvState = RecvState.WaitingBeginChar1_State; - int m_iPosition = 0; - UInt16 m_iFullMessageLength = 0; - byte m_iVerify = 0; - byte[] m_szFullMessage = new byte[1024]; //此数组用于状态机 - int iBarcodeGroupCount = 0; //读取的组数 - int iBarcodeLength = 0;//条码长度 - int iBarcodeGroupCountFlag = 0;//组数标记 - RFly_I160Adapter m_RFly_I160Adapter = null; - - private BackgroundWorker m_bgwReceive = new BackgroundWorker(); - private BackgroundWorker m_bgwDeal = new BackgroundWorker(); - private string m_FsIP = ""; - private Int32 m_FnPort = 0; - - private Socket m_ClientSock = null; - - private ArrayList m_FrecvData = new ArrayList(); - private Semaphore m_Fsem = new Semaphore(0, 100000); - private ManualResetEvent Exitevent = new ManualResetEvent(false); - - /// - /// 接收线程 - /// - /// - /// - private void bgwReceive_DoWork(object sender, DoWorkEventArgs e) - { - //LogService.Instance.Debug("RFly-I160 进入 bgwReceive_DoWork 线程函数:"); - int nPackLen = 1024; - byte[] buff = new byte[nPackLen]; - while (true) - { - try - { - if (m_ClientSock != null && m_ClientSock.Connected) - { - int nCount = m_ClientSock.Receive(buff, nPackLen, 0); - if (nCount > 0) - { - log.RfidLog(m_FsIP + ":" + m_FnPort + " RFly-I160接收原始报文:" + bytesToHexStr(buff, nCount)); - - PareReceiveBufferData(buff, nCount); //调用状态机函数 - continue; - } - else //接收错误 - { - if (m_ClientSock != null) - { - m_ClientSock.Close(); - m_ClientSock = null; - } - e.Cancel = true; - m_Fsem.Release(); - } - } - else - { - e.Cancel = true; - if (m_ClientSock != null) - { - m_ClientSock.Close(); - m_ClientSock = null; - } - m_Fsem.Release(); - return; - } - } - catch (Exception ex) - { - e.Cancel = true; - if (m_ClientSock != null) - { - m_ClientSock.Close(); - m_ClientSock = null; - } - log.Error("Socket接收数据线程退出",ex); - m_Fsem.Release(); - return; - } - if (this.m_bgwReceive.CancellationPending) - { - e.Cancel = true; - if (m_ClientSock != null) - { - m_ClientSock.Close(); - m_ClientSock = null; - } - m_Fsem.Release(); - return; - } - } - } - - /// - /// 发送线程(包括接收后的处理) - /// - /// - /// - private void bgwDeal_DoWork(object sender, DoWorkEventArgs e) - { - log.RfidLog("RFly-I160 进入 bgwDeal_DoWork 线程:"); - while (true) - { - try - { - m_Fsem.WaitOne(); - - lock (m_FrecvData) - { - if (m_FrecvData.Count > 0) - { - byte[] buff = (byte[])m_FrecvData[0]; - if (m_RFly_I160Adapter != null) - { - m_RFly_I160Adapter.Device_DealValidPack(buff); //处理函数 - } - m_FrecvData.RemoveAt(0); - } - } - if (Exitevent.WaitOne(0, false)) - { - e.Cancel = true; - log.RfidLog("Socket处理数据线程正常退出"); - Exitevent.Reset(); - return; - } - } - catch (Exception ex) - { - e.Cancel = true; - log.Error("Socket处理数据异常",ex); - return; - } - if (this.m_bgwDeal.CancellationPending) - { - log.RfidLog("Socket处理数据线程异常退出"); - e.Cancel = true; - return; - } - } - } - - /// - /// 发送函数 - /// - /// - /// - public bool SendMessage(MessagePack pMessagePack) - { - UInt16 iPos = 0; - byte[] u16byte = new byte[2]; - try - { - byte[] SendBuffer = new byte[pMessagePack.m_pData.Length]; //起始字符1 - Array.Copy(pMessagePack.m_pData, 0, SendBuffer, iPos, pMessagePack.m_pData.Length); //数据域 - - log.RfidLog(m_FsIP + ":" + m_FnPort + " RFly-I160发送原始报文:" + bytesToHexStr(SendBuffer, pMessagePack.m_pData.Length)); - - int nCount = m_ClientSock.Send(SendBuffer, pMessagePack.m_pData.Length, SocketFlags.None); - if (nCount > 0) //发送成功 - { - //LogInfo.Fatal("m_ClientSock.Send函数发送成功"); - return true; - } - else - { - log.RfidLog("连接已断开,数据发送失败"); - return false; - } - } - catch (Exception ex) - { - log.Error("发送报文异常", ex); - return false; - } - } - - #region 初始化一套函数 - public bool Connect() - { - //连接 - try - { - Exitevent.Reset(); - if (m_FsIP == "" || m_FnPort == 0) - { - log.RfidLog("IP和端口号不能为空,连接失败"); - return false; - } - if (m_ClientSock != null && GetState()) - { - log.RfidLog("已经连接了"); - return true; - } - m_ClientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - IPEndPoint iep = new IPEndPoint(IPAddress.Parse(m_FsIP), m_FnPort); - m_ClientSock.Connect(iep); - if (m_ClientSock.Connected) - { - try - { - if (!m_bgwReceive.IsBusy) - { - //启动接收线程 - m_bgwReceive.DoWork += new DoWorkEventHandler(bgwReceive_DoWork); - m_bgwReceive.RunWorkerAsync(); - } - else - { - log.RfidLog("接收线程正在运行"); - } - //启动处理线程 - if (!m_bgwDeal.IsBusy) - { - m_bgwDeal.DoWork += new DoWorkEventHandler(bgwDeal_DoWork); - m_bgwDeal.RunWorkerAsync(); - } - else - { - log.RfidLog("处理线程正在运行"); - } - return true; - } - catch (Exception ex) - { - log.Error("创建后台线程异常 ",ex); - return true; - } - } - else - { - return false; - } - } - catch (Exception ex) - { - log.Error("Socket连接异常 ",ex); - return false; - } - } - - public bool DisConnect() - { - try - { - Exitevent.Set(); - Thread.Sleep(100); - m_Fsem.Release(); - if (m_ClientSock != null) - { - log.Info("关闭Socket连接 "); - m_ClientSock.Disconnect(false); - m_ClientSock.Close(); - m_ClientSock = null; - } - return true; - } - catch (Exception ex) - { - log.Error("Socket连接异常 ",ex); - return false; - } - } - - public bool GetState() - { - bool bResult = false; - bool blockingState = false; - try - { - if (m_ClientSock != null) - { - blockingState = m_ClientSock.Blocking; - byte[] tmp = new byte[1]; - - m_ClientSock.Blocking = false; - m_ClientSock.Send(tmp, 0, 0); - bResult = true; - Console.WriteLine("Connected!"); - } - else - { - bResult = false; - } - } - catch (SocketException e) - { - // 10035 == WSAEWOULDBLOCK - if (e.NativeErrorCode.Equals(10035)) - { - bResult = true; - Console.WriteLine("Still Connected, but the Send would block"); - } - else - { - bResult = false; - Console.WriteLine("Disconnected: error code {0}!", e.NativeErrorCode); - } - } - finally - { - if (m_ClientSock != null) - { - m_ClientSock.Blocking = blockingState; - } - } - - return bResult; - } - - public void Init(string strIp, int iPort, object objetcter) - { - Exitevent.Reset(); - m_FsIP = strIp; - m_FnPort = iPort; - m_RFly_I160Adapter = objetcter as RFly_I160Adapter; - } - #endregion - - /// - /// 状态机函数 - /// - /// - /// - public void PareReceiveData(byte[] buffer, int iLen) - { - //LogService.Instance.Debug("RFly-I160进入状态机:"); - m_szFullMessage = new byte[iLen]; - for (int i = 0; i < iLen; i++) - { - switch (enumRecvState) - { - case RecvState.WaitingBeginChar1_State: //开始接受数据帧1 0xBB - //LogService.Instance.Debug("RFly-I160等待接收帧同步字符WaitingBeginChar1_State:0XBB"); - Array.Clear(m_szFullMessage, 0, iLen);//清空为0 - if (buffer[i] == 0xBB) - { - //LogService.Instance.Debug("Buffer数据为: " + StringChange.bytesToHexStr(buffer, buffer.Length)); - m_szFullMessage[m_iPosition] = buffer[i]; - m_iPosition++; - enumRecvState = RecvState.WaitingBeginChar2_State; - } - else - { - m_iFullMessageLength = 0; - m_iPosition = 0; - enumRecvState = RecvState.WaitingBeginChar1_State; - } - break; - case RecvState.WaitingBeginChar2_State: //开始接受数据帧1 0xDD - if (buffer[i] == 0xDD) - { - m_szFullMessage[m_iPosition] = buffer[i]; - m_iPosition++; - enumRecvState = RecvState.WaitingForBarcodeLength_State; - } - else - { - m_iFullMessageLength = 0; - m_iPosition = 0; - enumRecvState = RecvState.WaitingBeginChar1_State; - } - break; - case RecvState.WaitingForBarcodeLength_State: //开始接受数据长度(TagCount - EPC) - m_szFullMessage[m_iPosition] = buffer[i]; - iBarcodeLength = buffer[i]; //单组标签:18;两组标签:35 - m_iPosition++; - enumRecvState = RecvState.WaitingForCode_State; - break; - - case RecvState.WaitingForCode_State: //开始接受指令编号 - if (buffer[i] == 0x02) - { - m_szFullMessage[m_iPosition] = buffer[i]; - m_iPosition++; - enumRecvState = RecvState.WaitingForStus_State; - } - else if (buffer[i] == 0x90) // 如果是心跳BB DD 01 90 00 1F 8E 0D - { - //LogService.Instance.Debug("RFly-I160等待接受WaitingForEndChar_State:温度"); - m_szFullMessage[m_iPosition] = buffer[i]; - m_iPosition++; - enumRecvState = RecvState.WaitingForEndChar_State; - } - else if (buffer[i] == 0xBF) // 如果是心跳BB DD 04 BF 00 00 00 F9 0B 49 0D - { - //LogService.Instance.Debug("RFly-I160等待接受WaitingForEndChar_State:心跳"); - m_szFullMessage[m_iPosition] = buffer[i]; - m_iPosition++; - enumRecvState = RecvState.WaitingForEndChar_State; - } - else - { - m_iFullMessageLength = 0; - m_iPosition = 0; - enumRecvState = RecvState.WaitingBeginChar1_State; - } - break; - case RecvState.WaitingForStus_State: //开始接受状态码 - if (buffer[i] == 0x00) - { - m_szFullMessage[m_iPosition] = buffer[i]; - m_iPosition++; - enumRecvState = RecvState.WaitingForTagCount_State; - } - else if (buffer[i] == 0x40) - { - m_szFullMessage[m_iPosition] = buffer[i]; - //LogService.Instance.Debug("RFU620等待接受WaitingForEndChar_State:Noread"); - lock (m_FrecvData) - { - m_FrecvData.Add(m_szFullMessage); - } - m_Fsem.Release(); - m_iPosition = 0; - i = iLen; - enumRecvState = RecvState.WaitingBeginChar1_State; - //LogService.Instance.Debug("RFly-I160状态机结束。"); - } - break; - case RecvState.WaitingForTagCount_State: //开始接受标签组数 - Array.Copy(buffer, i, m_szFullMessage, m_iPosition, iBarcodeLength);//m_iPosition = 5 - byte[] tempData = new byte[iBarcodeLength]; - Array.Clear(tempData, 0, iBarcodeLength); - Array.Copy(buffer, i, tempData, 0, iBarcodeLength); - //LogService.Instance.Debug("解析的数据为: " + StringChange.bytesToHexStr(tempData, iBarcodeLength)); - - //m_szFullMessage[m_iPosition] = buffer[i]; //单组标签:01;两组标签:02 - m_iPosition = m_iPosition + iBarcodeLength; //m_iPosition = 39 - i = i + iBarcodeLength - 1; //i = 39 - enumRecvState = RecvState.WaitingForXor_State; - break; - case RecvState.WaitingForXor_State: //开始比对校验位 Rfly160 - byte[] m_CRCVerify = new byte[1024]; //此数组用于校验位计算 - Array.Clear(m_CRCVerify, 0, m_CRCVerify.Length); - Array.Copy(m_szFullMessage, 2, m_CRCVerify, 0, iBarcodeLength + 3); //校验位计算是从Length - EPC 结束 - m_szFullMessage[m_iPosition] = buffer[i]; - m_iVerify = m_szFullMessage[m_iPosition]; - if (m_iVerify == CalculateVerify(m_CRCVerify, m_CRCVerify.Length)) - { - m_iPosition++; - enumRecvState = RecvState.WaitingForEndChar_State; - } - else //如果校验不成功 - { - m_iFullMessageLength = 0; - m_iPosition = 0; - enumRecvState = RecvState.WaitingBeginChar1_State; - } - break; - case RecvState.WaitingForEndChar_State: - if (buffer[0] == 0xBB && buffer[1] == 0xDD && buffer[2] == 0x00 && buffer[3] != 0x90) //此处为Noread数据显示 - { - //LogService.Instance.Debug("RFly-I160等待接受WaitingForEndChar_State:Noread"); - m_szFullMessage[0] = 0xBB; - m_szFullMessage[1] = 0xDD; - m_szFullMessage[2] = 0x00; - lock (m_FrecvData) - { - m_FrecvData.Add(m_szFullMessage); - } - m_Fsem.Release(); - m_iPosition = 0; - i = iLen; - enumRecvState = RecvState.WaitingBeginChar1_State; - } - //else if (buffer[i] == 0x00) //获取温度 - //{ - // m_szFullMessage[3] = 0x00; - // m_iPosition++; - // lock (m_FrecvData) - // { - // m_FrecvData.Add(m_szFullMessage); - // } - // m_Fsem.Release(); - //} - else if (buffer[i] == 0x00) //获取温度 - { - //m_szFullMessage[3] = 0x90; - //m_iPosition++; - Array.Copy(buffer, 0, m_szFullMessage, 0, 8); - i = 8; - lock (m_FrecvData) - { - m_FrecvData.Add(m_szFullMessage); - } - m_Fsem.Release(); - //add by wenjy 2022-09-03 - //m_iPosition = 0; - //i = iLen; - //enumRecvState = RecvState.WaitingBeginChar1_State; - - } - else if (buffer[i] == 0x11) - { - //m_szFullMessage[3] = 0x00; - Array.Copy(buffer, 0, m_szFullMessage, 0, 7); - i = 7; - lock (m_FrecvData) - { - m_FrecvData.Add(m_szFullMessage); - } - m_Fsem.Release(); - } - else if (buffer[i] == 0x01) - { - //m_szFullMessage[3] = 0x00; - Array.Copy(buffer, 0, m_szFullMessage, 0, 8); - i = 8; - lock (m_FrecvData) - { - m_FrecvData.Add(m_szFullMessage); - } - m_Fsem.Release(); - } - else if (buffer[0] == 0xBB && buffer[1] == 0xDD && buffer[2] == 0x04 && buffer[3] == 0xBF) - { - m_szFullMessage[3] = 0xBF; - m_iPosition++; - lock (m_FrecvData) - { - m_FrecvData.Add(m_szFullMessage); - } - m_Fsem.Release(); - } - else - { - m_szFullMessage[m_iPosition] = buffer[i]; - m_iPosition++; - if (buffer[i] == 0x0D) - { - //LogService.Instance.Debug("RFly-I160准备发送"); - lock (m_FrecvData) - { - m_FrecvData.Add(m_szFullMessage); - } - m_Fsem.Release(); - } - } - m_iPosition = 0; - enumRecvState = RecvState.WaitingBeginChar1_State; - - //LogService.Instance.Debug("RFly-I160状态机结束。"); - break; - } - } - } - - /// - /// 状态机函数 Add By WenJy 2022-09-26 - /// - /// - /// - public void PareReceiveBufferData(byte[] buffer, int iLen) - { - var bufferStr = stringChange.bytesToHexStr(buffer, iLen); - log.RfidLog(String.Format("进入状态机函数:{0}", bufferStr)); - m_szFullMessage = new byte[iLen]; - for (int i = 0; i < iLen; i++) - { - switch (enumRecvState) - { - case RecvState.WaitingBeginChar1_State: //开始接受数据帧1 0xBB - Array.Clear(m_szFullMessage, 0, iLen);//清空为0 - if (buffer[i] == 0xBB) - { - m_szFullMessage[m_iPosition] = buffer[i]; - m_iPosition++; - enumRecvState = RecvState.WaitingBeginChar2_State; - } - else - { - m_iFullMessageLength = 0; - m_iPosition = 0; - enumRecvState = RecvState.WaitingBeginChar1_State; - } - break; - case RecvState.WaitingBeginChar2_State: //开始接受数据帧1 0xDD - if (buffer[i] == 0xDD) - { - m_szFullMessage[m_iPosition] = buffer[i]; - m_iPosition++; - enumRecvState = RecvState.WaitingForBarcodeLength_State; - } - else - { - m_iFullMessageLength = 0; - m_iPosition = 0; - enumRecvState = RecvState.WaitingBeginChar1_State; - } - break; - case RecvState.WaitingForBarcodeLength_State: //开始接受数据长度(TagCount - EPC) - m_szFullMessage[m_iPosition] = buffer[i]; - iBarcodeLength = buffer[i]; //单组标签:18;两组标签:35 - m_iPosition++; - enumRecvState = RecvState.WaitingForCode_State; - break; - - case RecvState.WaitingForCode_State: //开始接受指令编号 - if (buffer[i] == 0x02) - { - m_szFullMessage[m_iPosition] = buffer[i]; - m_iPosition++; - enumRecvState = RecvState.WaitingForStus_State; - } - else if (buffer[i] == 0x90) // 如果是心跳BB DD 01 90 00 1F 8E 0D - { - //LogService.Instance.Debug("RFly-I160等待接受WaitingForEndChar_State:温度"); - m_szFullMessage[m_iPosition] = buffer[i]; - m_iPosition++; - enumRecvState = RecvState.WaitingForEndChar_State; - } - else if (buffer[i] == 0xBF) // 如果是心跳BB DD 04 BF 00 00 00 F9 0B 49 0D - { - //LogService.Instance.Debug("RFly-I160等待接受WaitingForEndChar_State:心跳"); - m_szFullMessage[m_iPosition] = buffer[i]; - m_iPosition++; - enumRecvState = RecvState.WaitingForEndChar_State; - } - else - { - m_iFullMessageLength = 0; - m_iPosition = 0; - enumRecvState = RecvState.WaitingBeginChar1_State; - } - break; - case RecvState.WaitingForStus_State: //开始接受状态码 - if (buffer[i] == 0x00) - { - m_szFullMessage[m_iPosition] = buffer[i]; - m_iPosition++; - enumRecvState = RecvState.WaitingForTagCount_State; - } - else if (buffer[i] == 0x40) - { - m_szFullMessage[m_iPosition] = buffer[i]; - //LogService.Instance.Debug("RFU620等待接受WaitingForEndChar_State:Noread"); - lock (m_FrecvData) - { - m_FrecvData.Add(m_szFullMessage); - } - m_Fsem.Release(); - m_iPosition = 0; - i = iLen; - enumRecvState = RecvState.WaitingBeginChar1_State; - //LogService.Instance.Debug("RFly-I160状态机结束。"); - } - break; - case RecvState.WaitingForTagCount_State: //开始接受标签组数 - Array.Copy(buffer, i, m_szFullMessage, m_iPosition, iBarcodeLength);//m_iPosition = 5 - byte[] tempData = new byte[iBarcodeLength]; - Array.Clear(tempData, 0, iBarcodeLength); - Array.Copy(buffer, i, tempData, 0, iBarcodeLength); - m_iPosition = m_iPosition + iBarcodeLength; //m_iPosition = 39 - i = i + iBarcodeLength - 1; //i = 39 - enumRecvState = RecvState.WaitingForXor_State; - break; - case RecvState.WaitingForXor_State: //开始比对校验位 Rfly160 - byte[] m_CRCVerify = new byte[1024]; //此数组用于校验位计算 - Array.Clear(m_CRCVerify, 0, m_CRCVerify.Length); - Array.Copy(m_szFullMessage, 2, m_CRCVerify, 0, iBarcodeLength + 3); //校验位计算是从Length - EPC 结束 - m_szFullMessage[m_iPosition] = buffer[i]; - m_iVerify = m_szFullMessage[m_iPosition]; - if (m_iVerify == CalculateVerify(m_CRCVerify, m_CRCVerify.Length)) - { - m_iPosition++; - enumRecvState = RecvState.WaitingForEndChar_State; - } - else //如果校验不成功 - { - m_iFullMessageLength = 0; - m_iPosition = 0; - enumRecvState = RecvState.WaitingBeginChar1_State; - } - break; - case RecvState.WaitingForEndChar_State: - if (buffer[0] == 0xBB && buffer[1] == 0xDD && buffer[2] == 0x00 && buffer[3] != 0x90) //此处为Noread数据显示 - { - m_szFullMessage[0] = 0xBB; - m_szFullMessage[1] = 0xDD; - m_szFullMessage[2] = 0x00; - lock (m_FrecvData) - { - m_FrecvData.Add(m_szFullMessage); - } - m_Fsem.Release(); - m_iPosition = 0; - i = iLen; - enumRecvState = RecvState.WaitingBeginChar1_State; - } - else if (buffer[0] == 0xBB && buffer[1] == 0xDD && buffer[2] == 0x04 && buffer[3] == 0xBF) - { - Array.Copy(buffer, 0, m_szFullMessage, 0, 11); - i = 11; - lock (m_FrecvData) - { - m_FrecvData.Add(m_szFullMessage); - } - m_Fsem.Release(); - i = iLen; - } - else if (buffer[i] == 0x00) //获取温度 - { - Array.Copy(buffer, 0, m_szFullMessage, 0, 8); - i = 8; - lock (m_FrecvData) - { - m_FrecvData.Add(m_szFullMessage); - } - m_Fsem.Release(); - i = iLen; - } - else if (buffer[i] == 0x11) - { - Array.Copy(buffer, 0, m_szFullMessage, 0, 7); - i = 7; - lock (m_FrecvData) - { - m_FrecvData.Add(m_szFullMessage); - } - m_Fsem.Release(); - } - else if (buffer[i] == 0x01) - { - Array.Copy(buffer, 0, m_szFullMessage, 0, 8); - i = 8; - lock (m_FrecvData) - { - m_FrecvData.Add(m_szFullMessage); - } - m_Fsem.Release(); - } - else - { - m_szFullMessage[m_iPosition] = buffer[i]; - m_iPosition++; - if (buffer[i] == 0x0D) - { - lock (m_FrecvData) - { - m_FrecvData.Add(m_szFullMessage); - } - m_Fsem.Release(); - } - } - m_iPosition = 0; - enumRecvState = RecvState.WaitingBeginChar1_State; - break; - } - } - } - //CRC异或校验 - - public 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; - } - - private byte[] Swap16Bytes(byte[] OldU16) - { - byte[] ReturnBytes = new byte[2]; - ReturnBytes[1] = OldU16[0]; - ReturnBytes[0] = OldU16[1]; - return ReturnBytes; - } - - private string bytesToHexStr(byte[] bytes, int iLen)//e.g. { 0x01, 0x01} ---> " 01 01" - { - string returnStr = ""; - if (bytes != null) - { - for (int i = 0; i < iLen; i++) - { - returnStr += bytes[i].ToString("X2"); - } - } - return returnStr; - } - } -} - diff --git a/HighWayIot.Rfid/Impl/RFLY_I160ADAPTER.CS b/HighWayIot.Rfid/Impl/RFLY_I160ADAPTER.CS deleted file mode 100644 index 2e9f0d6..0000000 --- a/HighWayIot.Rfid/Impl/RFLY_I160ADAPTER.CS +++ /dev/null @@ -1,1486 +0,0 @@ -using GRreader; -using HighWayIot.Common; -using HighWayIot.Log4net; -using HighWayIot.Rfid; -using HighWayIot.Rfid.Dto; -using HighWayIot.Rfid.Impl; -using System; -using System.Collections.Generic; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading; - -namespace MaterialTraceability.Rfid.Impl -{ - [ClassInterface(ClassInterfaceType.None)] - public class RFly_I160Adapter : IDeviceAdapter - { - private LogHelper log = LogHelper.Instance; - private StringChange stringChange = StringChange.Instance; - private JsonChange jsonChange = JsonChange.Instance; - - #region 全局变量声明 - List m_TagInfoList = new List(); - CommType gConnetType = new CommType(); - private DeviceType m_iDeviceType = DeviceType.RFly_I160; - private DeviceType DeviceType; - ICommunicateService m_ICommunicateService = null; - public UHFreader MyReader = new UHFreader(); - public event RecvIdentifyData RecvIdentifyDataEvent = null; - public UInt16 m_iDeviceId = 0; - private string m_strIp; //读写器IP或串口号 - private int m_iPort; //读写器端口号或波特率 - private readonly string m_ReadDbm = "10"; - private readonly string m_WriteDbm = "10"; - private readonly string m_AnalysisFlag = "1"; //1:按照次数最多返回条码;2:按照平均功率最大返回条码 - private int m_ConnectFlag = 0; - //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_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 Semaphore m_WriteSem = 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; - private bool writeResult = false; - #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; - //LogService.Instance.Debug("函数调用: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); - log.RfidLog("设备初始化成功,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); - log.RfidLog("设备初始化成功,串口号:" + m_strIp + "波特率:" + m_iPort); - } - } - } - catch (Exception ex) - { - log.Error("连接读写器异常:",ex); - 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()) - { - log.RfidLog("Device_Connect:连接成功"); - return true; - } - else - { - log.RfidLog("Device_Connect:连接失败"); - return false; - } - } - else - { - return false; - } - } - catch (Exception ex) - { - log.Error("Device_Connect异常:",ex); - return false; - } - } - - public void Device_Destroy() - { - m_ICommunicateService.DisConnect(); - } - - public bool Device_GetState() - { - return m_ICommunicateService.GetState(); - } - - public bool Device_ReConnect() - { - log.RfidLog("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[14]; - byte[] bNoData = new byte[12]; - try - { - byte info = ValidData[3]; - switch (info) //心跳 - { - case 0X90: - m_GetHeartSuccessful = true; - Console.WriteLine("Device_DealValidPack处理函数:" + stringChange.bytesToHexStr(ValidData, ValidData.Length)); - try - { - log.SemaphoreLog("信号量m_GetHeartSem释放"); - m_GetHeartSem.Release(); - } - catch (Exception ex) - { - log.Error("心跳信号量处理异常", ex); - log.SemaphoreLog("信号量m_GetHeartSem释放异常:" + ex.Message); - } - break; - case 0x02: - if (ValidData[2] == 0x00 || ValidData[2] == 0x01) - { - log.RfidLog("----函数调用:Device_DealValidPack:NoRead!"); - m_BarcodeGroupCount = 0; - m_GetReadNoDataSuccessful = true; - m_OneEpcDataLen = 0; - } - else - { - - log.RfidLog("----函数调用:Device_DealValidPack 有数据!"); - - m_BarcodeGroupCount = Convert.ToInt32(ValidData[5].ToString()); //标签组数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, 14); - m_ReadDataLen = 14; - m_OneEpcDataLen = 14; - m_OneEpcData = new byte[m_OneEpcDataLen]; - - - bAntana = ValidData[8]; - m_OneEpcAntenna = ValidData[8]; - iReadCount = ValidData[6]; - iRSSI = ValidData[7]; - - //DeviceType 改为 m_iDeviceType,原代码为DeviceType,因测试一体机没有赋值改为m_iDeviceType - - //四通道读写器业务逻辑 - if (DeviceType == DeviceType.Mesnac_GRUR445) - { - m_TagInfoList = Device_DealTagInfoList2(m_MulAllData); - - } - else - { - m_TagInfoList = Device_AutoDealContent(m_MulAllData); - } - //m_OneEpcSem.Release(); - //m_ReadSem.Release(); - //log.RfidLog("----有数据,释放信号量!"); - } - try - { - log.SemaphoreLog("信号量m_OneEpcSem,释放"); - m_OneEpcSem.Release(); - //log.SemaphoreLog("信号量m_GlobalSem,释放"); - //m_GlobalSem.Release(); - } - catch(Exception e) - { - log.Error("释放信号量错误!"+e.ToString()); - log.SemaphoreLog("释放信号量错误:"+e.Message); - } - break; - default: - log.RfidLog("Device_DealValidPack处理函数:" + stringChange.bytesToHexStr(ValidData, ValidData.Length)); - Console.WriteLine("Device_DealValidPack处理函数:" + stringChange.bytesToHexStr(ValidData, ValidData.Length)); - break; - } - } - catch (Exception ex) - { - log.Error("空间名:" + ex.Source + ";" + '\n' + - "方法名:" + ex.TargetSite + '\n' + - "故障点:" + ex.StackTrace.Substring(ex.StackTrace.LastIndexOf("\\") + 1, ex.StackTrace.Length - ex.StackTrace.LastIndexOf("\\") - 1) + '\n' + - "错误提示:",ex); - return false; - } - return true; - } - - public byte Device_SendHeartPack() - { - byte iResult = 0; - try - { - log.RfidLog("函数调用: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; - m_GetHeartSem.WaitOne(1, false); - log.SemaphoreLog("信号量m_GetHeartSem,WaitOne(1, false)"); - if (m_ICommunicateService != null) - { - if (m_ICommunicateService.SendMessage(pMessagePack)) //发送报文成功 - { - log.SemaphoreLog("信号量m_GetHeartSem,WaitOne(9000, false)"); - if (m_GetHeartSem.WaitOne(5000, false)) //等待结果,并取结果返回。 - { - if (m_GetHeartSuccessful) - { - //log.RfidLog("发送心跳正常。"); - iResult = 1; //通讯连接和读写器都正常 - } - } - else //超时 - { - log.SemaphoreLog("信号量m_GetHeartSem,发送心跳超时"); - log.RfidLog("发送心跳超时。"); - iResult = 2; //通讯连接器正常,读写器故障 - }; - } - else - { - //通讯连接器失败或网络故障 - log.RfidLog("发送心跳报文失败,通讯故障。"); - iResult = 3; - } - } - else - { - //m_GetHeartSem.Release(); - return 3; - } - } - catch (Exception ex) - { - log.RfidLog("Device_SendHeartPack:"+ex.Message); - iResult = 3; - } - finally - { - //m_GlobalSem.Release(); - //m_GetHeartSem.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) - { - // log.RfidLog("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) - { - byte[] u16byte = new byte[2]; - byte iResult = 0; - byte[] bCRC = new byte[4]; - try - { - m_GlobalSem.WaitOne(1, false); - //log.RfidLog("函数调用:Device_GetOneIdentifyData"); - if (Antenna == 0) //此版本4为主机 - { - Antenna = (byte)(Antenna + 1); - } - - 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); //协议里为大端在前 - //log.RfidLog("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; - //log.RfidLog("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 + 1000, false)) //等待结果,并取结果返回。 - { - if ((m_OneEpcDataLen > 0 && m_OneEpcAntenna == Antenna)) //有数据,正常 - { - pReadData = new byte[14]; - Array.Copy(m_AutoReadEPC, 0, pReadData, 0, 14); - log.RfidLog("Device_GetOneIdentifyData:" + stringChange.bytesToHexStr(pReadData, pReadData.Length)); - - iResult = m_OneEpcDataLen; - m_OneEpcDataLen = 0; - m_OneEpcAntenna = 254; - m_ReadDataSuccessful = false; - } - else - { - log.RfidLog("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; - log.RfidLog("Device_GetOneIdentifyData超时"); - } - } - else - { - m_OneEpcDataLen = 0; - m_OneEpcAntenna = 254; - log.RfidLog("Device_GetOneIdentifyData发送识别单条EPC命令失败:"); - iResult = 0; - m_ReadDataSuccessful = false; - } - } - catch (Exception ex) - { - log.Error("Device_GetOneIdentifyData识别单条EPC数据异常:",ex); - iResult = 0; - m_ReadDataSuccessful = false; - } - finally - { - //m_GlobalSem.Release(); - } - return iResult; - } - - public UInt16 Device_Read(G2MemBank filterMembank, UInt16 filterWordPtr, UInt16 filterWordCnt, Byte[] filterData, - G2MemBank Membank, UInt16 WordPtr, UInt16 WordCnt, ref Byte[] pReadData, byte Antenna) - { - byte[] u16byte = new byte[2]; - byte iResult = 0; - try - { - m_GlobalSem.WaitOne(1, false); - log.RfidLog("函数调用: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; - log.RfidLog("Device_Read:" + stringChange.bytesToHexStr(pReadData, pReadData.Length)); - - m_ReadDataLen = 0; - } - else - { - m_ReadDataLen = 0; - log.RfidLog("Device_Read失败,返回长度为0"); - iResult = 0; - } - } - else //超时 - { - log.RfidLog("Device_Read失败,超时未返回"); - iResult = 0; - } - } - else - { - log.RfidLog("发送读取命令失败"); - iResult = 0; - } - } - catch (Exception ex) - { - log.Error("读取数据异常:",ex); - 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) - { - byte[] u16byte = new byte[2]; - byte iResult = 0; - try - { - m_GlobalSem.WaitOne(1, false); - log.RfidLog("函数调用:Device_Write"); - //int datalen = 36+ pWriteData.Length; - int datalen = 48; - MessagePack pMessagePack = new MessagePack(); - pMessagePack.m_pData = new byte[datalen]; - 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] = (byte)(datalen - 6);//先占位 - - pMessagePack.m_pData[3] = 0x03; - - pMessagePack.m_pData[4] = 0x00; - - pMessagePack.m_pData[5] = 0x64; - - pMessagePack.m_pData[6] = 0x00; - - pMessagePack.m_pData[7] = 0x00; - - pMessagePack.m_pData[8] = 0x00; - - pMessagePack.m_pData[9] = 0x00; - - pMessagePack.m_pData[10] = 0x01; - - pMessagePack.m_pData[11] = 0x00; - - pMessagePack.m_pData[12] = 0x00; - - pMessagePack.m_pData[13] = 0x00; - - pMessagePack.m_pData[14] = 0x20; - - pMessagePack.m_pData[15] = 0x60; - - Array.Copy(filterData, 0, pMessagePack.m_pData, 16, filterData.Length); - - pMessagePack.m_pData[28] = 0x03; - - pMessagePack.m_pData[29] = 0x00; - - pMessagePack.m_pData[30] = 0x00; - - pMessagePack.m_pData[31] = 0x00; - - pMessagePack.m_pData[32] = 0x00; - - pMessagePack.m_pData[33] = (byte)WordCnt; - - Array.Copy(pWriteData, 0, pMessagePack.m_pData, 34, pWriteData.Length); - - - byte[] bCRC = new byte[datalen - 4]; - - //Array.Copy(u16byte, 0, pMessagePack.m_pData, 4, 2); - - Array.Copy(pMessagePack.m_pData, 2, bCRC, 0, bCRC.Length); - - pMessagePack.m_pData[datalen - 2] = stringChange.CalculateVerify(bCRC, bCRC.Length); - - pMessagePack.m_pData[datalen - 1] = 0x0D; - - m_WriteSem.WaitOne(1, false); - m_ReadDataLen = 0; - if (m_ICommunicateService.SendMessage(pMessagePack)) //发送报文成功 - { - m_ReadDataSuccessful = true; - if (m_WriteSem.WaitOne(2000, false)) //等待结果,并取结果返回。 - { - //log.RfidLog("Device_Write成功"); - if (writeResult) - { - iResult = 1; - } - } - else //超时 - { - log.RfidLog("Device_Write失败,超时未返回"); - iResult = 0; - } - } - else - { - log.RfidLog("发送写入命令失败"); - iResult = 0; - } - } - catch (Exception ex) - { - log.Error("写入数据异常:",ex); - iResult = 0; - } - finally - { - //m_GlobalSem.Release(); - } - return iResult; - } - - /// - /// 返回读写器获取的条码中最好的一条 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) - { - // log.RfidLog("----调用通用函数:CommandAnalysisBarcode Flag=1 按照最大次数选择条码,如果次数相同 ,按照平均功率最大的选择条码:"); - } - #region 按照最大次数选择条码,如果次数相同 ,按照平均功率最大的选择条码 - #region 求出最大次数 - int iOrderMaxCount = cBarcodeObjList[0].Count; - for (int i = 0; i < cBarcodeObjList.Count; i++) - { - if (iPrintLogFlag == 1) - { - #region 打印所有条码 次数和平均功率 - log.RfidLog("条码:[ " + 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) - { - //LogService.Instance.Debug("按照次数优先,最大次数为:" + iOrderMaxCount.ToString()); - } - #endregion - #region 求出等于最大次数的所有条码对象 - List cBarcodeObjListCount = new List(); - foreach (TagInfo itemMax in cBarcodeObjList) - { - if (itemMax.Count == iOrderMaxCount) - { - cBarcodeObjListCount.Add(itemMax); - //LogService.Instance.Debug("按照次数优先,等于最大次数条码为:" + stringChange.bytesToHexStr(itemMax.EPC, itemMax.EPC.Length)); - } - } - if (iPrintLogFlag == 1) - { - //LogService.Instance.Debug("按照次数优先,等于最大次数条码数量为:" + cBarcodeObjListCount.Count.ToString()); - } - #endregion - #region 求出最优条码 - - if (cBarcodeObjListCount.Count == 1) //如果只有一条 - { - - foreach (TagInfo itemCount in cBarcodeObjListCount) - { - if (itemCount.Count == iOrderMaxCount) - { - if (iPrintLogFlag == 1) - { - //log.RfidLog("---- 取出的最优条码是: " + 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) - { - //LogService.Instance.Debug("按照次数优先,等于最大次数条码中平均功率最大值为:" + iOrderAvgMaxPower.ToString()); - } - - #endregion - - foreach (TagInfo itemAvg in cBarcodeObjListCount) - { - if (itemAvg.RSSI == iOrderAvgMaxPower) - { - if (iPrintLogFlag == 1) - { - //log.RfidLog("---- 取出的最优条码是: " + 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) - { - //log.RfidLog("----调用通用函数:CommandAnalysisBarcode Flag=2 按照最大平均功率取出条码,如果最大平均功率相同,则按照次数最大取值最优条码:"); - } - #region 按照最大平均功率取出条码,如果最大功率相同,则按照次数最大取值最优条码 - #region 求出最大功率 - int iMaxAvgPow = cBarcodeObjList[0].RSSI; - for (int i = 0; i < cBarcodeObjList.Count; i++) - { - if (iPrintLogFlag == 1) - { - #region 打印所有条码 次数和平均功率 - log.RfidLog("条码:[ " + 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) - { - //LogService.Instance.Debug("按照平均功率最大,最大平均功率为:" + iMaxAvgPow.ToString()); - } - #endregion - #region 求出等于最大功率的所有条码对象 - List cBarcodeObjListCount = new List(); - foreach (TagInfo itemMax in cBarcodeObjList) - { - if (itemMax.RSSI == iMaxAvgPow) - { - cBarcodeObjListCount.Add(itemMax); - } - } - if (iPrintLogFlag == 1) - { - //LogService.Instance.Debug("按照平均功率最大,最大平均功率相同的条码数量为:" + cBarcodeObjListCount.Count.ToString()); - } - #endregion - #region 求出最优条码 - //如果只有一条 - if (cBarcodeObjListCount.Count == 1) - { - if (iPrintLogFlag == 1) - { - // log.RfidLog("---- 取出的最优条码是: " + 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) - { - //LogService.Instance.Debug("在最大平均功率相同的条码中,读取次数最多为:" + iReadMaxCount.ToString()); - } - #endregion - foreach (TagInfo itemReadCount in cBarcodeObjListCount) - { - if (itemReadCount.Count == iReadMaxCount) - { - if (iPrintLogFlag == 1) - { - // log.RfidLog("---- 取出的最优条码是: " + 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(); - - /// - /// 四通道读写器标签逻辑处理 - /// - /// - /// - public List Device_DealTagInfoList(byte[] AutoDealReportData) - { - List tagInfoList = new List(); - - byte[] bResultEPC_Data = new byte[14]; - m_AutoReadEPC = null; - m_readEPCDataLen = 0; - //LogService.Instance.Debug("----函数调用:Device_AutoDealContent 开始!"); - 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; - 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]; - tempDataRSSI = stringChange.HexStringToNegative(stringChange.bytesToHexStr(tempRSSIByte, 1)); - - byte[] tempPCByte = new byte[2]; //取出PC - Array.Clear(tempPCByte, 0, 2); - Array.Copy(AutoDealReportData, iFirstPC, tempPCByte, 0, 2); - //tempPCByte = tempPCByte[0]; - - #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; - tag.EPCstring = System.Text.Encoding.ASCII.GetString(tempDataByte); - //tag.EPCstring = stringChange.bytesToHexStr(tempDataByte, tempDataByte.Length); - tag.PC = tempPCByte; - tag.Antana = tempDataANT; - tagInfoList.Add(tag); - int iBarcodeListLen = tagInfoList.Count; //特别注意,必须这样,要不然会多一条数据 - - //int iFirstCountPos = 6; //第一次读取标签次数位置 - //int iFirstRSSIPos = 7; //第一次读取标签强度位置 - //int iFirstAnt = 8; //第一次读取标签天线位置 - //int iFirstLeftBarcketPos = 11;//EPC数据起始位置 - - iFirstCountPos = iFirstCountPos + 21; //次数 - iFirstRSSIPos = iFirstCountPos + 1; //强度 - iFirstAnt = iFirstRSSIPos + 1; //天线 - iFirstPC = iFirstAnt + 1; - iFirstLeftBarcketPos = iFirstLeftBarcketPos + 21; - - log.RfidLog("----函数调用:Device_AutoDealContent 第[" + (iCommonSecondFlag + 1) + "]次数据解析为:" + tag.EPCstring + ",读取标签次数:[" + tempDataCount + "],标签信号强度:[" + tempDataRSSI + "],天线号:[" + tempDataANT + "]"); - iCommonSecondFlag++; - if (iCommonSecondFlag == iBarcodeGroupCount) - { - mutauto.ReleaseMutex(); - log.RfidLog("《《《返回标签数据!"); - return tagInfoList; - } - } - } - catch (Exception ex) - { - log.RfidLog("----函数调用:Device_AutoDealContent 自动处理函数异常:" + ex.ToString()); - mutauto.ReleaseMutex(); - } - return tagInfoList; - } - - public List Device_DealTagInfoList2(byte[] AutoDealReportData) - { - List tagInfoList = new List(); - byte[] bResultEPC_Data = new byte[14]; - m_AutoReadEPC = null; - m_readEPCDataLen = 0; - 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; - for (int j = 0; j < iBarcodeGroupCount; j++) - { - TagInfo tag = new TagInfo(); - byte[] tempPCByte = new byte[2]; //取出PC - Array.Clear(tempPCByte, 0, 2); - Array.Copy(AutoDealReportData, iFirstPC, tempPCByte, 0, 2); - - int pc = Convert.ToInt32(tempPCByte[0].ToString("X")); - 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.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; - - log.RfidLog("----函数调用:Device_DealTagInfoList2 第[" + (iCommonSecondFlag + 1) + "]次数据解析为:" + tag.EPCstring + ",读取标签次数:[" + tempDataCount + "],标签信号强度:[" + tempDataRSSI + "],天线号:[" + tempDataANT + "]"); - iCommonSecondFlag++; - if (iCommonSecondFlag == iBarcodeGroupCount) - { - mutauto.ReleaseMutex(); - log.RfidLog("《《《返回标签数据!"); - return tagInfoList; - } - } - } - catch (Exception ex) - { - log.RfidLog("----函数调用:Device_AutoDealContent 自动处理函数异常:" + ex.ToString()); - mutauto.ReleaseMutex(); - } - return tagInfoList; - } - - private int EPCLengthByPC(int pcValue) - { - int epcLength = 0; - if (pcValue >= 10 && pcValue < 20) - { - epcLength = 4; - } - else if (pcValue >= 20 && pcValue < 30) - { - epcLength = 8; - } - else if (pcValue >= 30 && pcValue < 40) - { - epcLength = 12; - } - else if (pcValue >= 40 && pcValue < 50) - { - epcLength = 16; - } - else if (pcValue >= 50 && pcValue < 60) - { - epcLength = 20; - } - else if (pcValue >= 60 && pcValue < 70) - { - epcLength = 24; - } - else if (pcValue >= 70 && pcValue < 80) - { - epcLength = 28; - } - else if (pcValue >= 80 && pcValue < 90) - { - epcLength = 30; - } - return epcLength; - } - - /// - /// 一体机读写器标签逻辑处理 - /// - /// - /// - public List Device_AutoDealContent(byte[] AutoDealReportData) - { - List tagInfoList = new List(); - - byte[] bResultEPC_Data = new byte[12]; - m_AutoReadEPC = null; - m_readEPCDataLen = 0; - 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()); //标签组数 - int iBarcodeLength = 16; //标签长度 - 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 = System.Text.Encoding.ASCII.GetString(tempDataByte);; - tag.Antana = tempDataANT; - tagInfoList.Add(tag); - int iBarcodeListLen = tagInfoList.Count; //特别注意,必须这样,要不然会多一条数据 - - iFirstCountPos = iFirstCountPos + 21; - iFirstRSSIPos = iFirstCountPos + 1; - iFirstAnt = iFirstRSSIPos + 1; - iFirstLeftBarcketPos = iFirstLeftBarcketPos + 21; - - } - - } - catch (Exception ex) - { - log.RfidLog("----函数调用:Device_AutoDealContent 自动处理函数异常:" + ex.ToString()); - mutauto.ReleaseMutex(); - } - return tagInfoList; - } - #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) //设置功率工程 - { - log.RfidLog("设置天线读功率成功!"); - bResult = true; - } - else - { - log.Error("设置天线读功率失败;"); - bResult = false; - } - } - else - { - Set_OK = MyReader.SetPower(Convert.ToSingle(m_ReadDbm), iDbi); - if (Set_OK) //设置功率工程 - { - log.RfidLog("设置天线写功率成功!"); - bResult = true; - } - else - { - log.Error("设置天线写功率失败;"); - bResult = false; - } - } - } - catch (Exception ex) - { - log.Error("Device_SetRf异常:",ex); - 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); - //log.RfidLog("函数调用: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;//change by yinzf - pMessagePack.m_pData[3] = 0x02; - //pMessagePack.m_pData[4] = 0x00; - //pMessagePack.m_pData[5] = 0x64; - //pMessagePack.m_pData[6] = 0x67; - u16byte = BitConverter.GetBytes(Timedout); //超时时间 - u16byte = stringChange.Swap16Bytes(u16byte); //协议里为大端在前 - //log.RfidLog("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) //有数据,正常 - { - strResult = Encoding.ASCII.GetString(m_OneEpcData); - //Encoding.ASCII.GetBytes(strResult); - //pReadData = new byte[m_OneEpcDataLen]; - //Array.Copy(m_OneEpcData, 0, pReadData, 0, m_OneEpcDataLen); - log.RfidLog("Device_GetOneIdentifyData:" + stringChange.bytesToHexStr(m_OneEpcData, m_OneEpcDataLen)); - - m_OneEpcDataLen = 0; - m_OneEpcAntenna = 254; - } - else - { - strResult = stringChange.bytesToHexStr(m_OneEpcData, m_OneEpcDataLen); - log.RfidLog("Device_GetOneIdentifyData:" + stringChange.bytesToHexStr(m_OneEpcData, m_OneEpcDataLen)); - - m_OneEpcDataLen = 0; - m_OneEpcAntenna = 254; - strResult = ""; - } - } - else //超时 - { - m_OneEpcDataLen = 0; - m_OneEpcAntenna = 254; - log.RfidLog("Device_GetOneIdentifyData超时未返回"); - strResult = ""; - } - } - else - { - m_OneEpcDataLen = 0; - m_OneEpcAntenna = 254; - log.RfidLog("Device_GetOneIdentifyData发送识别单条EPC命令超时"); - strResult = ""; - } - } - catch (Exception ex) - { - log.Error("Device_GetOneIdentifyData识别单条EPC数据异常:",ex); - 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 - { - log.RfidLog("函数调用: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 - { - log.RfidLog("发送开始连续识别命令失败:"); - bResult = false; - } - } - catch (Exception ex) - { - log.Error("发送开始连续识别命令异常", ex); - bResult = false; - } - return bResult; - } - - public bool Device_StopIdentify() - { - bool bResult = false; - try - { - log.RfidLog("函数调用: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 - { - log.RfidLog("发送停止连续识别命令失败:"); - bResult = false; - } - } - catch (Exception ex) - { - log.Error("发送停止连续识别命令异常:",ex); - bResult = false; - } - return bResult; - } - - public ushort Device_GetIdentifyData(ref byte[] pReadData, byte Antenna) - { - return 0; - } - - public List Device_GetTagInfoList(DeviceType devicetype,int waitTime) - { - log.Info("进入Device_GetTagInfoList函数,参数deviceType:" + jsonChange.ModeToJson(devicetype) + ";waitTime:" + waitTime); - byte[] u16byte = new byte[2]; - byte iResult = 0; - byte[] bCRC = new byte[4]; - try - { - //log.SemaphoreLog("信号量m_GlobalSem,WaitOne(1, false)"); - //m_GlobalSem.WaitOne(1, false); - 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; - if (waitTime == 0) - { - waitTime = 1000; - } - u16byte = BitConverter.GetBytes(waitTime); //超时时间 - u16byte = stringChange.Swap16Bytes(u16byte); //协议里为大端在前 - //log.RfidLog("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.WaitOne(1, false); - GetAllRelese(m_OneEpcSem); - - log.SemaphoreLog("信号量m_OneEpcSem,WaitOne(1, false)"); - m_OneEpcDataLen = 0; - DeviceType = devicetype; - if (m_ICommunicateService.SendMessage(pMessagePack)) //发送报文成功 - { - m_ReadDataSuccessful = true; - log.SemaphoreLog("信号量m_OneEpcSem,WaitOne("+ waitTime + ", false)"); - if (m_OneEpcSem.WaitOne(waitTime+2000, false)) //等待结果,并取结果返回。 - { - if (m_OneEpcDataLen > 0) //有数据,正常 - { - m_OneEpcDataLen = 0; - m_OneEpcAntenna = 254; - m_ReadDataSuccessful = false; - log.Info("成功返回数据!"); - return m_TagInfoList; - } - else - { - log.Info("Device_GetTagInfoLista长度或者天线号不正确"); - m_OneEpcDataLen = 0; - m_OneEpcAntenna = 254; - iResult = 0; - m_TagInfoList.Clear(); - m_ReadDataSuccessful = false; - } - } - else //超时 - { - iResult = 0; - m_OneEpcDataLen = 0; - m_OneEpcAntenna = 254; - m_ReadDataSuccessful = false; - m_TagInfoList.Clear(); - log.Info("Device_GetTagInfoList超时"); - log.SemaphoreLog("信号量m_OneEpcSem返回,Device_GetTagInfoList超时"); - - } - } - else - { - m_OneEpcDataLen = 0; - m_OneEpcAntenna = 254; - log.Info("Device_GetTagInfoList发送识别单条EPC命令失败:"); - iResult = 0; - m_TagInfoList.Clear(); - m_ReadDataSuccessful = false; - } - } - catch (Exception ex) - { - log.Info("Device_GetTagInfoList识别单条EPC数据异常:" + ex.Message); - log.Error("Device_GetTagInfoList识别单条EPC数据异常:", ex); - iResult = 0; - m_TagInfoList.Clear(); - m_ReadDataSuccessful = false; - } - finally - { - // m_GlobalSem.Release(); - } - return m_TagInfoList; - } - #endregion - - private void GetAllRelese(Semaphore sph) - { - bool res = sph.WaitOne(1, false); - if (res) - { - log.RfidLog("信号量手动释放"); - GetAllRelese(sph); - } - } - } -} - diff --git a/HighWayIot.TouchSocket/TouchSocketTcpClient.cs b/HighWayIot.TouchSocket/TouchSocketTcpClient.cs new file mode 100644 index 0000000..3fa7d32 --- /dev/null +++ b/HighWayIot.TouchSocket/TouchSocketTcpClient.cs @@ -0,0 +1,87 @@ +using HighWayIot.Log4net; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; +using TouchSocket.Core; +using TouchSocket.Sockets; +using TcpClient = TouchSocket.Sockets.TcpClient; + +namespace HighWayIot.TouchSocket +{ + public class TouchSocketTcpClient + { + private static LogHelper _logHelper = LogHelper.Instance; + + private static readonly Lazy lazy = new Lazy(() => new TouchSocketTcpClient()); + + public static TouchSocketTcpClient Instance => lazy.Value; + + TcpClient client = new TcpClient(); + + public async void TcpClient(string ip, string port) + { + client.Connecting = (client, e) => + { + return EasyTask.CompletedTask; + };//有客户端正在连接 + client.Connected = (client, e) => + { + return EasyTask.CompletedTask; + };//有客户端成功连接 + client.Closing = (client, e) => + { + return EasyTask.CompletedTask; + };//有客户端正在断开连接,只有当主动断开时才有效。 + client.Closed = (client, e) => + { + return EasyTask.CompletedTask; + };//有客户端断开连接 + client.Received = async (client, e) => + { + //从客户端收到信息 + var mes = e.ByteBlock.Span.ToString(Encoding.UTF8); + client.Logger.Info($"已从{client.IP}接收到信息:{mes}"); + }; + + await client.SetupAsync(new TouchSocketConfig() + .SetRemoteIPHost($"{ip}:{port}") + .ConfigureContainer(a => + { + a.AddConsoleLogger();//添加一个日志注入 + })); + + Result result = Result.Default; //不断尝试重连 + do + { + result = await client.TryConnectAsync(); + } + while (!result.IsSuccess); + + return; + } + + /// + /// 信息发送 + /// + /// Bytes + /// + public async Task Send(byte[] message) + { + try + { + await client.SendAsync(message); + return true; + } + catch (Exception e) + { + _logHelper.Error("发送数据发生错误" + e); + return false; + } + } + + } +} diff --git a/HighWayIot.TouchSocket/TouchSocketTcpServer.cs b/HighWayIot.TouchSocket/TouchSocketTcpServer.cs deleted file mode 100644 index 7c0d3b2..0000000 --- a/HighWayIot.TouchSocket/TouchSocketTcpServer.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using TouchSocket.Core; -using TouchSocket.Sockets; - -namespace HighWayIot.TouchSocket -{ - public class TouchSocketTcpServer - { - private static readonly Lazy lazy = new Lazy(() => new TouchSocketTcpServer()); - - public static TouchSocketTcpServer Instance => lazy.Value; - - TcpService service = new TcpService(); - - public async void TcpServer() - { - service.Connecting = (client, e) => - { - return EasyTask.CompletedTask; - };//有客户端正在连接 - service.Connected = (client, e) => - { - return EasyTask.CompletedTask; - };//有客户端成功连接 - service.Closing = (client, e) => - { - return EasyTask.CompletedTask; - };//有客户端正在断开连接,只有当主动断开时才有效。 - service.Closed = (client, e) => - { - return EasyTask.CompletedTask; - };//有客户端断开连接 - service.Received = async (client, e) => - { - //从客户端收到信息 - var mes = e.ByteBlock.Span.ToString(Encoding.UTF8); - client.Logger.Info($"已从{client.Id}接收到信息:{mes}"); - }; - - await service.SetupAsync(new TouchSocketConfig()//载入配置 - .SetListenIPHosts("tcp://127.0.0.1:7789", 7790)//可以同时监听两个地址 - .ConfigureContainer(a =>//容器的配置顺序应该在最前面 - { - a.AddConsoleLogger();//添加一个控制台日志注入(注意:在maui中控制台日志不可用) - }) - .ConfigurePlugins(a => - { - //a.Add();//此处可以添加插件 - })); - - await service.StartAsync();//启动 - } - } -} diff --git a/HighWayIot.Winform/Business/GeneralUtils.cs b/HighWayIot.Winform/Business/GeneralUtils.cs index 30284f2..eec34d6 100644 --- a/HighWayIot.Winform/Business/GeneralUtils.cs +++ b/HighWayIot.Winform/Business/GeneralUtils.cs @@ -45,27 +45,6 @@ namespace HighWayIot.Winform.Business } } - /// - /// 获取枚举类的键值对 - /// - /// - /// - public static IEnumerable> GetEnumKeyValuePairs() where T : Enum - { - var enumType = typeof(T); - var fields = enumType.GetFields(); - - foreach (var fi in fields) - { - if (fi.FieldType != enumType || !fi.IsLiteral) - continue; - - var name = fi.Name; - var value = (int)enumType.GetField(name).GetValue(null); - yield return new KeyValuePair(name, value); - } - } - /// /// 返回对应的枚举类Type类型 /// diff --git a/HighWayIot.Winform/MainForm/BaseForm.Designer.cs b/HighWayIot.Winform/MainForm/BaseForm.Designer.cs index 8e6f54a..7bccf94 100644 --- a/HighWayIot.Winform/MainForm/BaseForm.Designer.cs +++ b/HighWayIot.Winform/MainForm/BaseForm.Designer.cs @@ -53,6 +53,7 @@ namespace HighWayIot.Winform.MainForm this.RecipeConfigStripItem = new System.Windows.Forms.ToolStripMenuItem(); this.EquipMaterialBindingStripItem = new System.Windows.Forms.ToolStripMenuItem(); this.TestMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.DeviceDataManageToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.UserControlTabs = new System.Windows.Forms.TabControl(); this.ClosePageButton = new System.Windows.Forms.Button(); this.statusStrip1 = new System.Windows.Forms.StatusStrip(); @@ -64,7 +65,7 @@ namespace HighWayIot.Winform.MainForm this.StripLabel2 = new System.Windows.Forms.ToolStripStatusLabel(); this.TimeStripLabel = new System.Windows.Forms.ToolStripStatusLabel(); this.TimeDisplayTimer = new System.Windows.Forms.Timer(this.components); - this.DeviceDataManageToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.DataRefreshTimer = new System.Windows.Forms.Timer(this.components); this.MainMenu.SuspendLayout(); this.statusStrip1.SuspendLayout(); this.SuspendLayout(); @@ -224,6 +225,13 @@ namespace HighWayIot.Winform.MainForm this.TestMenuItem.Text = "PLC测试页面"; this.TestMenuItem.Click += new System.EventHandler(this.StripMenuItemClick); // + // DeviceDataManageToolStripMenuItem + // + this.DeviceDataManageToolStripMenuItem.Name = "DeviceDataManageToolStripMenuItem"; + this.DeviceDataManageToolStripMenuItem.Size = new System.Drawing.Size(92, 22); + this.DeviceDataManageToolStripMenuItem.Text = "设备数据管理"; + this.DeviceDataManageToolStripMenuItem.Click += new System.EventHandler(this.StripMenuItemClick); + // // UserControlTabs // this.UserControlTabs.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) @@ -316,12 +324,9 @@ namespace HighWayIot.Winform.MainForm this.TimeDisplayTimer.Interval = 1000; this.TimeDisplayTimer.Tick += new System.EventHandler(this.TimeDisplayTimer_Tick); // - // DeviceDataManageToolStripMenuItem + // DataRefreshTimer // - this.DeviceDataManageToolStripMenuItem.Name = "DeviceDataManageToolStripMenuItem"; - this.DeviceDataManageToolStripMenuItem.Size = new System.Drawing.Size(92, 22); - this.DeviceDataManageToolStripMenuItem.Text = "设备数据管理"; - this.DeviceDataManageToolStripMenuItem.Click += new System.EventHandler(this.StripMenuItemClick); + this.DataRefreshTimer.Tick += new System.EventHandler(this.DataRefreshTimer_Tick); // // BaseForm // @@ -379,5 +384,6 @@ namespace HighWayIot.Winform.MainForm private ToolStripMenuItem MaterialConfigStripItem; private ToolStripMenuItem MaterialTypeConfigStripItem; private ToolStripMenuItem DeviceDataManageToolStripMenuItem; + private Timer DataRefreshTimer; } } \ No newline at end of file diff --git a/HighWayIot.Winform/MainForm/BaseForm.cs b/HighWayIot.Winform/MainForm/BaseForm.cs index a71bd7f..1d7adbf 100644 --- a/HighWayIot.Winform/MainForm/BaseForm.cs +++ b/HighWayIot.Winform/MainForm/BaseForm.cs @@ -270,5 +270,15 @@ namespace HighWayIot.Winform.MainForm { } + + private void DataRefreshTimer_Tick(object sender, EventArgs e) + { + //一读 + + //判断结果 + + //哪个工位读到了哪个工位写 + + } } } diff --git a/HighWayIot.Winform/MainForm/BaseForm.resx b/HighWayIot.Winform/MainForm/BaseForm.resx index 6ec8fc0..3ab3a51 100644 --- a/HighWayIot.Winform/MainForm/BaseForm.resx +++ b/HighWayIot.Winform/MainForm/BaseForm.resx @@ -126,6 +126,9 @@ 257, 17 + + 415, 17 +