mirror of
https://github.com/ckaczor/OneWireAPI.git
synced 2026-02-09 17:52:40 -05:00
More cleanup
This commit is contained in:
18
Crc16.cs
18
Crc16.cs
@@ -4,30 +4,30 @@ namespace OneWireAPI
|
|||||||
{
|
{
|
||||||
private static readonly short[] OddParity = { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 };
|
private static readonly short[] OddParity = { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 };
|
||||||
|
|
||||||
public static int Calculate(byte[] nData, int iStart, int iEnd)
|
public static int Calculate(byte[] data, int start, int end)
|
||||||
{
|
{
|
||||||
return Calculate(nData, iStart, iEnd, 0);
|
return Calculate(data, start, end, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int Calculate(byte nData, int iInitialValue)
|
public static int Calculate(byte data, int initialValue)
|
||||||
{
|
{
|
||||||
var aData = new byte[1];
|
var bytes = new byte[1];
|
||||||
|
|
||||||
aData[0] = nData;
|
bytes[0] = data;
|
||||||
|
|
||||||
return Calculate(aData, 0, 0, iInitialValue);
|
return Calculate(bytes, 0, 0, initialValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int Calculate(byte[] nData, int iStart, int iEnd, int iInitialValue)
|
public static int Calculate(byte[] data, int start, int end, int iInitialValue)
|
||||||
{
|
{
|
||||||
int index; // Loop index
|
int index; // Loop index
|
||||||
var currentCrc = iInitialValue; // Current CRC accumulator
|
var currentCrc = iInitialValue; // Current CRC accumulator
|
||||||
|
|
||||||
// Loop over all bytes in the input array
|
// Loop over all bytes in the input array
|
||||||
for (index = iStart; index <= iEnd; index++)
|
for (index = start; index <= end; index++)
|
||||||
{
|
{
|
||||||
// Get the current element of data
|
// Get the current element of data
|
||||||
int iBuffer = nData[index];
|
int iBuffer = data[index];
|
||||||
|
|
||||||
// Calculate the current CRC for this position
|
// Calculate the current CRC for this position
|
||||||
iBuffer = (iBuffer ^ (currentCrc & 0xFF)) & 0xFF;
|
iBuffer = (iBuffer ^ (currentCrc & 0xFF)) & 0xFF;
|
||||||
|
|||||||
12
Device.cs
12
Device.cs
@@ -3,7 +3,8 @@ namespace OneWireAPI
|
|||||||
public class Device
|
public class Device
|
||||||
{
|
{
|
||||||
protected Session Session;
|
protected Session Session;
|
||||||
protected Identifier DeviceId;
|
|
||||||
|
public Identifier Id { get; protected set; }
|
||||||
|
|
||||||
public Device(Session session, short[] rawId)
|
public Device(Session session, short[] rawId)
|
||||||
{
|
{
|
||||||
@@ -11,17 +12,12 @@ namespace OneWireAPI
|
|||||||
Session = session;
|
Session = session;
|
||||||
|
|
||||||
// Create a new identifier and give it the ID supplied
|
// Create a new identifier and give it the ID supplied
|
||||||
DeviceId = new Identifier(rawId);
|
Id = new Identifier(rawId);
|
||||||
}
|
|
||||||
|
|
||||||
public Identifier Id
|
|
||||||
{
|
|
||||||
get { return DeviceId; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Family
|
public int Family
|
||||||
{
|
{
|
||||||
get { return DeviceId.Family; }
|
get { return Id.Family; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ namespace OneWireAPI
|
|||||||
public double GetTemperature()
|
public double GetTemperature()
|
||||||
{
|
{
|
||||||
// Select and access the ID of the device we want to talk to
|
// Select and access the ID of the device we want to talk to
|
||||||
Adapter.Select(DeviceId);
|
Adapter.Select(Id);
|
||||||
|
|
||||||
// Setup for for power delivery after the next byte
|
// Setup for for power delivery after the next byte
|
||||||
Adapter.SetLevel(TMEX.LevelOperation.Write, TMEX.LevelMode.StrongPullup, TMEX.LevelPrime.AfterNextByte);
|
Adapter.SetLevel(TMEX.LevelOperation.Write, TMEX.LevelMode.StrongPullup, TMEX.LevelPrime.AfterNextByte);
|
||||||
@@ -62,7 +62,7 @@ namespace OneWireAPI
|
|||||||
if (crc != data[9])
|
if (crc != data[9])
|
||||||
{
|
{
|
||||||
// Throw a CRC exception
|
// Throw a CRC exception
|
||||||
throw new OneWireException(OneWireException.ExceptionFunction.Crc, DeviceId);
|
throw new OneWireException(OneWireException.ExceptionFunction.Crc, Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the LSB of the temperature data and divide it by two
|
// Get the LSB of the temperature data and divide it by two
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ namespace OneWireAPI
|
|||||||
public byte[] ReadDevice()
|
public byte[] ReadDevice()
|
||||||
{
|
{
|
||||||
// Select and access the ID of the device we want to talk to
|
// Select and access the ID of the device we want to talk to
|
||||||
Adapter.Select(DeviceId);
|
Adapter.Select(Id);
|
||||||
|
|
||||||
// Data buffer to send over the network
|
// Data buffer to send over the network
|
||||||
var data = new byte[30];
|
var data = new byte[30];
|
||||||
@@ -88,7 +88,7 @@ namespace OneWireAPI
|
|||||||
if (crcResult != matchCrc)
|
if (crcResult != matchCrc)
|
||||||
{
|
{
|
||||||
// Throw a CRC exception
|
// Throw a CRC exception
|
||||||
throw new OneWireException(OneWireException.ExceptionFunction.Crc, DeviceId);
|
throw new OneWireException(OneWireException.ExceptionFunction.Crc, Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
var state = new byte[2];
|
var state = new byte[2];
|
||||||
@@ -111,7 +111,7 @@ namespace OneWireAPI
|
|||||||
data[dataCount++] = 0xFF;
|
data[dataCount++] = 0xFF;
|
||||||
|
|
||||||
// Select and access the ID of the device we want to talk to
|
// Select and access the ID of the device we want to talk to
|
||||||
Adapter.Select(DeviceId);
|
Adapter.Select(Id);
|
||||||
|
|
||||||
// Send the data
|
// Send the data
|
||||||
Adapter.SendBlock(data, dataCount);
|
Adapter.SendBlock(data, dataCount);
|
||||||
@@ -128,7 +128,7 @@ namespace OneWireAPI
|
|||||||
if (crcResult != matchCrc)
|
if (crcResult != matchCrc)
|
||||||
{
|
{
|
||||||
// Throw a CRC exception
|
// Throw a CRC exception
|
||||||
throw new OneWireException(OneWireException.ExceptionFunction.Crc, DeviceId);
|
throw new OneWireException(OneWireException.ExceptionFunction.Crc, Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store the state data
|
// Store the state data
|
||||||
@@ -140,7 +140,7 @@ namespace OneWireAPI
|
|||||||
public void WriteDevice(byte[] state)
|
public void WriteDevice(byte[] state)
|
||||||
{
|
{
|
||||||
// Select and access the ID of the device we want to talk to
|
// Select and access the ID of the device we want to talk to
|
||||||
Adapter.Select(DeviceId);
|
Adapter.Select(Id);
|
||||||
|
|
||||||
// Data buffer to send over the network
|
// Data buffer to send over the network
|
||||||
var data = new byte[30];
|
var data = new byte[30];
|
||||||
@@ -177,7 +177,7 @@ namespace OneWireAPI
|
|||||||
if (crcResult != matchCrc)
|
if (crcResult != matchCrc)
|
||||||
{
|
{
|
||||||
// Throw a CRC exception
|
// Throw a CRC exception
|
||||||
throw new OneWireException(OneWireException.ExceptionFunction.Crc, DeviceId);
|
throw new OneWireException(OneWireException.ExceptionFunction.Crc, Id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ namespace OneWireAPI
|
|||||||
public uint GetCounter(int counterPage)
|
public uint GetCounter(int counterPage)
|
||||||
{
|
{
|
||||||
// Select and access the ID of the device we want to talk to
|
// Select and access the ID of the device we want to talk to
|
||||||
Adapter.Select(DeviceId);
|
Adapter.Select(Id);
|
||||||
|
|
||||||
// Data buffer to send over the network
|
// Data buffer to send over the network
|
||||||
var data = new byte[30];
|
var data = new byte[30];
|
||||||
@@ -49,7 +49,7 @@ namespace OneWireAPI
|
|||||||
if (crcResult != matchCrc)
|
if (crcResult != matchCrc)
|
||||||
{
|
{
|
||||||
// Throw a CRC exception
|
// Throw a CRC exception
|
||||||
throw new OneWireException(OneWireException.ExceptionFunction.Crc, DeviceId);
|
throw new OneWireException(OneWireException.ExceptionFunction.Crc, Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint counter = 0;
|
uint counter = 0;
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ namespace OneWireAPI
|
|||||||
data[dataCount++] = (startAddress >> 8) & 0xFF;
|
data[dataCount++] = (startAddress >> 8) & 0xFF;
|
||||||
|
|
||||||
// Select and access the ID of the device we want to talk to
|
// Select and access the ID of the device we want to talk to
|
||||||
Adapter.Select(DeviceId);
|
Adapter.Select(Id);
|
||||||
|
|
||||||
// Write to the data pages specified
|
// Write to the data pages specified
|
||||||
for (var index = startAddress; index <= endAddress; index++)
|
for (var index = startAddress; index <= endAddress; index++)
|
||||||
@@ -75,7 +75,7 @@ namespace OneWireAPI
|
|||||||
if (data[dataCount - 1] != _control[index - startAddress])
|
if (data[dataCount - 1] != _control[index - startAddress])
|
||||||
{
|
{
|
||||||
// Throw an exception
|
// Throw an exception
|
||||||
throw new OneWireException(OneWireException.ExceptionFunction.SendBlock, DeviceId);
|
throw new OneWireException(OneWireException.ExceptionFunction.SendBlock, Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
int calculatedCrc; // CRC we calculated from sent data
|
int calculatedCrc; // CRC we calculated from sent data
|
||||||
@@ -107,7 +107,7 @@ namespace OneWireAPI
|
|||||||
if (calculatedCrc != sentCrc)
|
if (calculatedCrc != sentCrc)
|
||||||
{
|
{
|
||||||
// Throw a CRC exception
|
// Throw a CRC exception
|
||||||
throw new OneWireException(OneWireException.ExceptionFunction.Crc, DeviceId);
|
throw new OneWireException(OneWireException.ExceptionFunction.Crc, Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset the byte count
|
// Reset the byte count
|
||||||
@@ -118,7 +118,7 @@ namespace OneWireAPI
|
|||||||
public double[] GetVoltages()
|
public double[] GetVoltages()
|
||||||
{
|
{
|
||||||
// Select and access the ID of the device we want to talk to
|
// Select and access the ID of the device we want to talk to
|
||||||
Adapter.Select(DeviceId);
|
Adapter.Select(Id);
|
||||||
|
|
||||||
// Data buffer to send over the network
|
// Data buffer to send over the network
|
||||||
var data = new byte[30];
|
var data = new byte[30];
|
||||||
@@ -154,18 +154,18 @@ namespace OneWireAPI
|
|||||||
if (calculatedCrc != sentCrc)
|
if (calculatedCrc != sentCrc)
|
||||||
{
|
{
|
||||||
// Throw a CRC exception
|
// Throw a CRC exception
|
||||||
throw new OneWireException(OneWireException.ExceptionFunction.Crc, DeviceId);
|
throw new OneWireException(OneWireException.ExceptionFunction.Crc, Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup for for power delivery after the next byte
|
// Setup for for power delivery after the next byte
|
||||||
Adapter.SetLevel(TMEX.LevelOperation.Write, TMEX.LevelMode.StrongPullup, TMEX.LevelPrime.AfterNextByte);
|
Adapter.SetLevel(TMEX.LevelOperation.Write, TMEX.LevelMode.StrongPullup, TMEX.LevelPrime.AfterNextByte);
|
||||||
|
|
||||||
var nTransmitByte = (short) ((dataCount - 1) & 0x1F);
|
var transmitByte = (short) ((dataCount - 1) & 0x1F);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Send the byte and start strong pullup
|
// Send the byte and start strong pullup
|
||||||
Adapter.SendByte(nTransmitByte);
|
Adapter.SendByte(transmitByte);
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
@@ -186,7 +186,7 @@ namespace OneWireAPI
|
|||||||
Adapter.ReadByte();
|
Adapter.ReadByte();
|
||||||
|
|
||||||
// Select and access the ID of the device we want to talk to
|
// Select and access the ID of the device we want to talk to
|
||||||
Adapter.Select(DeviceId);
|
Adapter.Select(Id);
|
||||||
|
|
||||||
// Reinitialize the data count
|
// Reinitialize the data count
|
||||||
dataCount = 0;
|
dataCount = 0;
|
||||||
@@ -217,7 +217,7 @@ namespace OneWireAPI
|
|||||||
if (calculatedCrc != sentCrc)
|
if (calculatedCrc != sentCrc)
|
||||||
{
|
{
|
||||||
// Throw a CRC exception
|
// Throw a CRC exception
|
||||||
throw new OneWireException(OneWireException.ExceptionFunction.Crc, DeviceId);
|
throw new OneWireException(OneWireException.ExceptionFunction.Crc, Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Voltage values to return
|
// Voltage values to return
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ namespace OneWireAPI
|
|||||||
short busy;
|
short busy;
|
||||||
|
|
||||||
// Select and access the ID of the device we want to talk to
|
// Select and access the ID of the device we want to talk to
|
||||||
Adapter.Select(DeviceId);
|
Adapter.Select(Id);
|
||||||
|
|
||||||
// Data buffer to send over the network
|
// Data buffer to send over the network
|
||||||
var data = new byte[30];
|
var data = new byte[30];
|
||||||
@@ -62,7 +62,7 @@ namespace OneWireAPI
|
|||||||
if (crc != data[10])
|
if (crc != data[10])
|
||||||
{
|
{
|
||||||
// Throw a CRC exception
|
// Throw a CRC exception
|
||||||
throw new OneWireException(OneWireException.ExceptionFunction.Crc, DeviceId);
|
throw new OneWireException(OneWireException.ExceptionFunction.Crc, Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO - Check if we really need to change the input selector
|
// TODO - Check if we really need to change the input selector
|
||||||
@@ -170,7 +170,7 @@ namespace OneWireAPI
|
|||||||
if (crc != data[10])
|
if (crc != data[10])
|
||||||
{
|
{
|
||||||
// Throw a CRC exception
|
// Throw a CRC exception
|
||||||
throw new OneWireException(OneWireException.ExceptionFunction.Crc, DeviceId);
|
throw new OneWireException(OneWireException.ExceptionFunction.Crc, Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assemble the voltage data
|
// Assemble the voltage data
|
||||||
@@ -193,7 +193,7 @@ namespace OneWireAPI
|
|||||||
public double GetTemperature()
|
public double GetTemperature()
|
||||||
{
|
{
|
||||||
// Select and access the ID of the device we want to talk to
|
// Select and access the ID of the device we want to talk to
|
||||||
Adapter.Select(DeviceId);
|
Adapter.Select(Id);
|
||||||
|
|
||||||
// Send the conversion command byte
|
// Send the conversion command byte
|
||||||
Adapter.SendByte(0x44);
|
Adapter.SendByte(0x44);
|
||||||
@@ -245,7 +245,7 @@ namespace OneWireAPI
|
|||||||
if (crc != data[10])
|
if (crc != data[10])
|
||||||
{
|
{
|
||||||
// Throw a CRC exception
|
// Throw a CRC exception
|
||||||
throw new OneWireException(OneWireException.ExceptionFunction.Crc, DeviceId);
|
throw new OneWireException(OneWireException.ExceptionFunction.Crc, Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the two bytes of temperature data
|
// Get the two bytes of temperature data
|
||||||
@@ -253,10 +253,10 @@ namespace OneWireAPI
|
|||||||
int temperatureMsb = data[4];
|
int temperatureMsb = data[4];
|
||||||
|
|
||||||
// Shift the data into the right order
|
// Shift the data into the right order
|
||||||
var iTemperature = ((temperatureMsb << 8) | temperatureLsb) >> 3;
|
var temperature = ((temperatureMsb << 8) | temperatureLsb) >> 3;
|
||||||
|
|
||||||
// Return the temperature
|
// Return the temperature
|
||||||
return iTemperature * 0.03125F;
|
return temperature * 0.03125F;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ namespace OneWireAPI
|
|||||||
public void SetBackLight(bool state)
|
public void SetBackLight(bool state)
|
||||||
{
|
{
|
||||||
// Select the device
|
// Select the device
|
||||||
Adapter.Select(DeviceId);
|
Adapter.Select(Id);
|
||||||
|
|
||||||
// Set the state of the backlight
|
// Set the state of the backlight
|
||||||
Adapter.SendByte((short) (state ? 0x8 : 0x7));
|
Adapter.SendByte((short) (state ? 0x8 : 0x7));
|
||||||
@@ -87,7 +87,7 @@ namespace OneWireAPI
|
|||||||
if (_width > 16)
|
if (_width > 16)
|
||||||
{
|
{
|
||||||
// Select the device
|
// Select the device
|
||||||
Adapter.Select(DeviceId);
|
Adapter.Select(Id);
|
||||||
|
|
||||||
// Set the data block to just the first 16 characters
|
// Set the data block to just the first 16 characters
|
||||||
sendData = text.Substring(0, 16);
|
sendData = text.Substring(0, 16);
|
||||||
@@ -109,7 +109,7 @@ namespace OneWireAPI
|
|||||||
Adapter.SendBlock(data, dataCount);
|
Adapter.SendBlock(data, dataCount);
|
||||||
|
|
||||||
// Select the device
|
// Select the device
|
||||||
Adapter.Select(DeviceId);
|
Adapter.Select(Id);
|
||||||
|
|
||||||
// Send the scratchpad data to the LCD
|
// Send the scratchpad data to the LCD
|
||||||
Adapter.SendByte(0x48);
|
Adapter.SendByte(0x48);
|
||||||
@@ -130,7 +130,7 @@ namespace OneWireAPI
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Select the device
|
// Select the device
|
||||||
Adapter.Select(DeviceId);
|
Adapter.Select(Id);
|
||||||
|
|
||||||
// Initialize the data array
|
// Initialize the data array
|
||||||
data = new byte[18];
|
data = new byte[18];
|
||||||
@@ -152,7 +152,7 @@ namespace OneWireAPI
|
|||||||
Adapter.SendBlock(data, dataCount);
|
Adapter.SendBlock(data, dataCount);
|
||||||
|
|
||||||
// Select the device
|
// Select the device
|
||||||
Adapter.Select(DeviceId);
|
Adapter.Select(Id);
|
||||||
|
|
||||||
// Send the scratchpad data to the LCD
|
// Send the scratchpad data to the LCD
|
||||||
Adapter.SendByte(0x48);
|
Adapter.SendByte(0x48);
|
||||||
@@ -164,7 +164,7 @@ namespace OneWireAPI
|
|||||||
public void Clear()
|
public void Clear()
|
||||||
{
|
{
|
||||||
// Select the device
|
// Select the device
|
||||||
Adapter.Select(DeviceId);
|
Adapter.Select(Id);
|
||||||
|
|
||||||
// Clear the display
|
// Clear the display
|
||||||
Adapter.SendByte(0x49);
|
Adapter.SendByte(0x49);
|
||||||
|
|||||||
52
Identifer.cs
52
Identifer.cs
@@ -1,80 +1,68 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace OneWireAPI
|
namespace OneWireAPI
|
||||||
{
|
{
|
||||||
public class Identifier
|
public class Identifier
|
||||||
{
|
{
|
||||||
private readonly short[] _rawId; // The raw ID array
|
public short[] RawId { get; private set; }
|
||||||
private readonly string _friendlyName; // Friendly display name
|
|
||||||
private readonly int _familyCode; // Family code
|
public string Name { get; private set; }
|
||||||
|
|
||||||
|
public int Family { get; private set; }
|
||||||
|
|
||||||
public Identifier()
|
public Identifier()
|
||||||
{
|
{
|
||||||
// Create a blank ID
|
// Create a blank ID
|
||||||
_rawId = new short[8];
|
RawId = new short[8];
|
||||||
}
|
}
|
||||||
|
|
||||||
public Identifier(byte[] deviceId)
|
public Identifier(IList<byte> deviceId)
|
||||||
{
|
{
|
||||||
// Create a blank ID
|
// Create a blank ID
|
||||||
_rawId = new short[8];
|
RawId = new short[8];
|
||||||
|
|
||||||
// Copy the byte array to the short array
|
// Copy the byte array to the short array
|
||||||
for (var i = 0; i < deviceId.Length; i++)
|
for (var index = 0; index < deviceId.Count; index++)
|
||||||
_rawId[i] = deviceId[i];
|
RawId[index] = deviceId[index];
|
||||||
|
|
||||||
// Get the friendly name
|
// Get the friendly name
|
||||||
_friendlyName = ConvertToString(_rawId);
|
Name = ConvertToString(RawId);
|
||||||
|
|
||||||
// Get the family code
|
// Get the family code
|
||||||
_familyCode = _rawId[0];
|
Family = RawId[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
public Identifier(short[] deviceId)
|
public Identifier(short[] deviceId)
|
||||||
{
|
{
|
||||||
// Store the ID supplied
|
// Store the ID supplied
|
||||||
_rawId = deviceId;
|
RawId = deviceId;
|
||||||
|
|
||||||
// Get the friendly name
|
// Get the friendly name
|
||||||
_friendlyName = ConvertToString(_rawId);
|
Name = ConvertToString(RawId);
|
||||||
|
|
||||||
// Get the family code
|
// Get the family code
|
||||||
_familyCode = _rawId[0];
|
Family = RawId[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string ConvertToString(short[] rawId)
|
private static string ConvertToString(IList<short> rawId)
|
||||||
{
|
{
|
||||||
var friendlyId = new StringBuilder();
|
var friendlyId = new StringBuilder();
|
||||||
|
|
||||||
// Loop backwards over the ID array
|
// Loop backwards over the ID array
|
||||||
for (var iIndex = rawId.Length - 1; iIndex >= 0; iIndex--)
|
for (var index = rawId.Count - 1; index >= 0; index--)
|
||||||
{
|
{
|
||||||
// Convert the short value into a hex string and append it to the ID string
|
// Convert the short value into a hex string and append it to the ID string
|
||||||
friendlyId.AppendFormat("{0:X2}", rawId[iIndex]);
|
friendlyId.AppendFormat("{0:X2}", rawId[index]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the ID string
|
// Return the ID string
|
||||||
return friendlyId.ToString();
|
return friendlyId.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public short[] RawId
|
|
||||||
{
|
|
||||||
get { return _rawId; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public string Name
|
|
||||||
{
|
|
||||||
get { return _friendlyName; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public int Family
|
|
||||||
{
|
|
||||||
get { return _familyCode; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return _friendlyName;
|
return Name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
20
Network.cs
20
Network.cs
@@ -4,14 +4,15 @@ namespace OneWireAPI
|
|||||||
{
|
{
|
||||||
public class Network
|
public class Network
|
||||||
{
|
{
|
||||||
private Session _session; // Current session
|
private Session _session;
|
||||||
private Dictionary<string, Device> _deviceList; // List of current devices
|
|
||||||
|
public Dictionary<string, Device> Devices { get; private set; }
|
||||||
|
|
||||||
public Network(Session session)
|
public Network(Session session)
|
||||||
{
|
{
|
||||||
_session = session;
|
_session = session;
|
||||||
|
|
||||||
_deviceList = new Dictionary<string, Device>();
|
Devices = new Dictionary<string, Device>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public delegate void DeviceEventDelegate(Device device);
|
public delegate void DeviceEventDelegate(Device device);
|
||||||
@@ -67,10 +68,10 @@ namespace OneWireAPI
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check if we've seen this device before
|
// Check if we've seen this device before
|
||||||
if (!_deviceList.ContainsKey(device.Id.Name))
|
if (!Devices.ContainsKey(device.Id.Name))
|
||||||
{
|
{
|
||||||
// Add the device to the device list
|
// Add the device to the device list
|
||||||
_deviceList[device.Id.Name] = device;
|
Devices[device.Id.Name] = device;
|
||||||
|
|
||||||
// Raise the device added event
|
// Raise the device added event
|
||||||
if (DeviceAdded != null)
|
if (DeviceAdded != null)
|
||||||
@@ -83,11 +84,6 @@ namespace OneWireAPI
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Dictionary<string, Device> Devices
|
|
||||||
{
|
|
||||||
get { return _deviceList; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
// Load the device list
|
// Load the device list
|
||||||
@@ -97,8 +93,8 @@ namespace OneWireAPI
|
|||||||
public void Terminate()
|
public void Terminate()
|
||||||
{
|
{
|
||||||
// Get rid of the device list
|
// Get rid of the device list
|
||||||
_deviceList.Clear();
|
Devices.Clear();
|
||||||
_deviceList = null;
|
Devices = null;
|
||||||
|
|
||||||
// Get rid of the session
|
// Get rid of the session
|
||||||
_session = null;
|
_session = null;
|
||||||
|
|||||||
@@ -18,55 +18,47 @@ namespace OneWireAPI
|
|||||||
SetLevel
|
SetLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly int _errorNumber;
|
public Identifier DeviceId { get; private set; }
|
||||||
private readonly ExceptionFunction _errorFunction;
|
|
||||||
private readonly Identifier _deviceId;
|
public ExceptionFunction Function { get; private set; }
|
||||||
|
|
||||||
|
public int Number { get; private set; }
|
||||||
|
|
||||||
public OneWireException(ExceptionFunction function, int number)
|
public OneWireException(ExceptionFunction function, int number)
|
||||||
{
|
{
|
||||||
// Store the exception function
|
// Store the exception function
|
||||||
_errorFunction = function;
|
Function = function;
|
||||||
|
|
||||||
// Store the exception number
|
// Store the exception number
|
||||||
_errorNumber = number;
|
Number = number;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OneWireException(ExceptionFunction function, Identifier deviceId)
|
public OneWireException(ExceptionFunction function, Identifier deviceId)
|
||||||
{
|
{
|
||||||
// Store the exception function
|
// Store the exception function
|
||||||
_errorFunction = function;
|
Function = function;
|
||||||
|
|
||||||
// Store the device ID
|
// Store the device ID
|
||||||
_deviceId = deviceId;
|
DeviceId = deviceId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OneWireException(ExceptionFunction function, Identifier deviceId, int number)
|
public OneWireException(ExceptionFunction function, Identifier deviceId, int number)
|
||||||
{
|
{
|
||||||
// Store the exception function
|
// Store the exception function
|
||||||
_errorFunction = function;
|
Function = function;
|
||||||
|
|
||||||
// Store the device ID
|
// Store the device ID
|
||||||
_deviceId = deviceId;
|
DeviceId = deviceId;
|
||||||
|
|
||||||
// Store the exception number
|
// Store the exception number
|
||||||
_errorNumber = number;
|
Number = number;
|
||||||
}
|
|
||||||
|
|
||||||
public Identifier DeviceId
|
|
||||||
{
|
|
||||||
get { return _deviceId; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExceptionFunction Function
|
|
||||||
{
|
|
||||||
get { return _errorFunction; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string Message
|
public override string Message
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
switch (_errorFunction)
|
switch (Function)
|
||||||
{
|
{
|
||||||
case ExceptionFunction.Access:
|
case ExceptionFunction.Access:
|
||||||
return "Unable to access device";
|
return "Unable to access device";
|
||||||
@@ -87,14 +79,9 @@ namespace OneWireAPI
|
|||||||
case ExceptionFunction.SetLevel:
|
case ExceptionFunction.SetLevel:
|
||||||
return "Error setting level";
|
return "Error setting level";
|
||||||
default:
|
default:
|
||||||
return "Unknown error in function" + _errorFunction;
|
return "Unknown error in function" + Function;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Number
|
|
||||||
{
|
|
||||||
get { return _errorNumber; }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
80
Session.cs
80
Session.cs
@@ -5,57 +5,41 @@ namespace OneWireAPI
|
|||||||
{
|
{
|
||||||
public class Session
|
public class Session
|
||||||
{
|
{
|
||||||
private int _sessionHandle; // Session handle
|
public short PortNumber { get; private set; }
|
||||||
private Network _network; // Network object
|
|
||||||
|
|
||||||
private readonly short _portNumber; // Port number
|
public short PortType { get; private set; }
|
||||||
private readonly short _portType; // Port type
|
|
||||||
private readonly byte[] _stateBuffer; // Global state buffer
|
public int SessionHandle { get; private set; }
|
||||||
|
|
||||||
|
public Network Network { get; private set; }
|
||||||
|
|
||||||
|
public byte[] StateBuffer { get; private set; }
|
||||||
|
|
||||||
public Session()
|
public Session()
|
||||||
{
|
{
|
||||||
// Create the global state buffer
|
// Create the global state buffer
|
||||||
_stateBuffer = new byte[(int) TMEX.StateBufferSize.NoEpromWriting];
|
StateBuffer = new byte[(int) TMEX.StateBufferSize.NoEpromWriting];
|
||||||
|
|
||||||
|
short portNumber;
|
||||||
|
short portType;
|
||||||
|
|
||||||
// Get the default port number and type from the system
|
// Get the default port number and type from the system
|
||||||
var result = TMEX.TMReadDefaultPort(out _portNumber, out _portType);
|
var result = TMEX.TMReadDefaultPort(out portNumber, out portType);
|
||||||
|
|
||||||
Tracer.WriteLine("TMReadDefaultPort - Return: {0}, Port Number: {1}, Port Type: {2}", result, _portNumber, _portType);
|
PortNumber = portNumber;
|
||||||
|
PortType = portType;
|
||||||
|
|
||||||
|
Tracer.WriteLine("TMReadDefaultPort - Return: {0}, Port Number: {1}, Port Type: {2}", result, PortNumber, PortType);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Session(short portNumber, short portType)
|
public Session(short portNumber, short portType)
|
||||||
{
|
{
|
||||||
// Create the global state buffer
|
// Create the global state buffer
|
||||||
_stateBuffer = new byte[(int) TMEX.StateBufferSize.NoEpromWriting];
|
StateBuffer = new byte[(int) TMEX.StateBufferSize.NoEpromWriting];
|
||||||
|
|
||||||
// Store the port number and type specified
|
// Store the port number and type specified
|
||||||
_portNumber = portNumber;
|
PortNumber = portNumber;
|
||||||
_portType = portType;
|
PortType = portType;
|
||||||
}
|
|
||||||
|
|
||||||
public short PortNumber
|
|
||||||
{
|
|
||||||
get { return _portNumber; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public short PortType
|
|
||||||
{
|
|
||||||
get { return _portType; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public int SessionHandle
|
|
||||||
{
|
|
||||||
get { return _sessionHandle; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public Network Network
|
|
||||||
{
|
|
||||||
get { return _network; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] StateBuffer
|
|
||||||
{
|
|
||||||
get { return _stateBuffer; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Acquire()
|
public bool Acquire()
|
||||||
@@ -77,16 +61,16 @@ namespace OneWireAPI
|
|||||||
Tracer.WriteLine("Starting Aquire");
|
Tracer.WriteLine("Starting Aquire");
|
||||||
|
|
||||||
// Start a session on the port
|
// Start a session on the port
|
||||||
_sessionHandle = TMEX.TMExtendedStartSession(_portNumber, _portType, IntPtr.Zero);
|
SessionHandle = TMEX.TMExtendedStartSession(PortNumber, PortType, IntPtr.Zero);
|
||||||
|
|
||||||
Tracer.WriteLine("TMExtendedStartSession - Return: {0}", _sessionHandle);
|
Tracer.WriteLine("TMExtendedStartSession - Return: {0}", SessionHandle);
|
||||||
|
|
||||||
// If we didn't get a session then throw an error
|
// If we didn't get a session then throw an error
|
||||||
if (_sessionHandle <= 0)
|
if (SessionHandle <= 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Setup the port for the current session
|
// Setup the port for the current session
|
||||||
var result = TMEX.TMSetup(_sessionHandle);
|
var result = TMEX.TMSetup(SessionHandle);
|
||||||
|
|
||||||
Tracer.WriteLine("TMSetup - Return: {0}", result);
|
Tracer.WriteLine("TMSetup - Return: {0}", result);
|
||||||
|
|
||||||
@@ -100,10 +84,10 @@ namespace OneWireAPI
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create the network object and pass ourself as the session
|
// Create the network object and pass ourself as the session
|
||||||
_network = new Network(this);
|
Network = new Network(this);
|
||||||
|
|
||||||
// Initialize the network
|
// Initialize the network
|
||||||
_network.Initialize();
|
Network.Initialize();
|
||||||
|
|
||||||
// Initialize the static adapter code with the session
|
// Initialize the static adapter code with the session
|
||||||
Adapter.Initialize(this);
|
Adapter.Initialize(this);
|
||||||
@@ -116,24 +100,24 @@ namespace OneWireAPI
|
|||||||
Tracer.WriteLine("Starting Release");
|
Tracer.WriteLine("Starting Release");
|
||||||
|
|
||||||
// Terminate the network
|
// Terminate the network
|
||||||
if (_network != null)
|
if (Network != null)
|
||||||
{
|
{
|
||||||
_network.Terminate();
|
Network.Terminate();
|
||||||
_network = null;
|
Network = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close the session
|
// Close the session
|
||||||
var result = TMEX.TMClose(_sessionHandle);
|
var result = TMEX.TMClose(SessionHandle);
|
||||||
|
|
||||||
Tracer.WriteLine("TMClose - Return: {0}", result);
|
Tracer.WriteLine("TMClose - Return: {0}", result);
|
||||||
|
|
||||||
// End the session
|
// End the session
|
||||||
result = TMEX.TMEndSession(_sessionHandle);
|
result = TMEX.TMEndSession(SessionHandle);
|
||||||
|
|
||||||
Tracer.WriteLine("TMEndSession - Return: {0}", result);
|
Tracer.WriteLine("TMEndSession - Return: {0}", result);
|
||||||
|
|
||||||
// Clear the session variable
|
// Clear the session variable
|
||||||
_sessionHandle = 0;
|
SessionHandle = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user