public static void Main() { //our 10 bit address const ushort address10Bit = 0x1001; //binary 1000000001 = 129 const byte addressMask = 0x78; //reserved address mask 011110XX for 10 bit addressing //first MSB part of address ushort address1 = addressMask | (address10Bit >> 8); //is 7A and contains the two MSB of the 10 bit address I2CDevice.Configuration config = new I2CDevice.Configuration(address1, 100); I2CDevice device = new I2CDevice(config); //second LSB part of address byte address2 = (byte)(address10Bit & 0xFF); //the other 8 bits (LSB) byte[] address2OutBuffer = new byte[] { address2 }; I2CDevice.I2CWriteTransaction addressWriteTransaction = device.CreateWriteTransaction(address2OutBuffer); //prepare buffer to write data byte[] outBuffer = new byte[] { 0xAA }; I2CDevice.I2CWriteTransaction writeTransaction = device.CreateWriteTransaction(outBuffer); //prepare buffer to read data byte[] inBuffer = new byte[4]; I2CDevice.I2CReadTransaction readTransaction = device.CreateReadTransaction(inBuffer); //execute transactions I2CDevice.I2CTransaction[] transactions = new I2CDevice.I2CTransaction[] { addressWriteTransaction, writeTransaction, readTransaction }; device.Execute(transactions, 100); }
public I2CDevice.I2CReadTransaction CreateReadTransaction(byte[] buffer) { if (this.isDisposed) { throw new ObjectDisposedException(); } return(singletonDevice.CreateReadTransaction(buffer)); }
public int Read(XI2CDevice.Configuration configuration, byte[] data) { _device.Config = configuration; XI2CDevice.I2CReadTransaction read = XI2CDevice.CreateReadTransaction(data); return(_device.Execute(new XI2CDevice.I2CTransaction[] { read }, _readTimeout)); }
void readValues(I2CDevice adxl345, short[] result) { byte[] values = new byte[6]; I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[2]; byte[] RegisterNum = new byte[1] { valuesRegister }; xActions[0] = adxl345.CreateWriteTransaction(RegisterNum); xActions[1] = adxl345.CreateReadTransaction(values); if (adxl345.Execute(xActions, 1000) == 0) throw new Exception("Unable to read accelerometer"); result[0] = (short)(values[0] + (values[1] << 8)); result[1] = (short)(values[2] + (values[3] << 8)); result[2] = (short)(values[4] + (values[5] << 8)); }
public int ReadRegister(XI2CDevice.Configuration configuration, byte register, byte[] data, int length) { _device.Config = configuration; byte[] tmp = new byte[length]; XI2CDevice.I2CWriteTransaction write = XI2CDevice.CreateWriteTransaction(new byte[] { register }); XI2CDevice.I2CReadTransaction read = XI2CDevice.CreateReadTransaction(tmp); int transferred = _device.Execute(new XI2CDevice.I2CTransaction[] { write, read }, _readTimeout); if (transferred == (length + 1)) { Array.Copy(tmp, data, transferred - 1); return(transferred - 1); } return(-1); }
100 //clockrate in KHz ); I2CDevice device = new I2CDevice(config); //prepare buffer to write byte AA byte[] outBuffer = new byte[] { 0xAA }; I2CDevice.I2CWriteTransaction writeTransaction = device.CreateWriteTransaction(outBuffer); //prepare buffer to read four bytes byte[] inBuffer = new byte[4]; I2CDevice.I2CReadTransaction readTransaction = device.CreateReadTransaction(inBuffer); //execute both transactions I2CDevice.I2CTransaction[] transactions = new I2CDevice.I2CTransaction[] { writeTransaction, readTransaction }; int transferred = device.Execute(transactions, 100 //timeout in ms ); //transferred bytes should be 1 + 4 = 5 } } }