public static DoubleWordValue ParseDoubleWordValue(string valueString, Device contextDevice) { Match match1 = DoubleWordRegex.Match(valueString); if (match1.Success) { uint index = uint.Parse(match1.Groups[2].Value); WordValue offset = null; if (match1.Groups[4].Success) { if (match1.Groups[4].Value.ToUpper() == "V") { uint vindex = uint.Parse(match1.Groups[5].Value); if (contextDevice.VRange.AssertValue(vindex)) { offset = new VWordValue(vindex); } else { throw new ValueParseException(string.Format("Current PLC Device do not support V{0} address", vindex)); } } else { if (match1.Groups[4].Value.ToUpper() == "Z") { uint zindex = uint.Parse(match1.Groups[5].Value); if (contextDevice.ZRange.AssertValue(zindex)) { offset = new ZWordValue(zindex); } else { throw new ValueParseException(string.Format("Current PLC Device do not support Z{0} address", zindex)); } } } } if (match1.Groups[1].Value.ToUpper() == "D") { if (contextDevice.DRange.AssertValue(index)) { return(new DDoubleWordValue(index, offset)); } else { throw new ValueParseException(string.Format("Current PLC Device do not support D{0} address", index)); } } if (match1.Groups[1].Value.ToUpper() == "CV") { if (contextDevice.CV32Range.AssertValue(index)) { return(new CV32DoubleWordValue(index, offset)); } else { throw new ValueParseException(string.Format("Current PLC Device do not support CV32{0} address", index)); } } } else { Match match2 = IntKValueRegex.Match(valueString); if (match2.Success) { return(new KDoubleWordValue(int.Parse(match2.Groups[1].Value))); } else { var match3 = IntHValueRegex.Match(valueString); if (match3.Success) { return(new HDoubleWordValue(int.Parse(match3.Groups[1].Value, System.Globalization.NumberStyles.HexNumber))); } else { // 变量 Match match4 = VariableRegex.Match(valueString); if (match4.Success) { var name = match4.Groups[1].Value; try { var variable = VariableManager.GetVariableByName(name); var doublewordvalue = variable as VariableDoubleWordValue; if (doublewordvalue != null) { return(doublewordvalue); } } catch { throw new ValueParseException(string.Format("No DoubleWord Value found for variable {0}", name)); } throw new ValueParseException(string.Format("Variable {0} is not a DoubleWord Value", name)); } } } } throw new ValueParseException("Unexpected input"); }
public static FloatValue ParseFloatValue(string valueString, Device contextDevice) { Match match1 = FloatRegex.Match(valueString); if (match1.Success) { uint index = uint.Parse(match1.Groups[2].Value); WordValue offset = null; if (match1.Groups[4].Success) { if (match1.Groups[4].Value.ToUpper() == "V") { uint vindex = uint.Parse(match1.Groups[5].Value); if (contextDevice.VRange.AssertValue(vindex)) { offset = new VWordValue(vindex); } else { throw new ValueParseException(string.Format("Current PLC Device do not support V{0} address", vindex)); } } else { if (match1.Groups[4].Value.ToUpper() == "Z") { uint zindex = uint.Parse(match1.Groups[5].Value); if (contextDevice.ZRange.AssertValue(zindex)) { offset = new ZWordValue(zindex); } else { throw new ValueParseException(string.Format("Current PLC Device do not support Z{0} address", zindex)); } } } } if (contextDevice.DRange.AssertValue(index)) { return(new DFloatValue(index, offset)); } else { throw new ValueParseException(string.Format("Current PLC Device do not support D{0} address", index)); } } else { Match match2 = FloatKValueRegex.Match(valueString); if (match2.Success) { return(new KFloatValue(float.Parse(match2.Groups[1].Value))); } else { // 变量 Match match3 = VariableRegex.Match(valueString); if (match3.Success) { var name = match3.Groups[1].Value; try { var variable = VariableManager.GetVariableByName(name); var floatvalue = variable as VariableFloatValue; if (floatvalue != null) { return(floatvalue); } } catch { throw new ValueParseException(string.Format("No Float Value found for variable {0}", name)); } throw new ValueParseException(string.Format("Variable {0} is not a Float Value", name)); } } } throw new ValueParseException("Unexpected input"); }
public static BitValue ParseBitValue(string valueString, Device contextDevice) { Match match = BitRegex.Match(valueString); if (match.Success) { uint index = uint.Parse(match.Groups[2].Value); WordValue offset = null; if (match.Groups[4].Success) { if (match.Groups[4].Value.ToUpper() == "V") { uint vindex = uint.Parse(match.Groups[5].Value); if (contextDevice.VRange.AssertValue(vindex)) { offset = new VWordValue(vindex); } else { throw new ValueParseException(string.Format("Current PLC Device do not support V{0} address", vindex)); } } else { if (match.Groups[4].Value.ToUpper() == "Z") { uint zindex = uint.Parse(match.Groups[5].Value); if (contextDevice.ZRange.AssertValue(zindex)) { offset = new ZWordValue(zindex); } else { throw new ValueParseException(string.Format("Current PLC Device do not support Z{0} address", zindex)); } } } } if (match.Groups[1].Value.ToUpper() == "X") { if (contextDevice.XRange.AssertValue(index)) { return(new XBitValue(index, offset)); } else { throw new ValueParseException(string.Format("Current PLC Device do not support X{0} address", index)); } } if (match.Groups[1].Value.ToUpper() == "Y") { if (contextDevice.YRange.AssertValue(index)) { return(new YBitValue(index, offset)); } else { throw new ValueParseException(string.Format("Current PLC Device do not support Y{0} address", index)); } } if (match.Groups[1].Value.ToUpper() == "M") { if (contextDevice.MRange.AssertValue(index)) { return(new MBitValue(index, offset)); } else { throw new ValueParseException(string.Format("Current PLC Device do not support M{0} address", index)); } } if (match.Groups[1].Value.ToUpper() == "C") { if (contextDevice.YRange.AssertValue(index)) { return(new CBitValue(index, offset)); } else { throw new ValueParseException(string.Format("Current PLC Device do not support C{0} address", index)); } } if (match.Groups[1].Value.ToUpper() == "T") { if (contextDevice.TRange.AssertValue(index)) { return(new TBitValue(index, offset)); } else { throw new ValueParseException(string.Format("Current PLC Device do not support T{0} address", index)); } } if (match.Groups[1].Value.ToUpper() == "S") { if (contextDevice.SRange.AssertValue(index)) { return(new SBitValue(index, offset)); } else { throw new ValueParseException(string.Format("Current PLC Device do not support S{0} address", index)); } } } else { // 变量 Match match2 = VariableRegex.Match(valueString); if (match2.Success) { var name = match2.Groups[1].Value; try { var variable = VariableManager.GetVariableByName(name); VariableBitValue bitvalue = variable as VariableBitValue; if (bitvalue != null) { return(bitvalue); } } catch { throw new ValueParseException(string.Format("No Bit Value found for variable {0}", name)); } throw new ValueParseException(string.Format("Variable {0} is not a Bit Value", name)); } } throw new ValueParseException("Unexpected input"); }