public int GetHashCode(AddressUnit <TKey> obj) { return(obj.GetHashCode()); }
/// <summary> /// 写入数据 /// </summary> /// <param name="setDataType">写入类型</param> /// <param name="values">需要写入的数据字典,当写入类型为Address时,键为需要写入的地址,当写入类型为CommunicationTag时,键为需要写入的单元的描述</param> /// <returns>是否写入成功</returns> public async Task <bool> SetDatasAsync(MachineSetDataType setDataType, Dictionary <string, double> values) { try { //检测并连接设备 if (!BaseUtility.IsConnected) { await BaseUtility.ConnectAsync(); } //如果设备无法连接,终止 if (!BaseUtility.IsConnected) { return(false); } var addresses = new List <AddressUnit>(); //遍历每个要设置的值 foreach (var value in values) { //根据设置类型找到对应的地址描述 AddressUnit address = null; switch (setDataType) { case MachineSetDataType.Address: { address = GetAddresses.SingleOrDefault( p => AddressFormater.FormatAddress(p.Area, p.Address, p.SubAddress) == value.Key || (p.DataType != typeof(bool) && AddressFormater.FormatAddress(p.Area, p.Address) == value.Key)); break; } case MachineSetDataType.CommunicationTag: { address = GetAddresses.SingleOrDefault(p => p.CommunicationTag == value.Key); break; } case MachineSetDataType.Name: { address = GetAddresses.SingleOrDefault(p => p.Name == value.Key); break; } } //地址为空报错 if (address == null) { Console.WriteLine($"Machine {ConnectionToken} Address {value.Key} doesn't exist."); continue; } //不能写报错 if (!address.CanWrite) { Console.WriteLine($"Machine {ConnectionToken} Address {value.Key} cannot write."); continue; } addresses.Add(address); } //将地址编码成与实际设备通讯的地址 var communcationUnits = AddressCombinerSet.Combine(addresses); //遍历每条通讯的连续地址 foreach (var communicateAddress in communcationUnits) { //编码开始地址 var addressStart = AddressFormater.FormatAddress(communicateAddress.Area, communicateAddress.Address); var datasReturn = await BaseUtility.GetDatasAsync( AddressFormater.FormatAddress(communicateAddress.Area, communicateAddress.Address, 0), (int) Math.Ceiling(communicateAddress.GetCount * BigEndianValueHelper.Instance.ByteLength[ communicateAddress.DataType.FullName])); var valueHelper = ValueHelper.GetInstance(BaseUtility.Endian); //如果设备本身能获取到数据但是没有数据 var datas = datasReturn; //如果没有数据,终止 if (datas == null || datas.Length < (int) Math.Ceiling(communicateAddress.GetCount * BigEndianValueHelper.Instance.ByteLength[ communicateAddress.DataType.FullName])) { return(false); } foreach (var addressUnit in communicateAddress.OriginalAddresses) { //字节坐标地址 var byteCount = AddressHelper.MapProtocalGetCountToAbstractByteCount( addressUnit.Address - communicateAddress.Address + addressUnit.SubAddress * 0.125 / AddressTranslator.GetAreaByteLength(communicateAddress.Area), AddressTranslator.GetAreaByteLength(communicateAddress.Area), 0); //字节坐标主地址 var mainByteCount = (int)byteCount; //字节坐标自地址 var localByteCount = (int)((byteCount - (int)byteCount) * 8); //协议坐标地址 var localPos = byteCount / AddressTranslator.GetAreaByteLength(communicateAddress.Area); //协议坐标子地址 var subPos = (int) ((localPos - (int)localPos) / (0.125 / AddressTranslator.GetAreaByteLength(communicateAddress.Area))); //协议主地址字符串 var address = AddressFormater.FormatAddress(communicateAddress.Area, communicateAddress.Address + (int)localPos, subPos); //协议完整地址字符串 var address2 = subPos != 0 ? null : AddressFormater.FormatAddress(communicateAddress.Area, communicateAddress.Address + (int)localPos); //获取写入类型 var dataType = addressUnit.DataType; switch (setDataType) { case MachineSetDataType.Address: { //获取要写入的值 var value = values.SingleOrDefault( p => p.Key == address || (address2 != null && p.Key == address2)); //将要写入的值加入队列 var data = Convert.ChangeType(value.Value / addressUnit.Zoom, dataType); if (!valueHelper.SetValue(datas, mainByteCount, localByteCount, data)) { return(false); } break; } case MachineSetDataType.CommunicationTag: { var value = values.SingleOrDefault(p => p.Key == addressUnit.CommunicationTag); var data = Convert.ChangeType(value.Value / addressUnit.Zoom, dataType); if (!valueHelper.SetValue(datas, mainByteCount, localByteCount, data)) { return(false); } break; } case MachineSetDataType.Name: { var value = values.SingleOrDefault(p => p.Key == addressUnit.Name); var data = Convert.ChangeType(value.Value / addressUnit.Zoom, dataType); if (!valueHelper.SetValue(datas, mainByteCount, localByteCount, data)) { return(false); } break; } } } //写入数据 await BaseUtility.SetDatasAsync(addressStart, valueHelper.ByteArrayToObjectArray(datas, new KeyValuePair <Type, int>(communicateAddress.DataType, communicateAddress.GetCount))); } //如果不保持连接,断开连接 if (!KeepConnect) { BaseUtility.Disconnect(); } } catch (Exception e) { Console.WriteLine(ConnectionToken + " " + e.Message); return(false); } return(true); }
public bool Equals(AddressUnit <TKey> x, AddressUnit <TKey> y) { return((x.Area.ToUpper() == y.Area.ToUpper() && x.Address == y.Address) || x.Id.Equals(y.Id)); }