diff --git a/Crc16.cs b/Crc16.cs index 9504a24..828fbed 100644 --- a/Crc16.cs +++ b/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 }; - 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 var currentCrc = iInitialValue; // Current CRC accumulator // 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 - int iBuffer = nData[index]; + int iBuffer = data[index]; // Calculate the current CRC for this position iBuffer = (iBuffer ^ (currentCrc & 0xFF)) & 0xFF; diff --git a/Device.cs b/Device.cs index 5a53238..c3bd75d 100644 --- a/Device.cs +++ b/Device.cs @@ -3,7 +3,8 @@ namespace OneWireAPI public class Device { protected Session Session; - protected Identifier DeviceId; + + public Identifier Id { get; protected set; } public Device(Session session, short[] rawId) { @@ -11,17 +12,12 @@ namespace OneWireAPI Session = session; // Create a new identifier and give it the ID supplied - DeviceId = new Identifier(rawId); - } - - public Identifier Id - { - get { return DeviceId; } + Id = new Identifier(rawId); } public int Family { - get { return DeviceId.Family; } + get { return Id.Family; } } } } diff --git a/DeviceFamily10.cs b/DeviceFamily10.cs index 9e1e049..f3d93ab 100644 --- a/DeviceFamily10.cs +++ b/DeviceFamily10.cs @@ -11,7 +11,7 @@ namespace OneWireAPI public double GetTemperature() { // 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 Adapter.SetLevel(TMEX.LevelOperation.Write, TMEX.LevelMode.StrongPullup, TMEX.LevelPrime.AfterNextByte); @@ -62,7 +62,7 @@ namespace OneWireAPI if (crc != data[9]) { // 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 diff --git a/DeviceFamily12.cs b/DeviceFamily12.cs index 7083123..46ad8af 100644 --- a/DeviceFamily12.cs +++ b/DeviceFamily12.cs @@ -54,7 +54,7 @@ namespace OneWireAPI public byte[] ReadDevice() { // 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 var data = new byte[30]; @@ -88,7 +88,7 @@ namespace OneWireAPI if (crcResult != matchCrc) { // Throw a CRC exception - throw new OneWireException(OneWireException.ExceptionFunction.Crc, DeviceId); + throw new OneWireException(OneWireException.ExceptionFunction.Crc, Id); } var state = new byte[2]; @@ -111,7 +111,7 @@ namespace OneWireAPI data[dataCount++] = 0xFF; // Select and access the ID of the device we want to talk to - Adapter.Select(DeviceId); + Adapter.Select(Id); // Send the data Adapter.SendBlock(data, dataCount); @@ -128,7 +128,7 @@ namespace OneWireAPI if (crcResult != matchCrc) { // Throw a CRC exception - throw new OneWireException(OneWireException.ExceptionFunction.Crc, DeviceId); + throw new OneWireException(OneWireException.ExceptionFunction.Crc, Id); } // Store the state data @@ -140,7 +140,7 @@ namespace OneWireAPI public void WriteDevice(byte[] state) { // 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 var data = new byte[30]; @@ -177,7 +177,7 @@ namespace OneWireAPI if (crcResult != matchCrc) { // Throw a CRC exception - throw new OneWireException(OneWireException.ExceptionFunction.Crc, DeviceId); + throw new OneWireException(OneWireException.ExceptionFunction.Crc, Id); } } } diff --git a/DeviceFamily1D.cs b/DeviceFamily1D.cs index d384b8c..fbb629e 100644 --- a/DeviceFamily1D.cs +++ b/DeviceFamily1D.cs @@ -11,7 +11,7 @@ namespace OneWireAPI public uint GetCounter(int counterPage) { // 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 var data = new byte[30]; @@ -49,7 +49,7 @@ namespace OneWireAPI if (crcResult != matchCrc) { // Throw a CRC exception - throw new OneWireException(OneWireException.ExceptionFunction.Crc, DeviceId); + throw new OneWireException(OneWireException.ExceptionFunction.Crc, Id); } uint counter = 0; diff --git a/DeviceFamily20.cs b/DeviceFamily20.cs index 2444b4e..9c50b27 100644 --- a/DeviceFamily20.cs +++ b/DeviceFamily20.cs @@ -53,7 +53,7 @@ namespace OneWireAPI data[dataCount++] = (startAddress >> 8) & 0xFF; // 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 for (var index = startAddress; index <= endAddress; index++) @@ -75,7 +75,7 @@ namespace OneWireAPI if (data[dataCount - 1] != _control[index - startAddress]) { // 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 @@ -107,7 +107,7 @@ namespace OneWireAPI if (calculatedCrc != sentCrc) { // Throw a CRC exception - throw new OneWireException(OneWireException.ExceptionFunction.Crc, DeviceId); + throw new OneWireException(OneWireException.ExceptionFunction.Crc, Id); } // Reset the byte count @@ -118,7 +118,7 @@ namespace OneWireAPI public double[] GetVoltages() { // 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 var data = new byte[30]; @@ -154,18 +154,18 @@ namespace OneWireAPI if (calculatedCrc != sentCrc) { // 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 Adapter.SetLevel(TMEX.LevelOperation.Write, TMEX.LevelMode.StrongPullup, TMEX.LevelPrime.AfterNextByte); - var nTransmitByte = (short) ((dataCount - 1) & 0x1F); + var transmitByte = (short) ((dataCount - 1) & 0x1F); try { // Send the byte and start strong pullup - Adapter.SendByte(nTransmitByte); + Adapter.SendByte(transmitByte); } catch { @@ -186,7 +186,7 @@ namespace OneWireAPI Adapter.ReadByte(); // Select and access the ID of the device we want to talk to - Adapter.Select(DeviceId); + Adapter.Select(Id); // Reinitialize the data count dataCount = 0; @@ -217,7 +217,7 @@ namespace OneWireAPI if (calculatedCrc != sentCrc) { // Throw a CRC exception - throw new OneWireException(OneWireException.ExceptionFunction.Crc, DeviceId); + throw new OneWireException(OneWireException.ExceptionFunction.Crc, Id); } // Voltage values to return diff --git a/DeviceFamily26.cs b/DeviceFamily26.cs index 9888c8d..a1f0501 100644 --- a/DeviceFamily26.cs +++ b/DeviceFamily26.cs @@ -19,7 +19,7 @@ namespace OneWireAPI short busy; // 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 var data = new byte[30]; @@ -62,7 +62,7 @@ namespace OneWireAPI if (crc != data[10]) { // 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 @@ -170,7 +170,7 @@ namespace OneWireAPI if (crc != data[10]) { // Throw a CRC exception - throw new OneWireException(OneWireException.ExceptionFunction.Crc, DeviceId); + throw new OneWireException(OneWireException.ExceptionFunction.Crc, Id); } // Assemble the voltage data @@ -193,7 +193,7 @@ namespace OneWireAPI public double GetTemperature() { // Select and access the ID of the device we want to talk to - Adapter.Select(DeviceId); + Adapter.Select(Id); // Send the conversion command byte Adapter.SendByte(0x44); @@ -245,7 +245,7 @@ namespace OneWireAPI if (crc != data[10]) { // 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 @@ -253,10 +253,10 @@ namespace OneWireAPI int temperatureMsb = data[4]; // Shift the data into the right order - var iTemperature = ((temperatureMsb << 8) | temperatureLsb) >> 3; + var temperature = ((temperatureMsb << 8) | temperatureLsb) >> 3; // Return the temperature - return iTemperature * 0.03125F; + return temperature * 0.03125F; } } } diff --git a/DeviceFamilyFF.cs b/DeviceFamilyFF.cs index a5c2272..832f433 100644 --- a/DeviceFamilyFF.cs +++ b/DeviceFamilyFF.cs @@ -21,7 +21,7 @@ namespace OneWireAPI public void SetBackLight(bool state) { // Select the device - Adapter.Select(DeviceId); + Adapter.Select(Id); // Set the state of the backlight Adapter.SendByte((short) (state ? 0x8 : 0x7)); @@ -87,7 +87,7 @@ namespace OneWireAPI if (_width > 16) { // Select the device - Adapter.Select(DeviceId); + Adapter.Select(Id); // Set the data block to just the first 16 characters sendData = text.Substring(0, 16); @@ -109,7 +109,7 @@ namespace OneWireAPI Adapter.SendBlock(data, dataCount); // Select the device - Adapter.Select(DeviceId); + Adapter.Select(Id); // Send the scratchpad data to the LCD Adapter.SendByte(0x48); @@ -130,7 +130,7 @@ namespace OneWireAPI } // Select the device - Adapter.Select(DeviceId); + Adapter.Select(Id); // Initialize the data array data = new byte[18]; @@ -152,7 +152,7 @@ namespace OneWireAPI Adapter.SendBlock(data, dataCount); // Select the device - Adapter.Select(DeviceId); + Adapter.Select(Id); // Send the scratchpad data to the LCD Adapter.SendByte(0x48); @@ -164,7 +164,7 @@ namespace OneWireAPI public void Clear() { // Select the device - Adapter.Select(DeviceId); + Adapter.Select(Id); // Clear the display Adapter.SendByte(0x49); diff --git a/Identifer.cs b/Identifer.cs index 10f64ba..8e04622 100644 --- a/Identifer.cs +++ b/Identifer.cs @@ -1,80 +1,68 @@ +using System.Collections.Generic; using System.Text; namespace OneWireAPI { public class Identifier { - private readonly short[] _rawId; // The raw ID array - private readonly string _friendlyName; // Friendly display name - private readonly int _familyCode; // Family code + public short[] RawId { get; private set; } + + public string Name { get; private set; } + + public int Family { get; private set; } public Identifier() { // Create a blank ID - _rawId = new short[8]; + RawId = new short[8]; } - public Identifier(byte[] deviceId) + public Identifier(IList deviceId) { // Create a blank ID - _rawId = new short[8]; + RawId = new short[8]; // Copy the byte array to the short array - for (var i = 0; i < deviceId.Length; i++) - _rawId[i] = deviceId[i]; + for (var index = 0; index < deviceId.Count; index++) + RawId[index] = deviceId[index]; // Get the friendly name - _friendlyName = ConvertToString(_rawId); + Name = ConvertToString(RawId); // Get the family code - _familyCode = _rawId[0]; + Family = RawId[0]; } public Identifier(short[] deviceId) { // Store the ID supplied - _rawId = deviceId; + RawId = deviceId; // Get the friendly name - _friendlyName = ConvertToString(_rawId); + Name = ConvertToString(RawId); // Get the family code - _familyCode = _rawId[0]; + Family = RawId[0]; } - private static string ConvertToString(short[] rawId) + private static string ConvertToString(IList rawId) { var friendlyId = new StringBuilder(); // 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 - friendlyId.AppendFormat("{0:X2}", rawId[iIndex]); + friendlyId.AppendFormat("{0:X2}", rawId[index]); } // Return the ID string 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() { - return _friendlyName; + return Name; } } } diff --git a/Network.cs b/Network.cs index 1cbc727..a6b3d0b 100644 --- a/Network.cs +++ b/Network.cs @@ -4,14 +4,15 @@ namespace OneWireAPI { public class Network { - private Session _session; // Current session - private Dictionary _deviceList; // List of current devices + private Session _session; + + public Dictionary Devices { get; private set; } public Network(Session session) { _session = session; - _deviceList = new Dictionary(); + Devices = new Dictionary(); } public delegate void DeviceEventDelegate(Device device); @@ -67,10 +68,10 @@ namespace OneWireAPI } // 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 - _deviceList[device.Id.Name] = device; + Devices[device.Id.Name] = device; // Raise the device added event if (DeviceAdded != null) @@ -83,11 +84,6 @@ namespace OneWireAPI } } - public Dictionary Devices - { - get { return _deviceList; } - } - public void Initialize() { // Load the device list @@ -97,8 +93,8 @@ namespace OneWireAPI public void Terminate() { // Get rid of the device list - _deviceList.Clear(); - _deviceList = null; + Devices.Clear(); + Devices = null; // Get rid of the session _session = null; diff --git a/OneWireException.cs b/OneWireException.cs index 9eb56f8..f9dc6a9 100644 --- a/OneWireException.cs +++ b/OneWireException.cs @@ -18,55 +18,47 @@ namespace OneWireAPI SetLevel } - private readonly int _errorNumber; - private readonly ExceptionFunction _errorFunction; - private readonly Identifier _deviceId; + public Identifier DeviceId { get; private set; } + + public ExceptionFunction Function { get; private set; } + + public int Number { get; private set; } public OneWireException(ExceptionFunction function, int number) { // Store the exception function - _errorFunction = function; + Function = function; // Store the exception number - _errorNumber = number; + Number = number; } public OneWireException(ExceptionFunction function, Identifier deviceId) { // Store the exception function - _errorFunction = function; + Function = function; // Store the device ID - _deviceId = deviceId; + DeviceId = deviceId; } public OneWireException(ExceptionFunction function, Identifier deviceId, int number) { // Store the exception function - _errorFunction = function; + Function = function; // Store the device ID - _deviceId = deviceId; + DeviceId = deviceId; // Store the exception number - _errorNumber = number; - } - - public Identifier DeviceId - { - get { return _deviceId; } - } - - public ExceptionFunction Function - { - get { return _errorFunction; } + Number = number; } public override string Message { get { - switch (_errorFunction) + switch (Function) { case ExceptionFunction.Access: return "Unable to access device"; @@ -87,14 +79,9 @@ namespace OneWireAPI case ExceptionFunction.SetLevel: return "Error setting level"; default: - return "Unknown error in function" + _errorFunction; + return "Unknown error in function" + Function; } } } - - public int Number - { - get { return _errorNumber; } - } } } diff --git a/Session.cs b/Session.cs index 8568df9..9bd7696 100644 --- a/Session.cs +++ b/Session.cs @@ -5,57 +5,41 @@ namespace OneWireAPI { public class Session { - private int _sessionHandle; // Session handle - private Network _network; // Network object + public short PortNumber { get; private set; } - private readonly short _portNumber; // Port number - private readonly short _portType; // Port type - private readonly byte[] _stateBuffer; // Global state buffer + public short PortType { get; private set; } + + public int SessionHandle { get; private set; } + + public Network Network { get; private set; } + + public byte[] StateBuffer { get; private set; } public Session() { // 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 - 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) { // 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 - _portNumber = portNumber; - _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; } + PortNumber = portNumber; + PortType = portType; } public bool Acquire() @@ -77,16 +61,16 @@ namespace OneWireAPI Tracer.WriteLine("Starting Aquire"); // 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 (_sessionHandle <= 0) + if (SessionHandle <= 0) return false; // Setup the port for the current session - var result = TMEX.TMSetup(_sessionHandle); + var result = TMEX.TMSetup(SessionHandle); Tracer.WriteLine("TMSetup - Return: {0}", result); @@ -100,10 +84,10 @@ namespace OneWireAPI } // Create the network object and pass ourself as the session - _network = new Network(this); + Network = new Network(this); // Initialize the network - _network.Initialize(); + Network.Initialize(); // Initialize the static adapter code with the session Adapter.Initialize(this); @@ -116,24 +100,24 @@ namespace OneWireAPI Tracer.WriteLine("Starting Release"); // Terminate the network - if (_network != null) + if (Network != null) { - _network.Terminate(); - _network = null; + Network.Terminate(); + Network = null; } // Close the session - var result = TMEX.TMClose(_sessionHandle); + var result = TMEX.TMClose(SessionHandle); Tracer.WriteLine("TMClose - Return: {0}", result); // End the session - result = TMEX.TMEndSession(_sessionHandle); + result = TMEX.TMEndSession(SessionHandle); Tracer.WriteLine("TMEndSession - Return: {0}", result); // Clear the session variable - _sessionHandle = 0; + SessionHandle = 0; } } }