/// <summary> /// 将指定的OperateResult类型转化 /// </summary> /// <param name="result">原始的类型</param> /// <param name="byteTransform">数据转换方法</param> /// <returns>转化后的类型</returns> public static OperateResult <int> GetInt32ResultFromBytes(OperateResult <byte[]> result, IByteTransform byteTransform) { return(GetResultFromBytes(result, m => byteTransform.TransInt32(m, 0))); }
/// <summary> /// 将指定的OperateResult类型转化 /// </summary> /// <param name="result">原始的类型</param> /// <param name="byteTransform">数据转换方法</param> /// <returns>转化后的类型</returns> public static OperateResult <byte> GetByteResultFromBytes(OperateResult <byte[]> result, IByteTransform byteTransform) { return(GetResultFromBytes(result, m => byteTransform.TransByte(m, 0))); }
/// <summary> /// 将指定的OperateResult类型转化 /// </summary> /// <param name="result">原始的类型</param> /// <param name="byteTransform">数据转换方法</param> /// <returns>转化后的类型</returns> public static OperateResult <ushort> GetUInt16ResultFromBytes(OperateResult <byte[]> result, IByteTransform byteTransform) { return(GetResultFromBytes(result, m => byteTransform.TransUInt16(m, 0))); }
/// <summary> /// 将指定的OperateResult类型转化 /// </summary> /// <param name="result">原始的类型</param> /// <param name="byteTransform">数据转换方法</param> /// <returns>转化后的类型</returns> public static OperateResult <string> GetStringResultFromBytes(OperateResult <byte[]> result, IByteTransform byteTransform) { return(GetResultFromBytes(result, byteTransform.TransString)); }
/// <summary> /// 将指定的OperateResult类型转化 /// </summary> /// <param name="result">原始的类型</param> /// <param name="byteTransform">数据转换方法</param> /// <returns>转化后的类型</returns> public static OperateResult <bool> GetBoolResultFromBytes(OperateResult <byte[]> result, IByteTransform byteTransform) { return(GetResultFromBytes(result, byteTransform.TransBool)); }
/// <summary> /// 将指定的OperateResult类型转化 /// </summary> /// <param name="result">原始的类型</param> /// <param name="byteTransform">数据转换方法</param> /// <returns>转化后的类型</returns> public static OperateResult <ulong> GetUInt64ResultFromBytes(OperateResult <byte[]> result, IByteTransform byteTransform) { return(GetResultFromBytes(result, m => byteTransform.TransUInt64(m, 0))); }
/// <summary> /// 将指定的OperateResult类型转化 /// </summary> /// <param name="result">原始的类型</param> /// <param name="byteTransform">数据转换方法</param> /// <returns>转化后的类型</returns> public static OperateResult <float> GetSingleResultFromBytes(OperateResult <byte[]> result, IByteTransform byteTransform) { return(GetResultFromBytes(result, m => byteTransform.TransSingle(m, 0))); }
public DataStorage([Inject] IReadWriteNet readwriter, [Inject] IByteTransform transform, [Inject] IEventAggregator events) { this._events = events; this._transform = transform; _readwriter = readwriter; Load(); // Task.Run(() => //{ // while (true) // { // lock (_enable) // { // // 对数据进行分组,每组内间隔不超过PDU // int arrIndex = 0; // var sortedVars = DataValues.OrderBy(x => x.StartIndex).ToList(); // var arrList = new List<List<VAR>> { new List<VAR>() { sortedVars[0] } }; // for (int i = 1; i < sortedVars.Count; i++) // { // var a = sortedVars[i].StartIndex + sortedVars[i].GetLength(); // var b = sortedVars[i - 1].StartIndex; // if (a - b < PDU) // { // arrList[arrIndex].Add(sortedVars[i]); // } // else // { // arrList.Add(new List<VAR>()); // arrIndex++; // } // } // foreach (var list in arrList) // { // // 获取组内需要请求的字数 // int nShorts = 0; // foreach (var s in list) // { // nShorts += s.GetLength(); // } // if (nShorts > 10) // { // var s = list.First().StartIndex; // var l = list.Last().GetLength() + list.Last().StartIndex; // var r = _readwriter.Read($"x:3;{s}", (ushort)(l - s)); // if (r.IsSuccess) // { // var buffer = r.Content; // foreach (var v in list) // { // switch (v.Type) // { // case TYPE.BOOL: // var cs = transform.TransUInt16(buffer, (v.StartIndex - s) * 2); // v.Value = (cs & (1 << v.Tag)) > 0; // break; // case TYPE.FLOAT: // v.Value = transform.TransSingle(buffer, (v.StartIndex - s) * 2); // break; // case TYPE.INT: // v.Value = transform.TransInt32(buffer, (v.StartIndex - s) * 2); // break; // case TYPE.SHORT: // v.Value = transform.TransInt16(buffer, (v.StartIndex - s) * 2); // break; // case TYPE.STRING: // v.Value = transform.TransString(buffer, (v.StartIndex - s) * 2, v.Tag, Encoding.UTF8); // break; // } // v.Time = DateTime.Now; // } // } // else // { // _events.Publish(new MsgItem() // { // Level = "E", // Time = DateTime.Now, // Value = "Read from data server fail" // }); // } // } // else // { // foreach (var v in list) // { // switch (v.Type) // { // case TYPE.BOOL: // var r = _readwriter.ReadUInt16($"x=3;{v.StartIndex}"); // v.Value = (r.Content & (1 << v.Tag)) > 0; // break; // case TYPE.FLOAT: // var f = _readwriter.ReadFloat($"x=3;{v.StartIndex}"); // v.Value = f.Content; // break; // case TYPE.INT: // var i = _readwriter.ReadInt32($"x=3;{v.StartIndex}"); // v.Value = i.Content; // break; // case TYPE.STRING: // var s = _readwriter.ReadString($"x=3;{v.StartIndex}", (ushort)v.Tag); // v.Value = s.Content; // break; // case TYPE.SHORT: // var us = _readwriter.ReadInt16($"x=3;{v.StartIndex}"); // v.Value = us.Content; // break; // } // v.Time = DateTime.Now; // } // } // } // Refresh(); // } // System.Threading.Thread.Sleep(100); // } //}); }
public dynamic GetValue(byte[] data, IByteTransform byteTransform) { if (RegularCode == RegularNodeTypeItem.Int16.Code) { if (TypeLength == 1) { return(byteTransform.TransInt16(data, Index)); } else { return(byteTransform.TransInt16(data, Index, TypeLength)); } } else if (RegularCode == RegularNodeTypeItem.UInt16.Code) { if (TypeLength == 1) { return(byteTransform.TransUInt16(data, Index)); } else { return(byteTransform.TransUInt16(data, Index, TypeLength)); } } else if (RegularCode == RegularNodeTypeItem.Int32.Code) { if (TypeLength == 1) { return(byteTransform.TransInt32(data, Index)); } else { return(byteTransform.TransInt32(data, Index, TypeLength)); } } else if (RegularCode == RegularNodeTypeItem.UInt32.Code) { if (TypeLength == 1) { return(byteTransform.TransUInt32(data, Index)); } else { return(byteTransform.TransUInt32(data, Index, TypeLength)); } } else if (RegularCode == RegularNodeTypeItem.Int64.Code) { if (TypeLength == 1) { return(byteTransform.TransInt64(data, Index)); } else { return(byteTransform.TransInt64(data, Index, TypeLength)); } } else if (RegularCode == RegularNodeTypeItem.UInt64.Code) { if (TypeLength == 1) { return(byteTransform.TransUInt64(data, Index)); } else { return(byteTransform.TransUInt64(data, Index, TypeLength)); } } else if (RegularCode == RegularNodeTypeItem.Float.Code) { if (TypeLength == 1) { return(byteTransform.TransSingle(data, Index)); } else { return(byteTransform.TransSingle(data, Index, TypeLength)); } } else if (RegularCode == RegularNodeTypeItem.Double.Code) { if (TypeLength == 1) { return(byteTransform.TransDouble(data, Index)); } else { return(byteTransform.TransDouble(data, Index, TypeLength)); } } else if (RegularCode == RegularNodeTypeItem.StringAscii.Code) { return(Encoding.ASCII.GetString(data, Index, TypeLength)); } else { throw new Exception("Not Supported Data Type"); } }
/// <summary> /// 获取当前的数据信息实际值 /// </summary> /// <param name="data"></param> /// <param name="byteTransform"></param> /// <returns></returns> public dynamic GetValue(byte[] data, IByteTransform byteTransform) { if (RegularCode == RegularNodeTypeItem.Bool.Code) { if (TypeLength == 1) { var tmp = SoftBasic.ByteToBoolArray(data, data.Length * 8); return(tmp[Index]); } else { var tmp = SoftBasic.ByteToBoolArray(data, data.Length * 8); var array = new bool[TypeLength]; for (var i = 0; i < array.Length; i++) { array[i] = tmp[Index + i]; } return(array); } } if (RegularCode == RegularNodeTypeItem.Int16.Code) { if (TypeLength == 1) { return(byteTransform.TransInt16(data, Index)); } return(byteTransform.TransInt16(data, Index, TypeLength)); } if (RegularCode == RegularNodeTypeItem.UInt16.Code) { if (TypeLength == 1) { return(byteTransform.TransUInt16(data, Index)); } return(byteTransform.TransUInt16(data, Index, TypeLength)); } if (RegularCode == RegularNodeTypeItem.Int32.Code) { if (TypeLength == 1) { return(byteTransform.TransInt32(data, Index)); } return(byteTransform.TransInt32(data, Index, TypeLength)); } if (RegularCode == RegularNodeTypeItem.UInt32.Code) { if (TypeLength == 1) { return(byteTransform.TransUInt32(data, Index)); } return(byteTransform.TransUInt32(data, Index, TypeLength)); } if (RegularCode == RegularNodeTypeItem.Int64.Code) { if (TypeLength == 1) { return(byteTransform.TransInt64(data, Index)); } return(byteTransform.TransInt64(data, Index, TypeLength)); } if (RegularCode == RegularNodeTypeItem.UInt64.Code) { if (TypeLength == 1) { return(byteTransform.TransUInt64(data, Index)); } return(byteTransform.TransUInt64(data, Index, TypeLength)); } if (RegularCode == RegularNodeTypeItem.Float.Code) { if (TypeLength == 1) { return(byteTransform.TransSingle(data, Index)); } return(byteTransform.TransSingle(data, Index, TypeLength)); } if (RegularCode == RegularNodeTypeItem.Double.Code) { if (TypeLength == 1) { return(byteTransform.TransDouble(data, Index)); } return(byteTransform.TransDouble(data, Index, TypeLength)); } if (RegularCode == RegularNodeTypeItem.StringAscii.Code) { return(Encoding.ASCII.GetString(data, Index, TypeLength)); } throw new Exception("Not Supported Data Type"); }
/// <summary> /// 将指定的OperateResult类型转化 /// </summary> /// <param name="result">原始的类型</param> /// <param name="byteTransform">数据转换方法</param> /// <returns>转化后的类型</returns> public static OperateResult <string> GetStringResultFromBytes(OperateResult <byte[]> result, IByteTransform byteTransform) { return(GetResultFromBytes(result, m => byteTransform.TransString(m, 0, m.Length, Encoding.ASCII))); }
public void WriteDeviceData(string deviceNode, byte[] data, DeviceRequest request, IByteTransform byteTransform) { List <RegularNode> regularNodes = Util.SharpRegulars.GetRegularNodes(request.PraseRegularCode); if (regularNodes != null) { lock (Lock) { for (int i = 0; i < regularNodes.Count; i++) { dict_BaseDataVariableState[deviceNode + "/" + regularNodes[i].Name].Value = regularNodes[i].GetValue(data, byteTransform); dict_BaseDataVariableState[deviceNode + "/" + regularNodes[i].Name].ClearChangeMasks(SystemContext, false); } } } else { this.logNet?.WriteWarn(ToString( ), $"Not find regular : { request.PraseRegularCode}"); } }