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; }
/// <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)); }
public byte[] write_word_read_byte(int addr, byte byte_msb, byte byte_lsb, int read_len = 1) { byte[] buf = new byte[read_len]; byte[] bytes = new byte[2]; int res_write = 0, res_read = 0; bytes[0] = byte_msb; bytes[1] = byte_lsb; I2CNativeLib.write_word_read_byte(busHandle, addr, bytes, bytes.Length, buf, buf.Length, res_write, res_read); return(buf); }
/// <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()))); } if (res == -2) { throw new IOException(String.Format("Error writing to address '{0}': I2C transaction failed", address)); } }
protected virtual void Dispose(bool disposing) { if (disposing) { // disposing managed resouces } if (busHandle != 0) { I2CNativeLib.CloseBus(busHandle); busHandle = 0; } }
/// <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); }
protected static int Set16(string i2Cbusid, string deviceaddress, string dataaddress, string datavalue, int force) { var value = (UInt16)Convert.ToInt16(datavalue, 16); var add1 = (UInt16)Convert.ToInt16(dataaddress, 16); var add2 = ++add1; var msb = GetAsHexString(value >> 8); var lsb = GetAsHexString(value & 0xFF); Console.WriteLine("16-bit: Writing 16-bit Value: {0} as 2 8-bit values {1} and {2}", GetAsHexString(value), msb, lsb); Console.WriteLine("16-bit: Writing {0} to address {1}", msb, GetAsHexString(add1)); var data = I2CNativeLib.Set(i2Cbusid, deviceaddress, GetAsHexString(add1), msb, force); //set msb byte/8 bits Console.WriteLine("16-bit: Response to msb: {0}", data); Console.WriteLine("16-bit: Writing {0} to address {1}", lsb, GetAsHexString(add2)); data |= I2CNativeLib.Set(i2Cbusid, deviceaddress, GetAsHexString(add2), lsb, force); //set lsb byte/8 bits Console.WriteLine("16-bit: Response to msb |= lsb: {0}", data); return(data); }
protected static byte Get(string i2Cbusid, string deviceaddress, string dataaddress) { return((byte)I2CNativeLib.Get(i2Cbusid, deviceaddress, dataaddress)); }
protected static byte Set8(string i2Cbusid, string deviceaddress, string dataaddress, string datavalue, int force) { Console.WriteLine("8-bit: Writing {0} to address {1}", datavalue, dataaddress); //8-bit return((byte)I2CNativeLib.Set(i2Cbusid, deviceaddress, dataaddress, datavalue, force)); }