private async Task EstablishSerial(CancellationToken token) { IsBusy = true; Dictionary <string, SerialComm> comms = Globals.Serials; if (!comms.ContainsKey(COMPORT)) { comms.Add(COMPORT, new SerialComm()); _serial = comms[COMPORT]; } else { _serial = comms[COMPORT]; if (_serial.IsOpen) { return; } } Globals.Logger.LogInformation($"Opened serial port {COMPORT}"); if (await CreateNewMeter() == null) { _serial.Close(); IsBusy = false; Completed = false; return; } Completed = true; // Send message to create new tab that will be able to commission a meter (Application.Current.Properties["MessageBus"] as MessageBus) .Publish(new MessageCenter("newTab", new Tuple <string, string>(_comPort, _id))); }
/// <summary> /// Initializes this instance of the class in order to continuously call /// Phase Diagnostic for Meter. /// </summary> /// <returns></returns> private async Task PhaseDiagnostic() { // Initialize variables if this is the first time if (_serial == null) { int channel = 1; int length = Meter.Channels.Count; _oldPhase1Text = new string[length]; _oldPhase2Text = new string[length]; for (int i = 0; i < 5; i++) { if (i == 4) { int cnt = 0; for (int j = 0; j < length; j++) { int res = (cnt++ % 3); Phase1Text[j, i] = res == 0 ? "A" : res == 1 ? "B" : "C"; res = (cnt++ % 3); Phase2Text[j, i] = res == 0 ? "A" : res == 1 ? "B" : "C"; _allowCheck[j, 0] = false; _allowCheck[j, 1] = false; _oldPhase1Text[j] = string.Empty; _oldPhase2Text[j] = string.Empty; } } else if (i == 3) { // Displays the CT banks while commissioning for (int j = 0; j < length; j++) { Phase1Text[j, i] = channel++.ToString(); Phase2Text[j, i] = channel++.ToString(); } } else { for (int j = 0; j < length; j++) { Phase1Text[j, i] = string.Empty; Phase2Text[j, i] = string.Empty; } } } } // Reset the serial _serial = Globals.Serials[_idx]; // wait for the value to be reset while (_break) { } // Continuous loop to retrieve the phase diagnostic for a meter while (true) { // Requests the Phase Diagnostic from the meter, if successful // continue with discovering whether or not there was a large enough // change var retVal = Process(); if (retVal.Contains("Amps")) { UpdateMessage(); Detection(); } // Switches the connected meter object to this serial comm else if (retVal == "switch") { // Command to change the meter being actively evaluated and // associated with the serial port (Application.Current.Properties["MessageBus"] as MessageBus) .Publish(new MessageCenter("switchMeters")); // Loop here until external code sets break state to avoid race condition while (!_break) { } } // The Process function returns false when user closed the Attempting to reconnect // popup window or if the meter was switched so this function forces // the user to go back a page and cancels the phase diagnostic reads else if (!_break && string.IsNullOrEmpty(retVal)) { (Application.Current.Properties["MessageBus"] as MessageBus) .Publish(new MessageCenter("backward")); // Loop here until external code sets break state to avoid race condition while (!_break) { } } if (_break) { break; } } // reset break value so we can continuously execute this function // if we need to revisit this page _break = false; }