For reference - her is my OneWire Engine using Netduino Plus 2 and native OneWire. (it may not run without modifications as it is part of my home automation controller.).
The engine runs in a loop and maintain 2 tables - one for DS1820 and one for DS2438 as these are the once i use.
All use of the data from the devices, are retrieved from these tables.
I have used a similar module on Netduino Plus (ver.1) but with the use of CW2's implementation of OneWire. This requires the special firmware and is only availalbe on NETMF 4.1.
using System;using System.Collections;using System.Threading;using Config;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;namespace EPD{ static class owCommands { public const byte SkipRom = 0xCC; public const byte MatchRom = 0x55; public const byte Convert1820 = 0x44; public const byte ReadScratchpad = 0xBE; public const byte CopyScratchpad = 0x48; public const byte WriteScratchpad = 0x4E; public const byte RecallMemory = 0xB8; public const byte ConvertT = 0x44; public const byte ConvertV = 0xB4; } static class OneWireExtension { public static bool isOwAddr(string parm) { if (parm.Length != 16) return false; for (int i = 0; i < 16; i++) { bool ishex = (((parm[i] >= '0') & (parm[i] <= '9')) | ((parm[i] >= 'A') & (parm[i] <= 'F')) | ((parm[i] >= 'a') & (parm[i] <= 'f'))); if (!ishex) return false; } return true; } const string hexChar = "0123456789ABCDEF"; public static string AddrString(this byte[] buf) // { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF } converts to 'FECDAB8967452301' { string s = string.Empty; for (int i = 0; i < buf.Length; i++) s += buf[buf.Length - i - 1].toHex(); return s; } public static byte[] AddrBytes(this string s) // '0123456789ABCDEF' converts to { 0xEF, 0xCD, 0xAB, 0x89, 0x67, 0x45, 0x23, 0x01 } { int j = s.Length; byte[] b = new byte[(j + 1) >> 1]; for (int i = 0; i < j; i++) { int ir = b.Length - (i >> 1) - 1; int x = hexChar.IndexOf(s.ToUpper()[i]); b[ir] = ((i & 1) == 0) ? (byte)(x << 4) : (byte)(b[ir] | (byte)x); } return b; } public static int inList(this ArrayList ar, string addr) { for (int i = 0 ; i< ar.Count; i++) { if ((ar[i] as byte[]).AddrString() == addr) return i; } return -1; } public static string getLocation(string addr) { //string place = HttpClient.webGet("http://palsbo.net/getLocation.php?addr=" + addr); //if (place != "") Ini.setValue(addr, place); //return place == "" ? Ini.getValue(addr, "(undefined)") : place; return Ini.getValue(addr, "(undefined)"); } } class OneWireEngine : IDisposable { static OneWire core; public static bool Pause = false; private static bool ready = false; public OneWireEngine(OutputPort _core) { core = new OneWire(_core); ds1820.dev.Clear(); new Thread(Start).Start(); while (!ready) { Thread.Sleep(200); } Debug.Print("OneWire Started"); } public void Dispose() { GC.SuppressFinalize(this); } public static void Start() { while (true) { core.TouchReset(); core.TouchByte(owCommands.SkipRom); core.TouchByte(owCommands.Convert1820); Thread.Sleep(750); var devs = core.FindAllDevices(); lock (ds1820.dev) foreach (ds1820.Device d in ds1820.dev) { if (devs.inList(d.Addr) < 0) ds1820.dev.Remove(d); } lock (ds2438.dev) foreach (ds2438.Device d in ds2438.dev) { if (devs.inList(d.Addr) < 0) ds2438.dev.Remove(d); } foreach (byte[] _Rom in devs) { string addr = _Rom.AddrString(); if (addr.Substring(14) == "10") { double Temp = get1820(_Rom); int index = ds1820.inList(addr); if (index < 0) { string Place = OneWireExtension.getLocation(addr); ds1820.dev.Add(new ds1820.Device(addr, Temp, Place)); } else { ds1820.setTemp(index, Temp); } } if (addr.Substring(14) == "26") { double Temp = 0.0; double Current = 0.0; double Volt = 0.0; int index = ds2438.inList(addr); if (index < 0) { init2438(_Rom); get2438(_Rom, ref Temp, ref Current, ref Volt); string Place = OneWireExtension.getLocation(addr); ds2438.dev.Add(new ds2438.Device(addr, Temp, Current, Volt, Place)); } else { get2438(_Rom, ref Temp, ref Current, ref Volt); ds2438.setData(index, Temp, Current, Volt); } } ready = true; Thread.Sleep(2000); } } } public static void WriteBytes(byte[] data) { foreach (byte B in data) { core.TouchByte(B); } } public static void ReadBytes(byte[] buf) { for (var i = 0; i < buf.Length; i++) buf[i] = (byte)(core.ReadByte()); } public static void Match(byte[] addr) { core.TouchReset(); core.TouchByte(owCommands.MatchRom); //match rom WriteBytes(addr); } private static double get1820(byte[] _Rom) { Match(_Rom); core.WriteByte(owCommands.ReadScratchpad); byte[] buf = { 255, 255, 255, 255, 255, 255, 255, 255, 255 }; ReadBytes(buf); return (float)((short)((buf[1] << 8) | buf[0]) >> 1) + (float)(buf[7] - buf[6]) / (float)buf[7] - .25; } private static void init2438(byte[] _Rom) { const byte IAD = 0x01; const byte CA = 0x02; const byte EE = 0x04; //const byte AD = 0x08; const byte Page0 = 0x00; const byte Ctrl = IAD | CA | EE; var buf = new byte[9]; Match(_Rom); WriteBytes(new byte[] { owCommands.WriteScratchpad, Page0, Ctrl }); Match(_Rom); WriteBytes(new byte[] { owCommands.ReadScratchpad, 0x00 }); ReadBytes(buf); Match(_Rom); WriteBytes(new byte[] { owCommands.CopyScratchpad, 0x00 }); while (core.ReadByte() != 0xFF) { }; } private static void get2438(byte[] _Rom, ref double Temp, ref double Current, ref double Volt) { var buf = new byte[9]; Match(_Rom); core.WriteByte(owCommands.ConvertT); while (core.ReadByte() != 0xFF) { }; Thread.Sleep(10); Match(_Rom); core.WriteByte(owCommands.ConvertV); while (core.ReadByte() != 0xFF) { }; Thread.Sleep(10); Match(_Rom); WriteBytes(new byte[] { owCommands.RecallMemory, 0x00 }); Match(_Rom); WriteBytes(new byte[] { owCommands.ReadScratchpad, 0x00 }); ReadBytes(buf); Temp = (double)((buf[2] << 8) + (buf[1])) / 256; Volt = (double)((buf[4] << 8) + (buf[3])) - 224; // / 100; //Volt = Volt / 1.75672; Current = (double)((Int16)(buf[6] << 8) | (Int16)buf[5]); } } class ds1820 { public class Device { public string Addr { get; set; } public string Place { get; set; } public double Temp { get; set; } public Device(string addr, double temp, string place = null) { Addr = addr; Temp = temp; if (place == null) Place = Ini.getValue(Addr, "(undefined)"); else Place = place; } } public static int inList(string addr) { for (int i = 0; i< dev.Count; i++) { if ((dev[i] as Device).Addr == addr) return i; } return -1; } public static string Addr(int i) { return (dev[i] as Device).Addr; } public static string Place(int i) { return (dev[i] as Device).Place; } public static void Place(int i, string Place) { (dev[i] as Device).Place = Place; } public static double Temp(int i) { return (dev[i] as Device).Temp; } public static void setTemp(int index, double Temp) { (dev[index] as Device).Temp = Temp; } public static ArrayList dev = new ArrayList(); public static int Count { get { return dev.Count; } } public class InUse { static string _InUse; public static int Index { get { for (int i = 0; i < dev.Count; i++) { if (Addr(i) == _InUse) return i; } if (dev.Count > 0) return 0; else return -1; } } public static string Addr { get { return Index < 0 ? "" : Addr(Index); } set { _InUse = value.ToUpper(); Ini.setValue("DS1820inUse", _InUse); } } } } class ds2438 { public class Device { public string Addr { get; set; } public string Place { get; set; } public double Temp { get; set; } public double Volt { get; set; } public double Current { get; set; } public Device(string addr, double temp, double current, double volt, string place = null) { Addr = addr; Temp = temp; Current = current; Volt = volt; if (place == null) Place = Ini.getValue(Addr, "(undefined)"); else Place = place; } } public static int inList(string addr) { for (int i = 0; i<dev.Count; i++) { if ((dev[i] as Device).Addr == addr) return i; } return -1; } public static ArrayList dev = new ArrayList(); public static int Count { get { return dev.Count; } } public static string Addr(int i) { return (dev[i] as Device).Addr; } public static string Place(int i) { return (dev[i] as Device).Place; } public static void Place(int i, string Place) { (dev[i] as Device).Place = Place; } public static double Temp(int i) { return (dev[i] as Device).Temp; } public static double Current(int i) { return (dev[i] as Device).Current; } public static double Volt(int i) { return (dev[i] as Device).Volt; } public static void setData(int index, double Temp, double Current, double Volt) { (dev[index] as Device).Temp = Temp; (dev[index] as Device).Current = Current; (dev[index] as Device).Volt = Volt; } public class InUse { static string _InUse; public static int Index { get { for (int i = 0; i < dev.Count; i++) { if (Addr(i) == _InUse) return i; } if (dev.Count > 0) return 0; else return -1; } } public static string Addr { get { return Index < 0 ? "" : Addr(Index); } set { _InUse = value.ToUpper(); Ini.setValue("DS2438inUse", _InUse); } } } }}
(this code block display needs to be fixed!!!)