-
Notifications
You must be signed in to change notification settings - Fork 0
/
Testo549iProtocol.cs
89 lines (72 loc) · 3.01 KB
/
Testo549iProtocol.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.UI.Xaml.Controls;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Linq;
namespace AcaiaLogger
{
public sealed partial class MainPage : Page
{
string TestoServiceGuid = "0000fff0-0000-1000-8000-00805f9b34fb";
string TestoCharactNotifGuid = "0000fff2-0000-1000-8000-00805f9b34fb";
string TestoCharactWriteGuid = "0000fff1-0000-1000-8000-00805f9b34fb";
private void WriteCommandsToEnablePressureMeasurements()
{
// command 1
byte[] payload = new byte[] { 0x56, 0x00, 0x03, 0x00, 0x00, 0x00, 0x0c, 0x69, 0x02, 0x3e, 0x81 };
writeToTesto(payload);
// command 2
payload = new byte[] { 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x7b };
writeToTesto(payload);
// command 3
payload = new byte[] { 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x5a };
writeToTesto(payload);
}
private void WriteZeroPressure()
{
byte[] payload = new byte[] { 0x05, 0x00, 0x13, 0x00, 0x00, 0x00, 0x04, 0xca, 0x0c, 0x00, 0x00, 0x00, 0x5a, 0x65, 0x72, 0x6f, 0x50, 0x72, 0x65, 0x73 };
writeToTesto(payload);
payload = new byte[] { 0x73, 0x75, 0x72, 0x65, 0x01, 0x62, 0xe5 };
writeToTesto(payload);
}
private async void writeToTesto(byte[] payload)
{
try
{
// BT_Code: Writes the value from the buffer to the characteristic.
var result = await characteristicTestoWrite.WriteValueWithResultAsync(payload.AsBuffer());
if (result.Status != GattCommunicationStatus.Success)
FatalError("Failed to write to Testo characteristic");
}
catch (Exception ex)
{
FatalError("Failed to write to Testo characteristic " + ex.Message);
}
}
private bool DecodePressure(byte[] data, ref double pressure_bar)
{
if (data == null)
return false;
/* example pressure record
10 80 20 00 00 00 08 95 14 00 00 00 44 69 66 66 65 72 65 6e // first PDU
74 69 61 6c 50 72 65 73 73 75 72 65 00 d0 e9 45 00 00 37 37 // second PDU
we are interested in 4 bytes at offset 32, i.e. 00 d0 e9 45 00 - which are float value in Pa = 1E-5 bar */
if (data.Length != 20)
return false;
byte[] pressure_pattern = new byte[] { 0x74, 0x69, 0x61, 0x6c, 0x50 };
byte[] candidate = new byte[5];
Array.Copy(data, 0, candidate, 0, 5);
if (!candidate.SequenceEqual<byte>(pressure_pattern))
return false;
try
{
pressure_bar = BitConverter.ToSingle(data, 12) * 1E-5;
return true;
}
catch (Exception)
{
return false;
}
}
}
}