See the IO Pins page here for any extensions needed by Visual Studio and to enable the IO pins.

Namespaces

using Windows.Devices.Enumeration;
using Windows.Devices.I2c;

Initialise I2C Port For A Device

Write Bytes

	byte[] I2cWriteBuffer;

	//Write to I2C device
	I2cWriteBuffer = new byte[] { 0x01, 0x02 };
	OurI2cPort.Write(I2cWriteBuffer);						//Writes I2cWriteBuffer.Length bytes

	//Another way:
	OurI2cPort.Write(new byte[] { 0x01, 0x02 });						//Writes I2cWriteBuffer.Length bytes

Write Then Read Bytes

	byte[] I2cWriteBuffer;
	byte[] I2cReadBuffer;

	//Write then read I2C device
	I2cWriteBuffer = new byte[] {0x01, 0x02};
	I2cReadBuffer = new byte[3];
	OurI2cPort.WriteRead(I2cWriteBuffer, I2cReadBuffer);    //Writes I2cWriteBuffer.Length bytes, then does a restart and reads I2cReadBuffer.Length bytes

	//Another way:
	I2cReadBuffer = new byte[1];
	OurI2cPort.WriteRead(new byte[] { 0x00 }, I2cReadBuffer);
	// = I2cReadBuffer;

Read Bytes

	byte[] I2cReadBuffer;

	//Just read from I2C device
	I2cReadBuffer = new byte[4];
	OurI2cPort.Read(I2cReadBuffer);                         //Reads I2cReadBuffer.Length bytes

Disposing Of A I2C Device Object

	OurI2cPort.Dispose();

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *