public static VariableBitValue CreateVariableBitValue(string name, string mappedValuestring, string comment) { // 匿名 if (mappedValuestring == string.Empty) { VariableBitValue variable = new VariableBitValue(name, comment); return(variable); } else { // 非匿名 var value = ParseBitValue(mappedValuestring, PLCDevice.Device.DefaultDevice); if (value.IsVariable) { throw new ValueParseException("Can not map variable to variable"); } else { VariableBitValue variable = new VariableBitValue(name, value, comment); return(variable); } } }
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"); }