/// <summary> /// Closes open serial connection /// </summary> /// <returns></returns> private async Task CloseSerial() { try { IsBusy = true; var comms = Globals.Serials; var query = ComPorts.Select((value, index) => new { value, index }) .Where(x => x.value.Name == _comPort) .Select(x => x.index) .Take(1); var idx = query.ElementAt(0); // Closes the port only if it is currently open if (ComPorts[idx].Used) { // Send message to close the tab attached that is attached to the serial port that is being closed (Application.Current.Properties["MessageBus"] as MessageBus) .Publish(new MessageCenter("closeTab", new Tuple <string, string>(_comPort, comms[_comPort].SerialNo))); comms.Remove(_comPort); ComPorts[idx].Used = false; Globals.Logger.LogInformation($"Closed serial port {_comPort}"); } } finally { IsBusy = false; COMPORT = " "; } }
private void SetDefaultComPorts() { SerialPort.GetPortNames().ToList().ForEach(ComPorts.Add); //Autoselect for EdgePort hookup if (ComPorts.Count > 3) { var number = new Regex(@"(\d+)$"); //Take last 4 var edgeport = ComPorts.Select(c => new { Port = c, Numer = int.Parse(number.Match(c).Value) }) .OrderByDescending(c => c.Numer).Take(4).OrderBy(c => c.Numer).ToArray(); // .Take(4) // (n => ).ToArray(); ELComPort = edgeport[1].Port; ADComPort = edgeport[0].Port; DVComPort = edgeport[2].Port; } }
/// <summary> /// Code used to create/open meter object and create a tab for this item /// </summary> public async Task <string> CreateNewMeter() { Dictionary <string, Meter> meters = Globals.Meters; // Logs into meter and retrieves it's ID _id = await _serial.SetupSerial(_comPort); if (_id == "") { InfoView info = null; InfoViewModel ifvm = new InfoViewModel("Unable to Connect", "COM Port already open, close port or choose another COM..."); Application.Current.Dispatcher.Invoke(() => { info = new InfoView { Owner = Application.Current.MainWindow, WindowStartupLocation = WindowStartupLocation.CenterOwner, DataContext = ifvm }; info.Show(); }); return(null); } var query = ComPorts.Select((value, index) => new { value, index }) .Where(x => x.value.Name == _comPort) .Select(x => x.index) .Take(1); ComPorts[query.ElementAt(0)].Used = true; // Creates new meter if imported meters do not match current meter if (!meters.ContainsKey(_id)) { Globals.Logger.LogInformation($"Creating new meter for {_id}"); meters.Add(_id, new Meter()); var meter = meters[_id]; meter.ID = _id; // Sets the size of the meter based on the type/version of meter connected string version = await _serial.GetVersion(); string[] lines = version.Split(new char[] { '\n', '\r' }, System.StringSplitOptions.RemoveEmptyEntries); string inspect = ""; bool stop = false; foreach (string line in lines) { if (line.ToLower().Contains("software")) { stop = true; } else if (stop) { inspect = line; break; } } var cols = inspect.Split(new char[0], System.StringSplitOptions.RemoveEmptyEntries); string[] serials = (await _serial.GetChildSerial()).Split(','); if (cols[1] == "59330354") { meter.Firmware = cols[1]; _serial.NewFirmware = true; // Retrieves child serial numbers and assigns them to respective channel for (int i = 0; i < 12; i++) { meter.Channels.Add(new Channel(i + 1)); meter.Channels[i].Serial = serials[i]; } Globals.Logger.LogInformation($"Meter {_id} has firmware version {meter.Firmware}."); } else if (cols[1] == "59330353") { meter.Firmware = cols[1]; // Retrieves child serial numbers and assigns them to respective channel for (int i = 0; i < 12; i++) { meter.Channels.Add(new Channel(i + 1)); meter.Channels[i].Serial = serials[i]; } Globals.Logger.LogInformation($"Meter {_id} has firmware version {meter.Firmware}."); } else { InfoView info = null; InfoViewModel ifvm = new InfoViewModel("Bad Firmware Version", $"Invalid Firmware Version {cols[1]}"); Application.Current.Dispatcher.Invoke(() => { info = new InfoView { Owner = Application.Current.MainWindow, WindowStartupLocation = WindowStartupLocation.CenterOwner, DataContext = ifvm }; info.Show(); }); return(null); } } else { // Firmware was not populated from the file so read directly from the meter if (meters[_id].Firmware == null) { string version = await _serial.GetVersion(); string[] lines = version.Split(new char[] { '\n', '\r' }, System.StringSplitOptions.RemoveEmptyEntries); string inspect = ""; bool stop = false; foreach (string line in lines) { if (line.ToLower().Contains("software")) { stop = true; } else if (stop) { inspect = line; break; } } var cols = inspect.Split(new char[0], System.StringSplitOptions.RemoveEmptyEntries); meters[_id].Firmware = cols[1]; Globals.Logger.LogInformation($"Meter {_id} has firmware version {meters[_id].Firmware}."); } // This is an unknown firmware version, print message to user and exit if (meters[_id].Firmware != "59330353" && meters[_id].Firmware != "59330354") { InfoView info = null; InfoViewModel ifvm = new InfoViewModel("Bad Firmware Version", $"Invalid Firmware Version {meters[_id].Firmware}"); Application.Current.Dispatcher.Invoke(() => { info = new InfoView { Owner = Application.Current.MainWindow, WindowStartupLocation = WindowStartupLocation.CenterOwner, DataContext = ifvm }; info.Show(); }); return(null); } else { _serial.NewFirmware = (meters[_id].Firmware == "59330354"); } } return(_id); }