using System.Device.I2c; namespace Libs_SequentMicrosystems { public class RTDStackLevelReader { public readonly byte DEVICE_ADDRESS = 0x40; public readonly byte RTD_RESISTANCE_ADD = 59; /// /// Read Single Resistance /// /// Level on Stack 0-7 /// Chanel on stack layer 1-8 /// Resistance of selected element /// /// 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}\""); } if (val < 0) { val = 0; } return val; } /// /// Get data from all inputs on stack level /// /// Number of level in stack /// Number of decimals numbers of measured resistance /// Measured resistances of all inputs public float[] GetStack(byte stack, byte precision = 2) { float[] chanels = new float[8]; //initialize float array chanels[0] = (float)Math.Round(Get(stack, 1), precision); //write readed value from input to array chanels[1] = (float)Math.Round(Get(stack, 2), precision); chanels[2] = (float)Math.Round(Get(stack, 3), precision); chanels[3] = (float)Math.Round(Get(stack, 4), precision); chanels[4] = (float)Math.Round(Get(stack, 5), precision); chanels[5] = (float)Math.Round(Get(stack, 6), precision); chanels[6] = (float)Math.Round(Get(stack, 7), precision); chanels[7] = (float)Math.Round(Get(stack, 8), precision); return chanels; //return array } } }