private void ShowBluetoothStatus(object sender, BluetoothEventArgs e) { if (e.bolBluetoothConnected) { lblBluetoothConnection.Invoke((Action) delegate { lblBluetoothConnection.Text = "Verbunden mit Reach RS+"; }); picIconGrey.Invoke((Action) delegate { picIconGrey.Visible = false; }); picIconBlue.Invoke((Action) delegate { picIconBlue.Visible = true; }); } if (e.bolDataAvailable && bytQualityOld == 0) { lblRealtimeAverage.Invoke((Action) delegate { lblRealtimeAverage.Text = "UTM Koordinaten: Echtzeit"; }); btnTimer.Invoke((Action) delegate { btnTimer.Enabled = true; }); } }
public void ReceiveData() { // Get current path of "ReachConnectCE.exe" and check if file "mac.txt" exists (contains bluetooth MAC adress of Reach RS+) string strCurrentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase); if (!File.Exists(strCurrentDirectory + "\\mac.txt")) { MessageBox.Show("Die Datei \"mac.txt\" im Verzeichnis " + strCurrentDirectory + " wurde nicht gefunden. " + "Bitte Datei anlegen mit folgendem Inhalt: Bluetooth MAC-Adresse des Reach RS+ Empfängers.", "Lesefehler", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1); Application.Exit(); } // Read bluetooth MAC adress from file "mac.txt" and store in strMAC FileStream fs = new FileStream(strCurrentDirectory + "\\mac.txt", FileMode.Open); StreamReader sr = new StreamReader(fs); string strMAC = sr.ReadLine(); sr.Close(); DataEventArgs args = new DataEventArgs(); BluetoothEventArgs b = new BluetoothEventArgs(); // Initialize bluetooth stack try { BluetoothRadio radio = BluetoothRadio.PrimaryRadio; radio.Mode = RadioMode.Discoverable; } // Catch exeption if bluetooth radio not found catch { MessageBox.Show("Der Bluetooth Empfänger wurde nicht gefunden. Bitte Bluetooth einschalten.", "Bluetooth Fehler", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1); Application.Exit(); } // Establish bluetooth connection BluetoothClient bcConn = new BluetoothClient(); do { try { bcConn.Connect(BluetoothAddress.Parse(strMAC), BluetoothService.SerialPort); } catch { } }while (!bcConn.Connected); // Update GUI once bluetooth connected b.bolBluetoothConnected = true; this.BluetoothStatus(this, b); // Open bluetooth socket NetworkStream stream = bcConn.GetStream(); byte[] bytReadBuffer = new byte[142]; double[] utmCoordinates = new double[4]; string strMessage, strLatitude, strLongitude, strHeight, strQuality; double dblLatitude, dblLongitude; while (true) { try { stream.Read(bytReadBuffer, 0, 142); // Parse data from stream strMessage = System.Text.Encoding.ASCII.GetString(bytReadBuffer, 0, 142); strLatitude = strMessage.Substring(25, 13); strLongitude = strMessage.Substring(40, 12); strHeight = strMessage.Substring(55, 9); strQuality = strMessage.Substring(67, 1); if (strQuality == "1" || strQuality == "2" || strQuality == "5") { // Update GUI once data is received b.bolDataAvailable = true; this.BluetoothStatus(this, b); dblLatitude = Convert.ToDouble(strLatitude); dblLongitude = Convert.ToDouble(strLongitude); args.dblHeight = Convert.ToDouble(strHeight); args.bytQuality = Convert.ToByte(strQuality); // Convert latitude/longitude to UTM by using UTMConverter class. UTMConverter is a static class // which cannot be instanciated. latlontoUTM method expects two values (latitude and lontitude) and // returns array with 4 double values: [0] = UTM east, [1] = UTM north, [2] = zone, [3] = hemisphere utmCoordinates = UTMConverter.latlontoUTM(dblLatitude, dblLongitude); args.dblEast = utmCoordinates[0]; args.dblNorth = utmCoordinates[1]; args.strZone = utmCoordinates[2].ToString(); if (utmCoordinates[3] == 0) { args.strHemisphere = "Süd"; } else { args.strHemisphere = "Nord"; } this.NewDataAvailable(this, args); } } catch { } } }