public byte[] ReadDeviceRegister(int address, byte register) { byte[] buf = new byte[10]; int res = I2CNativeLib.readRegister(busHandle, address, register, buf); return(buf); }
/// <summary> /// .ctor /// </summary> /// <param name="busPath"></param> protected I2CBus(string busPath) { int res = I2CNativeLib.OpenBus(busPath); if (res < 0) { throw new IOException(/*String.Format("Error opening bus '{0}': {1}", busPath, UnixMarshal.GetErrorDescription(Stdlib.GetLastError()))*/); } busHandle = res; }
protected virtual void Dispose(bool disposing) { if (disposing) { // disposing managed resouces } if (busHandle != 0) { I2CNativeLib.CloseBus(busHandle); busHandle = 0; } }
/// <summary> /// Writes array of bytes. /// </summary> /// <remarks>Do not write more than 3 bytes at once, RPi drivers don't support this currently.</remarks> /// <param name="address">Address of a destination device</param> /// <param name="bytes"></param> public void WriteBytes(int address, byte[] bytes) { int res = I2CNativeLib.WriteBytes(busHandle, address, bytes, bytes.Length); if (res == -1) { throw new IOException(/*String.Format("Error accessing address '{0}': {1}", address, UnixMarshal.GetErrorDescription(Stdlib.GetLastError()))*/); } // HACK: removing res== -2 check because it was disrupting communications if (res == -2) { throw new IOException(String.Format("Error writing to address '{0}': I2C transaction failed", address)); } }
/// <summary> /// Reads bytes from device with passed address. /// </summary> /// <param name="address"></param> /// <param name="count"></param> /// <returns></returns> public byte[] ReadBytes(int address, int count) { byte[] buf = new byte[count]; int res = I2CNativeLib.ReadBytes(busHandle, address, buf, buf.Length); if (res == -1) { throw new IOException(/*String.Format("Error accessing address '{0}': {1}", address, UnixMarshal.GetErrorDescription(Stdlib.GetLastError()))*/); } if (res <= 0) { throw new IOException(String.Format("Error reading from address '{0}': I2C transaction failed", address)); } if (res < count) { Array.Resize(ref buf, res); } return(buf); }