using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Text.Json; using System.Threading.Tasks; using System.Windows.Forms; namespace daq_testing { public partial class MainForm : Form { public Instrument DAQ; List pslineMapping; List pslineConfigs; UserControlMeasure MeasureUserControl = new UserControlMeasure(); UserControlPslineConfig PslineConfigUserControl = new UserControlPslineConfig(); UserControlInstrument InstrumentUsercontrol = new UserControlInstrument(); UserControlMeasureHeader MeasureHeaderUsercontrol = new UserControlMeasureHeader(); UserControlDMM DMMUserControl = new UserControlDMM(); public List DaqSled1List = new List(); public List DaqSled2List = new List(); public List DaqSled3List = new List(); List Results = new List(); //public string DataloggerSerial { get; set; } public List MeasuredPositionList = new List(); public List MeasuredHeaderPinsPositionList = new List(); public MainForm() { InitializeComponent(); btnMeasure.Enabled = false; btnConfigPslines.Enabled = false; this.WindowState = FormWindowState.Maximized; DAQ = new Instrument(); GetMapping(); GetPsLineConfigFromJson(); //GetPsLineConfigFromCsv(); //GetDefaultPsLineConfig(); panelRight.Controls.Add(InstrumentUsercontrol); panelRight.Controls.Add(PslineConfigUserControl); panelRight.Controls.Add(MeasureUserControl); panelRight.Controls.Add(MeasureHeaderUsercontrol); panelRight.Controls.Add(DMMUserControl); PslineConfigUserControl.Data = pslineConfigs; panelRight.Controls[0].Show(); panelRight.Controls[1].Hide(); panelRight.Controls[2].Hide(); panelRight.Controls[3].Hide(); panelRight.Controls[4].Hide(); InstrumentUsercontrol.InstrumentConnectButtonClicked += ConnectInstrument; PslineConfigUserControl.UpdateBtnClick += UpdateButton; MeasureUserControl.MeasureButtonClick += Measure; MeasureUserControl.ClearAllMeasurementsBtnClick += ClearAllMeasurements; MeasureHeaderUsercontrol.MeasureButtonClick += MeasureHeader; MeasureHeaderUsercontrol.ClearAllMeasurementsBtnClick += ClearAllMeasurementsHeader; MeasureHeaderUsercontrol.PopuplateHeaderEvent += PopulateHeaders; } public void PopulateHeaders(object sender, EventArgs e) { } public List FakeResults() { List results = new List(); Random random = new Random(); for (int i = 1; i < 40; i++) { Result result = new Result() { ID = i, PsLine = random.Next(4)+1, Position = i, TimeStamp = DateTime.Now, Value =(random.NextDouble()+0.1)*2 , }; results.Add(result); } return results; } public List FakeResultsHeader() { List results = new List(); Random random = new Random(); for (int i = 1; i < 40; i++) { Result result = new Result() { ID = i, PsLine = random.Next(4) + 1, Position = i, TimeStamp = DateTime.Now, Value = (random.NextDouble() + 0.1) * 2, BiB = MeasureHeaderUsercontrol.BiBID, DriverID = "Simulation Driver", }; results.Add(result); } return results; } private void ConnectInstrument(object sender, EventArgs e) { DAQ.ResourceDesc = InstrumentUsercontrol.VisaAddress; if (DAQ.InitializeInstrument()) { InstrumentUsercontrol.SetConnectButtonActive(); MeasureUserControl.DataloggerSerial = DAQ.SerialNumber; btnConfigPslines.Enabled = true; } else { InstrumentUsercontrol.SetConnectButtonNonActive(); btnConfigPslines.Enabled = false; } } public void UpdateButton(object sender, EventArgs e) { btnMeasure.Enabled = true; GetPsLineConfigFromJson(); } public void Measure(object sender, EventArgs e) { PositionListToDataloggerChannels(); //var data = DAQ.GetMeasurementByScanList(scanlist); GetDataFromDaq(); MeasureUserControl.PopulateVoltage( this.Results); } public void MeasureHeader( object sender, EventArgs e) { var headerList = MeasureHeaderUsercontrol.CheckedItems; HeaderPinsListToDataloggerChannels(headerList); #if DEBUG this.Results = FakeResultsHeader(); #else GetDataFromDaqHeader(); #endif MeasureHeaderUsercontrol.PopulateVoltage(this.Results); } public void ClearAllMeasurements(object sender, EventArgs e) { DialogResult dialogResult = MessageBox.Show("Are you sure?", "Clear All Measurements", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { //do something foreach (var result in Results) { result.Value = 0.0; } MeasureUserControl.PopulateVoltage(this.Results); } else if (dialogResult == DialogResult.No) { //do something else } } public void ClearAllMeasurementsHeader(object sender, EventArgs e) { DialogResult dialogResult = MessageBox.Show("Are you sure?", "Clear All Measurements", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { //do something this.Results.Clear(); MeasureHeaderUsercontrol.PopulateVoltage(this.Results); } else if (dialogResult == DialogResult.No) { //do something else } } public void GetDataFromDaq() { if (DaqSled1List.Count >=1) { List sled1 = new List(); foreach (var item in DaqSled1List) { sled1.Add(item.daqchannel.ToString()); } Console.WriteLine("bla"); string scanlist = String.Join(",", sled1); DAQ.ScanList = scanlist; DAQ.ConfigInstrument(); var datasled1 = DAQ.GetMeasurementByScanList(scanlist); SaveResults(DaqSled1List, datasled1); } if (DaqSled2List.Count >= 1) { List sled2 = new List(); foreach (var item in DaqSled2List) { sled2.Add(item.daqchannel.ToString()); } string scanlist = String.Join(",", sled2); DAQ.ScanList = scanlist; DAQ.ConfigInstrument(); var datasled2 = DAQ.GetMeasurementByScanList(scanlist); SaveResults(DaqSled2List, datasled2); } if (DaqSled3List.Count >= 1) { List sled3 = new List(); foreach (var item in DaqSled3List) { sled3.Add(item.daqchannel.ToString()); } string scanlist = String.Join(",", sled3); DAQ.ScanList = scanlist; DAQ.ConfigInstrument(); var datasled3 = DAQ.GetMeasurementByScanList(scanlist); SaveResults(DaqSled3List, datasled3); } string fileName = string.Format("{0}_Result.json", MeasureUserControl.ProjectNumber); string jsonString = JsonSerializer.Serialize(this.Results); File.WriteAllText(fileName, jsonString); } public void GetDataFromDaqHeaderOld() { if (DaqSled1List.Count >= 1) { List sled1 = new List(); foreach (var item in DaqSled1List) { sled1.Add(item.daqchannel.ToString()); } Console.WriteLine("bla"); string scanlist = String.Join(",", sled1); DAQ.ScanList = scanlist; DAQ.ConfigInstrument(); var datasled1 = DAQ.GetMeasurementByScanList(scanlist); SaveResultsHeader(DaqSled1List, datasled1); } string fileName = string.Format("{0}_Result.json", MeasureUserControl.ProjectNumber); string jsonString = JsonSerializer.Serialize(this.Results); File.WriteAllText(fileName, jsonString); } public void ShowContinueMessageBox() { // Display the message box DialogResult result = MessageBox.Show("Please move header", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Question); // Check the result if (result == DialogResult.Yes) { // User clicked Yes MessageBox.Show("You chose to continue."); // Add your logic to continue here } else if (result == DialogResult.No) { // User clicked No MessageBox.Show("You chose not to continue."); // Add your logic for cancellation here } } public void GetDataFromDaqHeader() { if (DaqSled1List.Count >= 1) { List sled1 = new List(); foreach (var item in DaqSled1List) { sled1.Add(item.daqchannel.ToString()); } //Console.WriteLine("bla"); //string scanlist = String.Join(",", sled1); List value = new List(); var maxChannel = DaqSled1List.Max(x => x.position); Console.WriteLine(maxChannel); if (maxChannel>9) { List list1 = DaqSled1List.Where(x => x.position <= 9).ToList(); List list2 = DaqSled1List.Where(x => x.position > 9).ToList(); foreach (var item in list2) { DAQ.DAQInstrument.Voltage.DCVoltage.AutoRange[item.daqchannel] = true; value.Add(DAQ.DAQInstrument.Measure.DCVoltMeasure(item.daqchannel.ToString(),10, 0.01)[0].ToString()); } ShowContinueMessageBox(); foreach (var item in list1) { DAQ.DAQInstrument.Voltage.DCVoltage.AutoRange[item.daqchannel] = true; value.Add(DAQ.DAQInstrument.Measure.DCVoltMeasure(item.daqchannel.ToString(), 10, 0.01)[0].ToString()); } } else { foreach (var item in DaqSled1List) { DAQ.DAQInstrument.Voltage.DCVoltage.AutoRange[item.daqchannel] = true; value.Add(DAQ.DAQInstrument.Measure.DCVoltMeasure(item.daqchannel.ToString(), 10, 0.01)[0].ToString()); } } var datasled1 = value.ToArray(); SaveResultsHeader(DaqSled1List, datasled1); } string fileName = string.Format("{0}_Result.json", MeasureUserControl.ProjectNumber); string jsonString = JsonSerializer.Serialize(this.Results); File.WriteAllText(fileName, jsonString); } public void SaveResults(List DaqSledList, string[] data) { List SortedDaqSledList = DaqSledList.OrderBy(o => o.daqchannel).ToList(); int counter = 0; foreach (var item in SortedDaqSledList) { CheckIfItemExists(item); var powerSupplyEnabled = pslineConfigs.Where(x => x.Psline == item.psline).Any(x => x.Enabled); if (powerSupplyEnabled) { double measuredVoltage = Convert.ToDouble(data[counter]); //if (Math.Abs(measuredVoltage) < 0.1) { measuredVoltage = 0.0; } Result result = new Result() { Position = item.position, PsLine = item.psline, TimeStamp = DateTime.UtcNow, MeasureLocation = MeasureUserControl.MeasurementLocation, BiB = MeasureHeaderUsercontrol.BiBID, Value = measuredVoltage }; Results.Add(result); } counter++; } } public void SaveResultsHeader(List DaqSledList, string[] data) { // List SortedDaqSledList = DaqSledList.OrderBy(o => o.daqchannel).ToList(); List SortedDaqSledList = DaqSledList.OrderBy(o => o.position).ToList(); int counter = 0; foreach (var item in SortedDaqSledList) { CheckIfItemExistsHeader(item); //var powerSupplyEnabled = pslineConfigs.Where(x => x.Psline == item.psline).Any(x => x.Enabled); if (1==1) //if (powerSupplyEnabled) { double measuredVoltage = Convert.ToDouble(data[counter]); //if (Math.Abs(measuredVoltage) < 0.1) { measuredVoltage = 0.0; } Result result = new Result() { ID = Results.Select(x => x.ID).DefaultIfEmpty(0).Max() + 1, Position = Convert.ToInt16(MeasureHeaderUsercontrol.CurrentMeasurePosition), PsLine = item.position, DriverID = MeasureHeaderUsercontrol.DriverID, BiB = MeasureHeaderUsercontrol.BiBID, Description = pslineConfigs.Where(x => x.Psline == item.position).Select(x => x.Description).First(), TimeStamp = DateTime.UtcNow, MeasureLocation = MeasureHeaderUsercontrol.MeasurementLocation ?? "N/A", Value = measuredVoltage }; Results.Add(result); Console.WriteLine("HELLO?"); } counter++; } } public void CheckIfItemExists(PslineMapping item) { Results.RemoveAll(x => x.Position == item.position & x.PsLine == item.psline); } public void CheckIfItemExistsHeader(PslineMapping item) { //Results.RemoveAll(x => x.Position == item.position & x.PsLine == Convert.ToInt16(MeasureHeaderUsercontrol.CurrentMeasurePosition)); Results.RemoveAll(x => x.PsLine == item.position & x.Position == Convert.ToInt32(MeasureHeaderUsercontrol.CurrentMeasurePosition) & x.BiB == MeasureHeaderUsercontrol.BiBID); } public void HeaderPinsListToDataloggerChannelsOld(List positionList) { MeasuredPositionList.Clear(); int prevPosition = 0; foreach (PslineMapping mapping in pslineMapping) { if (positionList.Contains(mapping.position)) { if (mapping.position != prevPosition) { MeasuredPositionList.Add(mapping); } } prevPosition = mapping.position; } //Console.WriteLine("bla"); DaqSled1List.Clear(); foreach (var position in MeasuredPositionList) { if (position.daqchannel > 0 & position.daqchannel < 141) { DaqSled1List.Add(position); } } DaqSled2List.Clear(); foreach (var position in MeasuredPositionList) { if (position.daqchannel > 200 & position.daqchannel < 241) { DaqSled2List.Add(position); } } DaqSled3List.Clear(); foreach (var position in MeasuredPositionList) { if (position.daqchannel > 300 & position.daqchannel < 341) { DaqSled3List.Add(position); } } } public void HeaderPinsListToDataloggerChannels(List positionList) { MeasuredPositionList.Clear(); Dictionary HeaderMapping = new Dictionary() { {1,101 }, {2,121 }, {3,102 }, {4,122 }, {5,103 }, {6,123 }, {7,104 }, {8,124 }, {9,105 }, }; //headerpin,daqchannel foreach (var position in positionList) { const int HEADERSIZE = 9; PslineMapping mapping= new PslineMapping(); mapping.position = position; if (position > HEADERSIZE) { HeaderMapping.TryGetValue(position - HEADERSIZE, out mapping.daqchannel); } else { HeaderMapping.TryGetValue(position, out mapping.daqchannel); } MeasuredPositionList.Add(mapping); } //Console.WriteLine("bla"); DaqSled1List.Clear(); foreach (var position in MeasuredPositionList) { if (position.daqchannel > 0 & position.daqchannel < 141) { DaqSled1List.Add(position); } } } public void PositionListToDataloggerChannels() { MeasuredPositionList.Clear(); var positionList = MeasureUserControl.PositionList; foreach (PslineMapping mapping in pslineMapping) { if(positionList.Contains(mapping.position)) { MeasuredPositionList.Add(mapping); } } //Console.WriteLine("bla"); DaqSled1List.Clear(); foreach (var position in MeasuredPositionList) { if(position.daqchannel > 0 & position.daqchannel < 141) { DaqSled1List.Add(position); } } DaqSled2List.Clear(); foreach (var position in MeasuredPositionList) { if (position.daqchannel > 200 & position.daqchannel < 241) { DaqSled2List.Add(position); } } DaqSled3List.Clear(); foreach (var position in MeasuredPositionList) { if (position.daqchannel > 300 & position.daqchannel < 341) { DaqSled3List.Add(position); } } } public void GetPsLineConfigFromJson() { string filePath = @"c:\configs\data.json"; var fileHandler = new FileHandler(filePath); pslineConfigs = fileHandler.GetPslineConfig(); MeasureUserControl.pslineConfigs = pslineConfigs; MeasureHeaderUsercontrol.pslineConfigs = pslineConfigs; } public void GetPsLineConfigFromCsv() { string path = Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName, "pslineconfig.csv"); pslineConfigs = File.ReadAllLines(path) .Skip(1) .Select(v => PslineConfig.GetPslineConfigFromCsv(v)) .ToList(); MeasureUserControl.pslineConfigs = pslineConfigs; } public void GetDefaultPsLineConfig() { pslineConfigs = new List(); PslineConfig pc1 = new PslineConfig() { Psline = 1, Description = "vcc1", Setpoint = 1.00, Tolerance = 1.00, Enabled = true }; PslineConfig pc2 = new PslineConfig() { Psline = 2, Description = "vcc2", Setpoint = 1.00, Tolerance = 1.00, Enabled = true }; PslineConfig pc3 = new PslineConfig() { Psline = 3, Description = "vcc3", Setpoint = 1.00, Tolerance = 1.00, Enabled = true }; PslineConfig pc4 = new PslineConfig() { Psline = 4, Description = "vcc4", Setpoint = 1.00, Tolerance = 1.00, Enabled = true }; PslineConfig pc5 = new PslineConfig() { Psline = 5, Description = "vcc5", Setpoint = 1.00, Tolerance = 1.00, Enabled = true }; PslineConfig pc6 = new PslineConfig() { Psline = 6, Description = "vcc5", Setpoint = 1.00, Tolerance = 1.00, Enabled = false }; PslineConfig pc7 = new PslineConfig() { Psline = 7, Description = "vcc7", Setpoint = 1.00, Tolerance = 1.00, Enabled = true }; PslineConfig pc8 = new PslineConfig() { Psline = 8, Description = "vcc8", Setpoint = 1.00, Tolerance = 1.00, Enabled = true }; PslineConfig pc9 = new PslineConfig() { Psline = 9, Description = "vcc9", Setpoint = 1.00, Tolerance = 1.00, Enabled = true }; PslineConfig pc10 = new PslineConfig() { Psline = 10, Description = "vcc10", Setpoint = 1.00, Tolerance = 1.00, Enabled = false }; pslineConfigs.Add(pc1); pslineConfigs.Add(pc2); pslineConfigs.Add(pc3); pslineConfigs.Add(pc4); pslineConfigs.Add(pc5); pslineConfigs.Add(pc6); pslineConfigs.Add(pc7); pslineConfigs.Add(pc8); pslineConfigs.Add(pc9); pslineConfigs.Add(pc10); MeasureUserControl.pslineConfigs = pslineConfigs; } private void GetMapping() { try { //string path = Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName, "pslinemapping.csv"); string path = @"c:\configs\pslinemapping.csv"; pslineMapping = File.ReadAllLines(path) .Skip(1) .Select(v => PslineMapping.FromCsv(v)) .ToList(); } catch (Exception) { throw; } } private void Form1_Load(object sender, EventArgs e) { } private void btnInstrument_Click(object sender, EventArgs e) { panelRight.Controls[0].Show(); panelRight.Controls[1].Hide(); panelRight.Controls[2].Hide(); panelRight.Controls[3].Hide(); panelRight.Controls[4].Hide(); } private void btnConfigPslines_Click(object sender, EventArgs e) { panelRight.Controls[1].Show(); panelRight.Controls[0].Hide(); panelRight.Controls[2].Hide(); panelRight.Controls[3].Hide(); panelRight.Controls[4].Hide(); PslineConfigUserControl.OnUpdateBtnClick(EventArgs.Empty); } private void btnMeasure_Click(object sender, EventArgs e) { panelRight.Controls[2].Show(); panelRight.Controls[0].Hide(); panelRight.Controls[1].Hide(); panelRight.Controls[3].Hide(); panelRight.Controls[4].Hide(); } private void btn_MeasureHeader_Click(object sender, EventArgs e) { panelRight.Controls[3].Show(); panelRight.Controls[0].Hide(); panelRight.Controls[1].Hide(); panelRight.Controls[2].Hide(); panelRight.Controls[4].Hide(); MeasureHeaderUsercontrol.PopulateMeasureHeader(); } private void btnDMM_Click(object sender, EventArgs e) { panelRight.Controls[4].Show(); panelRight.Controls[0].Hide(); panelRight.Controls[1].Hide(); panelRight.Controls[2].Hide(); panelRight.Controls[3].Hide(); } } }