private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { try { string r = serialPort.ReadLine(); if (r.Length != 4) { return; } int c1 = r[0]; string v = "0x" + r.Substring(1, 3); int val = Convert.ToInt32(v, 16); if (val > 1024) { return; } switch (c1 >> 4) { case 0x1: break; case 0x2: break; case 0x3: int value = val; SensorEventArgs arg = new SensorEventArgs(); arg.Data = value; arg.Sensor = (Sensors)(c1 & 0x0F); SensorDataReceived?.Invoke(this, arg); break; case 0x4: break; case 0x5: break; case 0x6: break; } } catch { } }
private async Task ProcessEventHandler(ProcessEventArgs eventArgs) { try { BinaryData bytes = eventArgs.Data.EventBody; string json = Encoding.UTF8.GetString(bytes); SensorData watchData = JsonConvert.DeserializeObject <SensorData>(json); SensorDataReceived?.Invoke(this, new SensorDataReceivedEventArgs(watchData)); // Write the body of the event to the console window //Console.WriteLine("\tReceived event: {0}", Encoding.UTF8.GetString(eventArgs.Data.Body.ToArray())); // Update checkpoint in the blob storage so that the app receives only new events the next time it's run await eventArgs.UpdateCheckpointAsync(eventArgs.CancellationToken); } catch (Exception exc) { System.Console.WriteLine(exc.Message); } }
protected virtual void OnSensorDataReceived(SensorData data) { SensorDataReceived?.Invoke(this, data); }