private async void Page_Loaded(object sender, RoutedEventArgs e) { // Display the version txtVersion.Text = string.Format("MeterMate {0} V{1}.{2}", Model, MajorVersion, MinorVersion); // Display the Copyright notice txtCopyright.Text = "Swiftsoft - Copyright © 2016-2017"; // Show that the handset by default is not connected ledHandsetConnected.LedOn = false; ledEmr3Connected.LedOn = false; // Create timer to expire immediately and then every 12 seconds thereafter timer = new Timer(TimerExpired, null, 0, 12000); SerialDevice meterMatePort = null; SerialDevice bluetoothPort = null; try { // meterMatePort = await GetSerialPort("prolific usb-to-serial comm port"); //meterMatePort = await GetSerialPort("usb serial converter"); meterMatePort = await GetSerialPort("USB-RS232 Cable"); if (meterMatePort == null) { meterMatePort = await GetSerialPort("usb serial converter"); } //meterMatePort = await GetSerialPort("usb <-> serial"); //meterMatePort = await GetSerialPort("cp2102 usb to uart bridge controller"); bluetoothPort = await GetSerialPort("nfo"); if (bluetoothPort == null) { bluetoothPort = await GetSerialPort("minwinpc"); } } catch (NullReferenceException) { //txtStatus.Text = "Could not obtain serial port."; } // Start the EMR3 thread Emr3 emr3 = new Emr3(meterMatePort, this); emr3.Start(); // Start the PDA thread Pda pda = new Pda(bluetoothPort, this); pda.Start(); }
public static async Task ProcessMessage(string message) { using (var releaser = await myLock.LockAsync()) { try { // Construct default (error) JSON. string json = "{\"Command\": \"\", \"Result\": -99}"; // Check EMR3 thread is running. If it is // not then wait until it is. while (!Emr3.IsRunning()) { await Task.Delay(250); } // Split the message around commas. string[] parts = message.Split(new char[] { ',' }); // Check there is at least one part if (parts.Length >= 1) { switch (parts[0]) { case "BL": // Bootloader. //SystemUpdate.AccessBootloader(); break; case "Gv": // Get Version. await ParentPage.ResetTimer(); json = string.Format("{{\"Command\": \"Gv\", \"Result\": 0, \"Version\": {0}.{1}, \"Model\":\"{2}\"}}", MainPage.MajorVersion, MainPage.MinorVersion, MainPage.Model); break; case "Gf": // Get features. json = Emr3.GetFeatures(); break; case "Gt": // Get temperature. json = await Emr3.GetTemperature(); break; case "Gs": // Get status. json = await Emr3.GetStatus(); break; case "Gpl": // Get preset litres. json = await Emr3.GetPreset(); break; case "Grl": // Get realtime litres. json = await Emr3.GetRealtime(); break; case "Gtc": // Get transaction count json = await Emr3.GetTranCount(); break; case "Gtr": // Get transaction record await ParentPage.ResetTimer(); if (parts.Length == 2) { json = await Emr3.GetTran(parts[1]); } break; case "Spl": // Set polling. if (parts.Length == 2) { json = Emr3.SetPolling(parts[1]); } break; case "Sp": // Set preset. await ParentPage.ResetTimer(); if (parts.Length == 2) { json = await Emr3.SetPreset(parts[1]); } break; case "NOP": await ParentPage.ResetTimer(); json = "{\"Command\": \"NOP\", \"Result\": 0}"; break; default: json = string.Format("{{\"Command\": \"{0}\", \"Result\": -99}}", parts[0]); break; } } // Send reply to PDA. await SendMessage(json); } catch (Exception ex) { Debug.WriteLine("PDA.ProcessMessage: Exception " + ex.Message); } } }