Libs_SequentMicrosystems/RTD_8chanels_board.cs

57 lines
1.6 KiB
C#
Raw Normal View History

2023-11-22 16:24:01 +00:00
using System;
using System.Device.Gpio;
using System.Device.I2c;
namespace Libs_SequentMicrosystems
{
public class RTDStackLevelReader
{
public readonly byte DEVICE_ADDRESS = 0x40;
public readonly byte RTD_RESISTANCE_ADD = 59;
/// <summary>
/// Read Single Resistance
/// </summary>
/// <param name="stack">Level on Stack 0-7</param>
/// <param name="channel">Chanel on stack layer 1-8</param>
/// <returns>Resistance of selected element</returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="IOException"></exception>
public float Get(byte stack, byte channel)
{
if (stack < 0 || stack > 7)
throw new ArgumentException("Invalid stack level");
if (channel < 1 || channel > 8)
throw new ArgumentException("Invalid channel number");
float val = -273.15f;
try
{
using (I2cDevice i2c = I2cDevice.Create(new I2cConnectionSettings(1, DEVICE_ADDRESS + stack)))
{
byte[] buffer = new byte[4];
i2c.WriteByte((byte)(RTD_RESISTANCE_ADD + (4 * (channel - 1))));
i2c.Read(buffer);
val = BitConverter.ToSingle(buffer, 0);
}
}
catch (IOException e)
{
throw new IOException($"Fail to communicate with the RTD card with message: \"{e.Message}\"");
}
return val;
}
//public float[] GetStack(byte stack)
//{
//}
}
}