mirror of
https://github.com/ckaczor/OneWireAPI.git
synced 2026-01-18 01:25:40 -05:00
Code modernization
This commit is contained in:
@@ -1,277 +1,262 @@
|
||||
using System;
|
||||
|
||||
namespace OneWireAPI
|
||||
{
|
||||
public class owDeviceFamily26 : owDevice
|
||||
{
|
||||
#region Constructor
|
||||
public class owDeviceFamily26 : owDevice
|
||||
{
|
||||
public owDeviceFamily26(owSession session, short[] id)
|
||||
: base(session, id)
|
||||
{
|
||||
// Just call the base constructor
|
||||
}
|
||||
|
||||
public owDeviceFamily26(owSession Session, short[] ID) : base(Session, ID)
|
||||
{
|
||||
// Just call the base constructor
|
||||
}
|
||||
public enum VoltageType : short
|
||||
{
|
||||
Supply,
|
||||
Output
|
||||
}
|
||||
|
||||
#endregion
|
||||
private double GetVoltage(VoltageType type)
|
||||
{
|
||||
short busy;
|
||||
|
||||
#region Methods
|
||||
// Select and access the ID of the device we want to talk to
|
||||
owAdapter.Select(DeviceId);
|
||||
|
||||
public enum VoltageType : short
|
||||
{
|
||||
Supply,
|
||||
Output
|
||||
}
|
||||
// Data buffer to send over the network
|
||||
var data = new byte[30];
|
||||
|
||||
private double GetVoltage(VoltageType Type)
|
||||
{
|
||||
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
|
||||
short nCRC; // Result of the CRC check
|
||||
int iIndex;
|
||||
short nBusy;
|
||||
double dVoltage;
|
||||
// How many bytes of data to send
|
||||
short dataCount = 0;
|
||||
|
||||
// Select and access the ID of the device we want to talk to
|
||||
owAdapter.Select(_deviceID);
|
||||
|
||||
// Set the command to recall the status/configuration page to the scratchpad
|
||||
aData[nDataCount++] = 0xB8;
|
||||
// Set the command to recall the status/configuration page to the scratchpad
|
||||
data[dataCount++] = 0xB8;
|
||||
|
||||
// Set the page number to recall
|
||||
aData[nDataCount++] = 0x00;
|
||||
// Set the page number to recall
|
||||
data[dataCount++] = 0x00;
|
||||
|
||||
// Send the data block
|
||||
nResult = owAdapter.SendBlock(aData, nDataCount);
|
||||
// Send the data block
|
||||
owAdapter.SendBlock(data, dataCount);
|
||||
|
||||
// Clear the data count
|
||||
nDataCount = 0;
|
||||
// Clear the data count
|
||||
dataCount = 0;
|
||||
|
||||
// Access the device we want to talk to
|
||||
owAdapter.Access();
|
||||
// Access the device we want to talk to
|
||||
owAdapter.Access();
|
||||
|
||||
// Set the command to read the scratchpad
|
||||
aData[nDataCount++] = 0xBE;
|
||||
// Set the command to read the scratchpad
|
||||
data[dataCount++] = 0xBE;
|
||||
|
||||
// Set the page number to read
|
||||
aData[nDataCount++] = 0x00;
|
||||
// Set the page number to read
|
||||
data[dataCount++] = 0x00;
|
||||
|
||||
// Add 9 bytes to be read - 8 for the data and 1 for the CRC
|
||||
for (iIndex = 0; iIndex < 9; iIndex++)
|
||||
aData[nDataCount++] = 0xFF;
|
||||
// Add 9 bytes to be read - 8 for the data and 1 for the CRC
|
||||
for (var index = 0; index < 9; index++)
|
||||
data[dataCount++] = 0xFF;
|
||||
|
||||
// Send the data block
|
||||
nResult = owAdapter.SendBlock(aData, nDataCount);
|
||||
// Send the data block
|
||||
owAdapter.SendBlock(data, dataCount);
|
||||
|
||||
// Calculate the CRC of the scratchpad data
|
||||
nCRC = owCRC8.Calculate(aData, 2, 9);
|
||||
// Calculate the CRC of the scratchpad data
|
||||
var crc = owCRC8.Calculate(data, 2, 9);
|
||||
|
||||
// If the CRC doesn't match then throw an exception
|
||||
if (nCRC != aData[10])
|
||||
{
|
||||
// Throw a CRC exception
|
||||
throw new owException(owException.owExceptionFunction.CRC, _deviceID);
|
||||
}
|
||||
// If the CRC doesn't match then throw an exception
|
||||
if (crc != data[10])
|
||||
{
|
||||
// Throw a CRC exception
|
||||
throw new owException(owException.ExceptionFunction.Crc, DeviceId);
|
||||
}
|
||||
|
||||
// TODO - Check if we really need to change the input selector
|
||||
if (true)
|
||||
{
|
||||
// Access the device we want to talk to
|
||||
owAdapter.Access();
|
||||
// TODO - Check if we really need to change the input selector
|
||||
if (true)
|
||||
{
|
||||
// Access the device we want to talk to
|
||||
owAdapter.Access();
|
||||
|
||||
// Reset the data count
|
||||
nDataCount = 0;
|
||||
// Reset the data count
|
||||
dataCount = 0;
|
||||
|
||||
// Set the command to write the scratchpad
|
||||
aData[nDataCount++] = 0x4E;
|
||||
// Set the command to write the scratchpad
|
||||
data[dataCount++] = 0x4E;
|
||||
|
||||
// Set the page number to write
|
||||
aData[nDataCount++] = 0x00;
|
||||
// Set the page number to write
|
||||
data[dataCount++] = 0x00;
|
||||
|
||||
// Set or clear the AD bit based on the type requested
|
||||
if (Type == VoltageType.Supply)
|
||||
aData[nDataCount++] = (byte) (aData[2] | 0x08);
|
||||
else
|
||||
aData[nDataCount++] = (byte) (aData[2] & 0xF7);
|
||||
// Set or clear the AD bit based on the type requested
|
||||
if (type == VoltageType.Supply)
|
||||
data[dataCount++] = (byte) (data[2] | 0x08);
|
||||
else
|
||||
data[dataCount++] = (byte) (data[2] & 0xF7);
|
||||
|
||||
// Move the existing data down in the array
|
||||
for (iIndex = 0; iIndex < 7; iIndex++)
|
||||
aData[nDataCount++] = aData[iIndex + 4];
|
||||
// Move the existing data down in the array
|
||||
for (var index = 0; index < 7; index++)
|
||||
data[dataCount++] = data[index + 4];
|
||||
|
||||
// Send the data block
|
||||
nResult = owAdapter.SendBlock(aData, nDataCount);
|
||||
// Send the data block
|
||||
owAdapter.SendBlock(data, dataCount);
|
||||
|
||||
// Reset the data count
|
||||
nDataCount = 0;
|
||||
|
||||
// Access the device we want to talk to
|
||||
owAdapter.Access();
|
||||
|
||||
// Set the command to copy the scratchpad
|
||||
aData[nDataCount++] = 0x48;
|
||||
|
||||
// Set the page number to copy to
|
||||
aData[nDataCount++] = 0x00;
|
||||
|
||||
// Send the data block
|
||||
nResult = owAdapter.SendBlock(aData, nDataCount);
|
||||
|
||||
// Loop until the data copy is complete
|
||||
do
|
||||
{
|
||||
nBusy = owAdapter.ReadByte();
|
||||
}
|
||||
while (nBusy == 0);
|
||||
}
|
||||
// Reset the data count
|
||||
dataCount = 0;
|
||||
|
||||
// Access the device we want to talk to
|
||||
owAdapter.Access();
|
||||
// Access the device we want to talk to
|
||||
owAdapter.Access();
|
||||
|
||||
// Send the voltage conversion command
|
||||
owAdapter.SendByte(0xB4);
|
||||
// Set the command to copy the scratchpad
|
||||
data[dataCount++] = 0x48;
|
||||
|
||||
// Loop until conversion is complete
|
||||
do
|
||||
{
|
||||
nBusy = owAdapter.ReadByte();
|
||||
}
|
||||
while (nBusy == 0);
|
||||
// Set the page number to copy to
|
||||
data[dataCount++] = 0x00;
|
||||
|
||||
// Clear the data count
|
||||
nDataCount = 0;
|
||||
// Send the data block
|
||||
owAdapter.SendBlock(data, dataCount);
|
||||
|
||||
// Set the command to recall the status/configuration page to the scratchpad
|
||||
aData[nDataCount++] = 0xB8;
|
||||
// Loop until the data copy is complete
|
||||
do
|
||||
{
|
||||
busy = owAdapter.ReadByte();
|
||||
}
|
||||
while (busy == 0);
|
||||
}
|
||||
|
||||
// Set the page number to recall
|
||||
aData[nDataCount++] = 0x00;
|
||||
// Access the device we want to talk to
|
||||
owAdapter.Access();
|
||||
|
||||
// Access the device we want to talk to
|
||||
owAdapter.Access();
|
||||
// Send the voltage conversion command
|
||||
owAdapter.SendByte(0xB4);
|
||||
|
||||
// Send the data block
|
||||
nResult = owAdapter.SendBlock(aData, nDataCount);
|
||||
// Loop until conversion is complete
|
||||
do
|
||||
{
|
||||
busy = owAdapter.ReadByte();
|
||||
}
|
||||
while (busy == 0);
|
||||
|
||||
// Clear the data count
|
||||
nDataCount = 0;
|
||||
// Clear the data count
|
||||
dataCount = 0;
|
||||
|
||||
// Access the device we want to talk to
|
||||
owAdapter.Access();
|
||||
// Set the command to recall the status/configuration page to the scratchpad
|
||||
data[dataCount++] = 0xB8;
|
||||
|
||||
// Set the command to read the scratchpad
|
||||
aData[nDataCount++] = 0xBE;
|
||||
// Set the page number to recall
|
||||
data[dataCount++] = 0x00;
|
||||
|
||||
// Set the page number to read
|
||||
aData[nDataCount++] = 0x00;
|
||||
// Access the device we want to talk to
|
||||
owAdapter.Access();
|
||||
|
||||
// Add 9 bytes to be read - 8 for the data and 1 for the CRC
|
||||
for (iIndex = 0; iIndex < 9; iIndex++)
|
||||
aData[nDataCount++] = 0xFF;
|
||||
// Send the data block
|
||||
owAdapter.SendBlock(data, dataCount);
|
||||
|
||||
// Send the data block
|
||||
nResult = owAdapter.SendBlock(aData, nDataCount);
|
||||
// Clear the data count
|
||||
dataCount = 0;
|
||||
|
||||
// Calculate the CRC of the scratchpad data
|
||||
nCRC = owCRC8.Calculate(aData, 2, 9);
|
||||
// Access the device we want to talk to
|
||||
owAdapter.Access();
|
||||
|
||||
// If the CRC doesn't match then throw an exception
|
||||
if (nCRC != aData[10])
|
||||
{
|
||||
// Throw a CRC exception
|
||||
throw new owException(owException.owExceptionFunction.CRC, _deviceID);
|
||||
}
|
||||
|
||||
// Assemble the voltage data
|
||||
dVoltage = (double) ((aData[6] << 8) | aData[5]);
|
||||
|
||||
return dVoltage / 100;
|
||||
}
|
||||
// Set the command to read the scratchpad
|
||||
data[dataCount++] = 0xBE;
|
||||
|
||||
public double GetSupplyVoltage()
|
||||
{
|
||||
return GetVoltage(VoltageType.Supply);
|
||||
}
|
||||
// Set the page number to read
|
||||
data[dataCount++] = 0x00;
|
||||
|
||||
public double GetOutputVoltage()
|
||||
{
|
||||
// Add 9 bytes to be read - 8 for the data and 1 for the CRC
|
||||
for (var index = 0; index < 9; index++)
|
||||
data[dataCount++] = 0xFF;
|
||||
|
||||
return GetVoltage(VoltageType.Output);
|
||||
}
|
||||
// Send the data block
|
||||
owAdapter.SendBlock(data, dataCount);
|
||||
|
||||
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
|
||||
short nCRC; // Result of the CRC check
|
||||
int iTemperatureLSB; // The LSB of the temperature data
|
||||
int iTemperatureMSB; // The MSB of the temperature data
|
||||
int iTemperature; // Complete temperature data
|
||||
double dTemperature; // double version of the temperature
|
||||
// Calculate the CRC of the scratchpad data
|
||||
crc = owCRC8.Calculate(data, 2, 9);
|
||||
|
||||
// Select and access the ID of the device we want to talk to
|
||||
owAdapter.Select(_deviceID);
|
||||
// If the CRC doesn't match then throw an exception
|
||||
if (crc != data[10])
|
||||
{
|
||||
// Throw a CRC exception
|
||||
throw new owException(owException.ExceptionFunction.Crc, DeviceId);
|
||||
}
|
||||
|
||||
// Send the conversion command byte
|
||||
owAdapter.SendByte(0x44);
|
||||
// Assemble the voltage data
|
||||
var dVoltage = (double) ((data[6] << 8) | data[5]);
|
||||
|
||||
// Sleep while the data is converted
|
||||
System.Threading.Thread.Sleep(10);
|
||||
return dVoltage / 100;
|
||||
}
|
||||
|
||||
// Access the device we want to talk to
|
||||
owAdapter.Access();
|
||||
public double GetSupplyVoltage()
|
||||
{
|
||||
return GetVoltage(VoltageType.Supply);
|
||||
}
|
||||
|
||||
// Set the command to recall the status/configuration page to the scratchpad
|
||||
aData[nDataCount++] = 0xB8;
|
||||
public double GetOutputVoltage()
|
||||
{
|
||||
|
||||
// Set the page number to recall
|
||||
aData[nDataCount++] = 0x00;
|
||||
return GetVoltage(VoltageType.Output);
|
||||
}
|
||||
|
||||
// Send the data block
|
||||
nResult = owAdapter.SendBlock(aData, nDataCount);
|
||||
public double GetTemperature()
|
||||
{
|
||||
// Select and access the ID of the device we want to talk to
|
||||
owAdapter.Select(DeviceId);
|
||||
|
||||
// Clear the data count
|
||||
nDataCount = 0;
|
||||
// Send the conversion command byte
|
||||
owAdapter.SendByte(0x44);
|
||||
|
||||
// Access the device we want to talk to
|
||||
owAdapter.Access();
|
||||
// Sleep while the data is converted
|
||||
System.Threading.Thread.Sleep(10);
|
||||
|
||||
// Set the command to read the scratchpad
|
||||
aData[nDataCount++] = 0xBE;
|
||||
// Access the device we want to talk to
|
||||
owAdapter.Access();
|
||||
|
||||
// Set the page number to read
|
||||
aData[nDataCount++] = 0x00;
|
||||
// Data buffer to send over the network
|
||||
var data = new byte[30];
|
||||
|
||||
// Add 9 bytes to be read - 8 for the data and 1 for the CRC
|
||||
for (int iIndex = 0; iIndex < 9; iIndex++)
|
||||
aData[nDataCount++] = 0xFF;
|
||||
// How many bytes of data to send
|
||||
short dataCount = 0;
|
||||
|
||||
// Send the data block
|
||||
nResult = owAdapter.SendBlock(aData, nDataCount);
|
||||
// Set the command to recall the status/configuration page to the scratchpad
|
||||
data[dataCount++] = 0xB8;
|
||||
|
||||
// Calculate the CRC of the scratchpad data
|
||||
nCRC = owCRC8.Calculate(aData, 2, 9);
|
||||
// Set the page number to recall
|
||||
data[dataCount++] = 0x00;
|
||||
|
||||
// If the CRC doesn't match then throw an exception
|
||||
if (nCRC != aData[10])
|
||||
{
|
||||
// Throw a CRC exception
|
||||
throw new owException(owException.owExceptionFunction.CRC, _deviceID);
|
||||
}
|
||||
// Send the data block
|
||||
owAdapter.SendBlock(data, dataCount);
|
||||
|
||||
// Get the two bytes of temperature data
|
||||
iTemperatureLSB = aData[3];
|
||||
iTemperatureMSB = aData[4];
|
||||
// Clear the data count
|
||||
dataCount = 0;
|
||||
|
||||
// Shift the data into the right order
|
||||
iTemperature = ((iTemperatureMSB << 8) | iTemperatureLSB) >> 3;
|
||||
// Access the device we want to talk to
|
||||
owAdapter.Access();
|
||||
|
||||
// Figure out the temperature
|
||||
dTemperature = iTemperature * 0.03125F;
|
||||
// Set the command to read the scratchpad
|
||||
data[dataCount++] = 0xBE;
|
||||
|
||||
// Return the temperature
|
||||
return dTemperature;
|
||||
}
|
||||
// Set the page number to read
|
||||
data[dataCount++] = 0x00;
|
||||
|
||||
#endregion
|
||||
}
|
||||
// Add 9 bytes to be read - 8 for the data and 1 for the CRC
|
||||
for (var iIndex = 0; iIndex < 9; iIndex++)
|
||||
data[dataCount++] = 0xFF;
|
||||
|
||||
// Send the data block
|
||||
owAdapter.SendBlock(data, dataCount);
|
||||
|
||||
// Calculate the CRC of the scratchpad data
|
||||
var crc = owCRC8.Calculate(data, 2, 9);
|
||||
|
||||
// If the CRC doesn't match then throw an exception
|
||||
if (crc != data[10])
|
||||
{
|
||||
// Throw a CRC exception
|
||||
throw new owException(owException.ExceptionFunction.Crc, DeviceId);
|
||||
}
|
||||
|
||||
// Get the two bytes of temperature data
|
||||
int temperatureLsb = data[3];
|
||||
int temperatureMsb = data[4];
|
||||
|
||||
// Shift the data into the right order
|
||||
var iTemperature = ((temperatureMsb << 8) | temperatureLsb) >> 3;
|
||||
|
||||
// Return the temperature
|
||||
return iTemperature * 0.03125F;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user