protected Instrument( string title, UnitConversionCollection units, CalibrationFunctionsCollection calibFunc, int preferredGain, ChannelType channelType) { //Label = label; menuItem = new InstrumentTypeOption(this); menuItem.SubOptions.Add(units.MenuItem); if (calibFunc.Count > 0) { menuItem.SubOptions.Add(calibFunc.MenuItem); } this.UnitConversions = units; this.Title = title; if (calibFunc.Count == 0) { calibFunc.Add(new G1Function(), ""); } this.calibTF = calibFunc; this.RequiredGain = preferredGain; this.ChannelType = channelType; }
public static Instrument FromFile(string iFile) { try { string[] iLines = File.ReadAllLines(iFile); string title = Path.GetFileNameWithoutExtension(iFile); string resetToOffsetString = "Reset to Offset", resetToOffsetDescription = "Enter a value"; ChannelType icType = ChannelType.None; int i2cInsType = -1; bool iHasCalib = true; UnitConversionCollection units = null; InstrumentRangeCollection ranges = null; List <InstrumentCommand> iCommands = new List <InstrumentCommand>(); CalibrationFunctionsCollection calibTFCollection = new CalibrationFunctionsCollection();; int iGain = 1; bool resetToZeroEnabled = false, resetToOffsetEnabled = false; units = new UnitConversionCollection(); ranges = new InstrumentRangeCollection(); foreach (var iLine in iLines) { var parts = iLine.Split(new char[] { '=' }, 2); for (int i = 0; i < parts.Length; i++) { parts[i] = parts[i].Trim(); } if (parts.Length == 1) { if (parts[0].StartsWith("//") || parts[0].StartsWith("%")) // its a comment { continue; } } parts[0] = parts[0].ToLower().Replace(" ", ""); if (parts[0] == "title") { title = parts[1]; } else if (parts[0] == "gain") { iGain = Convert.ToInt32(parts[1]); } else if (parts[0] == "channeltype") { icType = (ChannelType)Convert.ToByte(parts[1]); } else if (parts[0] == "instrumentid") { i2cInsType = Convert.ToInt16(parts[1]); } else if (parts[0] == "hascalibration") { iHasCalib = parts[1].ToLower() == "true" || parts[1].ToLower() == "1" || parts[1].ToLower().StartsWith("enable") || parts[1].ToLower().StartsWith("yes"); } else if (parts[0] == "resettozero") { resetToZeroEnabled = parts[1].ToLower() == "true" || parts[1].ToLower() == "1" || parts[1].ToLower().StartsWith("enable") || parts[1].ToLower().StartsWith("yes"); } else if (parts[0] == "resettooffset") { resetToOffsetEnabled = parts[1].ToLower() == "true" || parts[1].ToLower() == "1" || parts[1].ToLower().StartsWith("enable") || parts[1].ToLower().StartsWith("yes"); } else if (parts[0] == "resettooffsetstring") { resetToOffsetString = parts[1]; } else if (parts[0] == "resettooffsetdescription") { resetToOffsetDescription = parts[1]; } else if (parts[0] == "command") // only in i2c Instruments { var com = InstrumentCommand.Parse(parts[1]); if (com != null) { iCommands.Add(com); } } else if (parts[0] == "unit") { var u = UnitConversion.Parse(parts[1]); if (u != null) { units.Add(u); } } else if (parts[0] == "range") { var r = InstrumentRange.Parse(parts[1]); if (r != null) { ranges.Add(r); } } else { } } if (units.Count == 0) { units.Add(new UnitConversion("untitled", "", new G1Function())); } if (ranges.Count == 0) { ranges.Add(new InstrumentRange("untitled", new G1Function(), 0)); } var calibFiles = Directory.GetFiles("CalibrationData", title + "*.plf"); if (iHasCalib) { foreach (var cFile in calibFiles) { var cLines = File.ReadAllLines(cFile); string cID = (units.Count + 1).ToString(); Function itf = null; foreach (var cLine in cLines) { var parts = cLine.Split(new char[] { '=' }, 2); parts[0] = parts[0].ToLower(); if (parts[0] == "instrumentid") { cID = parts[1]; } else if (parts[0] == "tf") { itf = Function.Parse(parts[1]); } } if (itf != null) { calibTFCollection.Add(itf, cID); } } if (calibTFCollection.Count == 0) { units.Items.Clear(); units.Add(new UnitConversion("Voltage", "", new G1Function())); //calibTFCollection.Add(new G1Function(), ""); } } if (units.Count == 0 || icType == ChannelType.None) { return(null); } else { if (i2cInsType < 0) { return(new GenericInstrument(title, units, resetToZeroEnabled, resetToOffsetEnabled, resetToOffsetString, resetToOffsetDescription, calibTFCollection, iGain, icType)); } else { return(new I2CInstrument( title, i2cInsType, units, ranges, iCommands, resetToZeroEnabled, resetToOffsetEnabled, resetToOffsetString, resetToOffsetDescription, calibTFCollection, iGain, icType)); } } } catch (Exception ex) { return(null); } }