/// <summary> /// Convert an array of bytes to a value whose type is given by dataType /// </summary> protected static object BytesToObj(byte[] bytes, int index, int dataType) { switch (dataType) { case DataTypes.Integer: return(BitConverter.ToInt32(bytes, index)); case DataTypes.Double: return(BitConverter.ToDouble(bytes, index)); case DataTypes.Boolean: return(bytes[index] > 0); case DataTypes.DateTime: return(ScadaUtils.DecodeDateTime(BitConverter.ToDouble(bytes, index))); case DataTypes.String: int strDataSize = BitConverter.ToUInt16(bytes, index); index += 2; if (index + strDataSize > bytes.Length) { strDataSize = bytes.Length - index; } // for ASCII-encoded data, the Encoding.UTF8.GetString () method will also work correctly return(strDataSize > 0 ? Encoding.UTF8.GetString(bytes, index, strDataSize) : ""); default: return(DBNull.Value); } }
/// <summary> /// Преобразовать массив байт в значение, тип которого задан dataType /// </summary> protected static object BytesToObj(byte[] bytes, int index, int dataType) { switch (dataType) { case DataTypes.Integer: return(BitConverter.ToInt32(bytes, index)); case DataTypes.Double: return(BitConverter.ToDouble(bytes, index)); case DataTypes.Boolean: return(bytes[index] > 0); case DataTypes.DateTime: return(ScadaUtils.DecodeDateTime(BitConverter.ToDouble(bytes, index))); case DataTypes.String: int strDataSize = BitConverter.ToUInt16(bytes, index); index += 2; if (index + strDataSize > bytes.Length) { strDataSize = bytes.Length - index; } // для данных в кодировке ASCII метод Encoding.UTF8.GetString() тоже будет корректно работать return(strDataSize > 0 ? Encoding.UTF8.GetString(bytes, index, strDataSize) : ""); default: return(DBNull.Value); } }
private List <ActiveCnlList> ActiveCnl = new List <ActiveCnlList>(); // Создание списка Активных сигналов, где ActiveCnl.Cnl - номер сигнала, ActiveCnl.Name - Имя сигнала, // ActiveCnl.Fotmat - Тип активной переменной, ActiveCnl.IdxTag индекс сигнала в KPTags, ActiveCnl.IdxValue - Индекс группы, // в которую входит сигнал, ActiveCnl.MenuName - Имя меню, которому принадлежит сигнал /// <summary> /// Преобразовать данные тега КП в строку /// </summary> protected override string ConvertTagDataToStr(int signal, SrezTableLight.CnlData tagData) // Необходимо продумать как передать сюда список типов переменных - текст, время, цифровое и т.д. { string strval = ""; bool readstr = myTagId.TryGetValue(signal, out strval); // Чтение типа переменной, привязанной к сигналу if (tagData.Stat > 0) { if (strval == "DateTime") // Проверка сигнала на тип данных Время для отображения в текстовом виде в таблице Коммуникатора { return(ScadaUtils.DecodeDateTime(tagData.Val).ToString()); } } return(base.ConvertTagDataToStr(signal, tagData)); }
/// <summary> /// Converts the tag data to string. /// </summary> protected override string ConvertTagDataToStr(int signal, SrezTableLight.CnlData tagData) { if (tagData.Stat > 0) { switch (tagTypes[signal - 1]) { case TagType.String: return(ScadaUtils.DecodeAscii(tagData.Val)); case TagType.DateTime: return(ScadaUtils.DecodeDateTime(tagData.Val).ToLocalizedString()); } } return(base.ConvertTagDataToStr(signal, tagData)); }
/// <summary> /// Converts the tag data to string. /// </summary> protected override string ConvertTagDataToStr(KPTag kpTag, SrezTableLight.CnlData tagData) { if (tagData.Stat > 0 && kpTag.Aux is TagType tagType) { switch (tagType) { case TagType.String: return(ScadaUtils.DecodeAscii(tagData.Val)); case TagType.DateTime: return(ScadaUtils.DecodeDateTime(tagData.Val).ToLocalizedString()); } } return(base.ConvertTagDataToStr(kpTag, tagData)); }
/// <summary> /// Format input channel value /// </summary> public void FormatCnlVal(double val, int stat, InCnlProps cnlProps, string decSep, string grSep, out string text, out string textWithUnit, out bool textIsNumber, bool throwOnError = false) { bool cnlPropsIsNull = cnlProps == null; try { if (stat <= 0) { text = textWithUnit = EmptyVal; textIsNumber = false; } else { text = textWithUnit = NoVal; textIsNumber = false; int formatID = cnlPropsIsNull ? 0 : cnlProps.FormatID; int unitArrLen = cnlPropsIsNull || cnlProps.UnitArr == null ? 0 : cnlProps.UnitArr.Length; if (cnlPropsIsNull || cnlProps.ShowNumber) { // getting dimension string unit = unitArrLen > 0 ? " " + cnlProps.UnitArr[0] : ""; // number format definition NumberFormatInfo nfi; bool sepDefined = !(decSep == null && grSep == null); if (cnlPropsIsNull || sepDefined) { nfi = sepDefined ? CreateFormatInfo(DefDecDig, decSep, grSep) : defNfi; } else if (cnlProps.FormatInfo == null) { nfi = cnlProps.FormatInfo = CreateFormatInfo(cnlProps.DecDigits, decSep, grSep); } else { nfi = cnlProps.FormatInfo; } // value formatting text = val.ToString("N", nfi); textWithUnit = text + unit; textIsNumber = true; } else if (formatID == BaseValues.Formats.EnumText) { if (unitArrLen > 0) { var unitInd = (int)val; if (unitInd < 0) { unitInd = 0; } else if (unitInd >= unitArrLen) { unitInd = unitArrLen - 1; } text = textWithUnit = cnlProps.UnitArr[unitInd]; } } else if (formatID == BaseValues.Formats.AsciiText) { text = textWithUnit = ScadaUtils.DecodeAscii(val); } else if (formatID == BaseValues.Formats.UnicodeText) { text = textWithUnit = ScadaUtils.DecodeUnicode(val); } else if (formatID == BaseValues.Formats.DateTime) { text = textWithUnit = ScadaUtils.DecodeDateTime(val).ToLocalizedString(); } else if (formatID == BaseValues.Formats.Date) { text = textWithUnit = ScadaUtils.DecodeDateTime(val).ToLocalizedDateString(); } else if (formatID == BaseValues.Formats.Time) { text = textWithUnit = ScadaUtils.DecodeDateTime(val).ToLocalizedTimeString(); } } } catch (Exception ex) { if (throwOnError) { string cnlNumStr = cnlPropsIsNull ? "?" : cnlProps.CnlNum.ToString(); throw new ScadaException($"Error formatting value of input channel {cnlNumStr}", ex); } text = textWithUnit = FrmtErrVal; textIsNumber = false; } }
/// <summary> /// Форматировать значение входного канала /// </summary> public void FormatCnlVal(double val, int stat, InCnlProps cnlProps, string decSep, string grSep, out string text, out string textWithUnit, out bool textIsNumber, bool throwOnError = false) { bool cnlPropsIsNull = cnlProps == null; try { if (stat <= 0) { text = textWithUnit = EmptyVal; textIsNumber = false; } else { text = textWithUnit = NoVal; textIsNumber = false; int formatID = cnlPropsIsNull ? 0 : cnlProps.FormatID; int unitArrLen = cnlPropsIsNull || cnlProps.UnitArr == null ? 0 : cnlProps.UnitArr.Length; if (cnlPropsIsNull || cnlProps.ShowNumber) { string unit = unitArrLen > 0 ? " " + cnlProps.UnitArr[0] : ""; nfi.NumberDecimalDigits = cnlPropsIsNull ? DefDecDig : cnlProps.DecDigits; nfi.NumberDecimalSeparator = decSep == null ? defDecSep : decSep; nfi.NumberGroupSeparator = grSep == null ? defGrSep : grSep; text = val.ToString("N", nfi); textWithUnit = text + unit; textIsNumber = true; } else if (formatID == BaseValues.Formats.EnumText) { if (unitArrLen > 0) { int unitInd = (int)val; if (unitInd < 0) { unitInd = 0; } else if (unitInd >= unitArrLen) { unitInd = unitArrLen - 1; } text = textWithUnit = cnlProps.UnitArr[unitInd]; } } else if (formatID == BaseValues.Formats.AsciiText) { text = textWithUnit = ScadaUtils.DecodeAscii(val); } else if (formatID == BaseValues.Formats.UnicodeText) { text = textWithUnit = ScadaUtils.DecodeUnicode(val); } else if (formatID == BaseValues.Formats.DateTime) { text = textWithUnit = ScadaUtils.DecodeDateTime(val).ToLocalizedString(); } else if (formatID == BaseValues.Formats.Date) { text = textWithUnit = ScadaUtils.DecodeDateTime(val).ToLocalizedDateString(); } else if (formatID == BaseValues.Formats.Time) { text = textWithUnit = ScadaUtils.DecodeDateTime(val).ToLocalizedTimeString(); } } } catch (Exception ex) { if (throwOnError) { string cnlNumStr = cnlPropsIsNull ? "?" : cnlProps.CnlNum.ToString(); throw new ScadaException(string.Format(Localization.UseRussian ? "Ошибка при форматировании значения входного канала {0}" : "Error formatting value of input channel {0}", cnlNumStr), ex); } else { text = textWithUnit = FrmtErrVal; textIsNumber = false; } } }
/// <summary> /// Форматировать значение входного канала /// </summary> public void FormatCnlVal(double val, int stat, InCnlProps cnlProps, string decSep, string grSep, out string text, out string textWithUnit, out bool textIsNumber, bool throwOnError = false) { bool cnlPropsIsNull = cnlProps == null; try { if (stat <= 0) { text = textWithUnit = EmptyVal; textIsNumber = false; } else { text = textWithUnit = NoVal; textIsNumber = false; int formatID = cnlPropsIsNull ? 0 : cnlProps.FormatID; int unitArrLen = cnlPropsIsNull || cnlProps.UnitArr == null ? 0 : cnlProps.UnitArr.Length; if (cnlPropsIsNull || cnlProps.ShowNumber) { // получение размерности string unit = unitArrLen > 0 ? " " + cnlProps.UnitArr[0] : ""; // определение формата числа NumberFormatInfo nfi; bool sepDefined = !(decSep == null && grSep == null); if (cnlPropsIsNull) { nfi = sepDefined ? CreateFormatInfo(DefDecDig, decSep, grSep) : defNfi; } else if (sepDefined) { nfi = CreateFormatInfo(cnlProps.DecDigits, decSep, grSep); } else if (cnlProps.FormatInfo == null) { nfi = cnlProps.FormatInfo = CreateFormatInfo(cnlProps.DecDigits, decSep, grSep); } else { nfi = cnlProps.FormatInfo; } // форматирование значения text = val.ToString("N", nfi); textWithUnit = text + unit; textIsNumber = true; } else if (formatID == BaseValues.Formats.EnumText) { if (unitArrLen > 0) { int unitInd = (int)val; if (unitInd < 0) { unitInd = 0; } else if (unitInd >= unitArrLen) { unitInd = unitArrLen - 1; } text = textWithUnit = cnlProps.UnitArr[unitInd]; } } else if (formatID == BaseValues.Formats.AsciiText) { text = textWithUnit = ScadaUtils.DecodeAscii(val); } else if (formatID == BaseValues.Formats.UnicodeText) { text = textWithUnit = ScadaUtils.DecodeUnicode(val); } else if (formatID == BaseValues.Formats.DateTime) { text = textWithUnit = ScadaUtils.DecodeDateTime(val).ToLocalizedString(); } else if (formatID == BaseValues.Formats.Date) { text = textWithUnit = ScadaUtils.DecodeDateTime(val).ToLocalizedDateString(); } else if (formatID == BaseValues.Formats.Time) { text = textWithUnit = ScadaUtils.DecodeDateTime(val).ToLocalizedTimeString(); } } } catch (Exception ex) { if (throwOnError) { string cnlNumStr = cnlPropsIsNull ? "?" : cnlProps.CnlNum.ToString(); throw new ScadaException(string.Format(Localization.UseRussian ? "Ошибка при форматировании значения входного канала {0}" : "Error formatting value of input channel {0}", cnlNumStr), ex); } else { text = textWithUnit = FrmtErrVal; textIsNumber = false; } } }