-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeviceFamily10.cs
90 lines (69 loc) · 2.96 KB
/
DeviceFamily10.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
90
namespace OneWireAPI
{
public class DeviceFamily10 : Device
{
public DeviceFamily10(Session session, short[] id)
: base(session, id)
{
// Just call the base constructor
}
public double GetTemperature()
{
// Select and access the ID of the device we want to talk to
Adapter.Select(Id);
// Setup for for power delivery after the next byte
Adapter.SetLevel(TMEX.LevelOperation.Write, TMEX.LevelMode.StrongPullup, TMEX.LevelPrime.AfterNextByte);
try
{
// Send the byte and start strong pullup
Adapter.SendByte(0x44);
}
catch
{
// Stop the strong pullup
Adapter.SetLevel(TMEX.LevelOperation.Write, TMEX.LevelMode.Normal, TMEX.LevelPrime.Immediate);
// Re-throw the exception
throw;
}
// Sleep while the data is transfered
System.Threading.Thread.Sleep(1000);
// Stop the strong pullup
Adapter.SetLevel(TMEX.LevelOperation.Write, TMEX.LevelMode.Normal, TMEX.LevelPrime.Immediate);
// Access the device we want to talk to
Adapter.Access();
// Data buffer to send over the network
var data = new byte[30];
// How many bytes of data to send
short dataCount = 0;
// Set the command to get the temperature from the scatchpad
data[dataCount++] = 0xBE;
// Setup the rest of the bytes that we want
for (var i = 0; i < 9; i++)
data[dataCount++] = 0xFF;
// Send the data block and get data back
Adapter.SendBlock(data, dataCount);
// Calculate the CRC of the first eight bytes of data
var crc = Crc8.Calculate(data, 1, 8);
// Check to see if our CRC matches the CRC supplied
if (crc != data[9])
{
// Throw a CRC exception
throw new OneWireException(OneWireException.ExceptionFunction.Crc, Id);
}
// Get the LSB of the temperature data and divide it by two
var temperatureLsb = data[1] / 2;
// If the data is negative then flip the bits
if ((data[2] & 0x01) == 0x01) temperatureLsb |= -128;
// Convert the temperature into a double
var temperature = (double) temperatureLsb;
// Get the number of counts remaining
double countRemaining = data[7];
// Get the number of counts per degree C
double countPerDegreeC = data[8];
// Use the "counts remaining" data to calculate the temperaure to greater accuracy
temperature = temperature - 0.25F + (countPerDegreeC - countRemaining) / countPerDegreeC;
// Return the temperature
return temperature;
}
}
}