HXMdata ByteArrayToHXMdata(byte[] bytes) { GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned); HXMdata stuff = (HXMdata)Marshal.PtrToStructure( handle.AddrOfPinnedObject(), typeof(HXMdata)); handle.Free(); return(stuff); }
private static bool BatteryValueRange(HXMdata data) { if (data.BatteryCharge > 100) { Console.WriteLine("Battery Charge Reading our of range. Expected value was less than 100. Actuall was {0}", data.BatteryCharge); return(false); } return(true); }
private static bool StridesValueRange(HXMdata data) { const ushort Maximum = 128; if (data.Strides > Maximum) { Console.WriteLine("Strides Reading is out of range. Expected value was less than 255. Actuall was {0}", data.Strides); return(false); } return(true); }
private static bool HeartRateValueRange(HXMdata data) { const byte Minimum = 30; const byte Maximum = 240; if (data.HeartRate < Minimum || data.HeartRate > Maximum) { Console.WriteLine("Heart Rate Reading is out of range. Expected value was between 30 and 240. Actuall was {0}", data.HeartRate); return(false); } return(true); }
void DataReceived(object sender, SerialDataReceivedEventArgs e) { SerialPort port = (SerialPort)sender; if (port.BytesToRead >= 60) { byte[] buffer = new byte[60]; port.Read(buffer, 0, 60); if (IsStructureValid(buffer)) { HXMdata data = ByteArrayToHXMdata(buffer); //Console.WriteLine(data.RR(0)); if (OnData != null && CheckDataValues(data) && buffer[CRCOffset] == CalculateCRC(buffer)) { OnData(data); } } else { port.Close(); } } }
public static bool CheckDataValues(HXMdata data) { return(BatteryValueRange(data) && HeartRateValueRange(data) && StridesValueRange(data)); }