Created
January 22, 2018 15:05
-
-
Save brannow/4e9bd53c5b58d3c7d579001ce45cc0c5 to your computer and use it in GitHub Desktop.
C# USB Temperature Sensor Tester Interface for DS18B20
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// see: https://github.com/mikeobrien/HidLibrary/tree/master/src/HidLibrary | |
using HidLibrary; | |
using System; | |
using System.Linq; | |
using System.Runtime.InteropServices; | |
namespace Environment.HardwareData.SensorData | |
{ | |
class TempSensor | |
{ | |
[StructLayout(LayoutKind.Explicit)] | |
private struct SensorDataResult { | |
[FieldOffset(1)] | |
public byte SensorCount; | |
[FieldOffset(2)] | |
public byte currentSensor; | |
[FieldOffset(3)] | |
public byte powerSupply; | |
[FieldOffset(5)] | |
public short temp; | |
[FieldOffset(8)] | |
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8, ArraySubType = UnmanagedType.U8)] | |
public byte[] sensorId; | |
} | |
private const int VendorId = 0x16C0; | |
private const int ProductId = 0x0480; | |
private readonly HidDevice sensor; | |
private SensorDataResult sensorResult; | |
private bool failure; | |
public static TempSensor DetectSensor() | |
{ | |
HidDevice _device = HidDevices.Enumerate(VendorId, ProductId).FirstOrDefault(); | |
if (_device != null) | |
{ | |
return new TempSensor(_device); | |
} | |
return null; | |
} | |
public TempSensor(HidDevice sensor) | |
{ | |
this.sensor = sensor; | |
this.sensorResult = new SensorDataResult(); | |
Update(); | |
} | |
// in °C | |
public float GetTemp() | |
{ | |
return (float)sensorResult.temp / 10; | |
} | |
public bool IsFailure() | |
{ | |
return failure; | |
} | |
public void Update() | |
{ | |
HidDeviceData data = sensor.Read(2); | |
if (data.Status == HidDeviceData.ReadStatus.Success) | |
{ | |
int size = Marshal.SizeOf(this.sensorResult); | |
IntPtr ptr = Marshal.AllocHGlobal(size); | |
Marshal.Copy(data.Data, 0, ptr, size); | |
this.sensorResult = (SensorDataResult)Marshal.PtrToStructure(ptr, sensorResult.GetType()); | |
Marshal.FreeHGlobal(ptr); | |
failure = false; | |
} | |
else | |
{ | |
failure = true; | |
sensorResult.temp = 0; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment