Page 1 of 1

i2cdetect

Posted: Wed Sep 09, 2015 9:56 am
by dbrosy
Is there a tool for detecting devices connected to i2c bus?

I'm looking for something for windows iot and don't mind if its just c#

i have seen seen demo videos but no actual tools

cheers,

Damian

Re: i2cdetect

Posted: Wed Sep 09, 2015 1:26 pm
by haroldpulcher
I would head over to the windowsondevices.com site:

http://ms-iot.github.io/content/en-US/w ... stTool.htm

It is a tool that you need to compile and load onto the rpi that is a command line interface to the i2c stuff.

Pretty low level, but it may help.

Re: i2cdetect

Posted: Wed Sep 09, 2015 1:42 pm
by ricl

Re: i2cdetect

Posted: Wed Sep 09, 2015 4:32 pm
by DougieLawson
dbrosy wrote:Is there a tool for detecting devices connected to i2c bus?
The normal way you do it is to write to each address from 0x00 to 0x7F and see which devices give any reply.

Re: i2cdetect

Posted: Sun Sep 27, 2015 1:46 pm
by vidalsasoon
I didn't find one for C# so I made a primitive one. Basically it tries to read some data from each address and returns either "success" or "failed" for the hex address.

Code: Select all

		public async Task<bool> FindI2CAddressesAsync(Func<Message, Task> onMessageReceived)
		{
			string aqs = I2cDevice.GetDeviceSelector("I2C1");

			var i2CDevices = await DeviceInformation.FindAllAsync(aqs).AsTask().ConfigureAwait(false);
			if (i2CDevices.Count == 0)
			{
				await onMessageReceived(new Message("bus not found")).ConfigureAwait(false);
				return false;
			}

			for (byte slaveAddress = 0x00; slaveAddress <= 0x7F; slaveAddress++)
			{

				var settings = new I2cConnectionSettings(slaveAddress)
				{
					BusSpeed = I2cBusSpeed.FastMode,
					SharingMode = I2cSharingMode.Shared
				};

				foreach (var i2CDevice in i2CDevices)
				{
					var i2CDeviceDetails = await I2cDevice.FromIdAsync(i2CDevice.Id, settings).AsTask().ConfigureAwait(false);

					try
					{
						var testCommand = new byte[] { 0x00, 0x0 };
						i2CDeviceDetails.Read(testCommand);

						await onMessageReceived(new Message($"SlaveAddress: {settings.SlaveAddress:X} SUCCESS \n"));
					}
					catch(Exception)
					{
						await onMessageReceived(new Message($"SlaveAddress: {settings.SlaveAddress:X} FAILED \n"));
					}
				}
			}

			return false;
		}