mirror of
https://github.com/ckaczor/OneWireAPI.git
synced 2026-01-18 09:35:37 -05:00
Code modernization
This commit is contained in:
@@ -1,102 +1,90 @@
|
||||
using System;
|
||||
|
||||
namespace OneWireAPI
|
||||
{
|
||||
public class owDeviceFamily10 : owDevice
|
||||
{
|
||||
#region Constructor
|
||||
public class owDeviceFamily10 : owDevice
|
||||
{
|
||||
public owDeviceFamily10(owSession session, short[] id)
|
||||
: base(session, id)
|
||||
{
|
||||
// Just call the base constructor
|
||||
}
|
||||
|
||||
public owDeviceFamily10(owSession 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
|
||||
owAdapter.Select(DeviceId);
|
||||
|
||||
#endregion
|
||||
// Setup for for power delivery after the next byte
|
||||
owAdapter.SetLevel(TMEX.LevelOperation.Write, TMEX.LevelMode.StrongPullup, TMEX.LevelPrime.AfterNextByte);
|
||||
|
||||
#region Methods
|
||||
try
|
||||
{
|
||||
// Send the byte and start strong pullup
|
||||
owAdapter.SendByte(0x44);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Stop the strong pullup
|
||||
owAdapter.SetLevel(TMEX.LevelOperation.Write, TMEX.LevelMode.Normal, TMEX.LevelPrime.Immediate);
|
||||
|
||||
public double GetTemperature()
|
||||
{
|
||||
short nResult; // Result of method calls
|
||||
byte[] aData = new byte[30]; // Data buffer to send over the network
|
||||
short nDataCount = 0; // How many bytes of data to send
|
||||
double dCountRemaining; // How many counters remain in the temperature conversion
|
||||
double dCountPerDegreeC; // How many counters per degree C
|
||||
int iTemperatureLSB; // The LSB of the temperature
|
||||
double dTemperature; // double version of the temperature
|
||||
short nCRC; // Result of the CRC check
|
||||
// Re-throw the exception
|
||||
throw;
|
||||
}
|
||||
|
||||
// Select and access the ID of the device we want to talk to
|
||||
owAdapter.Select(_deviceID);
|
||||
// Sleep while the data is transfered
|
||||
System.Threading.Thread.Sleep(1000);
|
||||
|
||||
// Setup for for power delivery after the next byte
|
||||
nResult = owAdapter.SetLevel(TMEX.TMOneWireLevelOperation.Write, TMEX.TMOneWireLevelMode.StrongPullup, TMEX.TMOneWireLevelPrime.AfterNextByte);
|
||||
// Stop the strong pullup
|
||||
owAdapter.SetLevel(TMEX.LevelOperation.Write, TMEX.LevelMode.Normal, TMEX.LevelPrime.Immediate);
|
||||
|
||||
try
|
||||
{
|
||||
// Send the byte and start strong pullup
|
||||
owAdapter.SendByte(0x44);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Stop the strong pullup
|
||||
owAdapter.SetLevel(TMEX.TMOneWireLevelOperation.Write, TMEX.TMOneWireLevelMode.Normal, TMEX.TMOneWireLevelPrime.Immediate);
|
||||
// Access the device we want to talk to
|
||||
owAdapter.Access();
|
||||
|
||||
// Re-throw the exception
|
||||
throw;
|
||||
}
|
||||
// Data buffer to send over the network
|
||||
var data = new byte[30];
|
||||
|
||||
// Sleep while the data is transfered
|
||||
System.Threading.Thread.Sleep(1000);
|
||||
// How many bytes of data to send
|
||||
short dataCount = 0;
|
||||
|
||||
// Stop the strong pullup
|
||||
nResult = owAdapter.SetLevel(TMEX.TMOneWireLevelOperation.Write, TMEX.TMOneWireLevelMode.Normal, TMEX.TMOneWireLevelPrime.Immediate);
|
||||
// Set the command to get the temperature from the scatchpad
|
||||
data[dataCount++] = 0xBE;
|
||||
|
||||
// Access the device we want to talk to
|
||||
owAdapter.Access();
|
||||
// Setup the rest of the bytes that we want
|
||||
for (var i = 0; i < 9; i++)
|
||||
data[dataCount++] = 0xFF;
|
||||
|
||||
// Set the command to get the temperature from the scatchpad
|
||||
aData[nDataCount++] = 0xBE;
|
||||
// Send the data block and get data back
|
||||
owAdapter.SendBlock(data, dataCount);
|
||||
|
||||
// Setup the rest of the bytes that we want
|
||||
for (int i = 0; i < 9; i++)
|
||||
aData[nDataCount++] = 0xFF;
|
||||
|
||||
// Send the data block and get data back
|
||||
nResult = owAdapter.SendBlock(aData, nDataCount);
|
||||
// Calculate the CRC of the first eight bytes of data
|
||||
var crc = owCRC8.Calculate(data, 1, 8);
|
||||
|
||||
// Calculate the CRC of the first eight bytes of data
|
||||
nCRC = owCRC8.Calculate(aData, 1, 8);
|
||||
// Check to see if our CRC matches the CRC supplied
|
||||
if (crc != data[9])
|
||||
{
|
||||
// Throw a CRC exception
|
||||
throw new owException(owException.ExceptionFunction.Crc, DeviceId);
|
||||
}
|
||||
|
||||
// Check to see if our CRC matches the CRC supplied
|
||||
if (nCRC != aData[9])
|
||||
{
|
||||
// Throw a CRC exception
|
||||
throw new owException(owException.owExceptionFunction.CRC, _deviceID);
|
||||
}
|
||||
// Get the LSB of the temperature data and divide it by two
|
||||
var temperatureLsb = data[1] / 2;
|
||||
|
||||
// Get the LSB of the temperature data and divide it by two
|
||||
iTemperatureLSB = aData[1] / 2;
|
||||
// If the data is negative then flip the bits
|
||||
if ((data[2] & 0x01) == 0x01) temperatureLsb |= -128;
|
||||
|
||||
// If the data is negative then flip the bits
|
||||
if ((aData[2] & 0x01) == 0x01) iTemperatureLSB |= -128;
|
||||
// Convert the temperature into a double
|
||||
var temperature = (double) temperatureLsb;
|
||||
|
||||
// Convert the temperature into a double
|
||||
dTemperature = (double) iTemperatureLSB;
|
||||
// Get the number of counts remaining
|
||||
double countRemaining = data[7];
|
||||
|
||||
// Get the number of counts remaining
|
||||
dCountRemaining = aData[7];
|
||||
// Get the number of counts per degree C
|
||||
double countPerDegreeC = data[8];
|
||||
|
||||
// Get the number of counts per degree C
|
||||
dCountPerDegreeC = aData[8];
|
||||
// Use the "counts remaining" data to calculate the temperaure to greater accuracy
|
||||
temperature = temperature - 0.25F + (countPerDegreeC - countRemaining) / countPerDegreeC;
|
||||
|
||||
// Use the "counts remaining" data to calculate the temperaure to greater accuracy
|
||||
dTemperature = dTemperature - 0.25F + (dCountPerDegreeC - dCountRemaining) / dCountPerDegreeC;
|
||||
|
||||
// Return the temperature
|
||||
return dTemperature;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
// Return the temperature
|
||||
return temperature;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user