Reputation: 49
I'm trying the new System.Device.Gpio
library in .NET Core (https://github.com/dotnet/iot) with my Raspberry PI 3B+, but the I2C communication is giving me some trouble.
I want to reproduce the behavior of some python code that is already working, starting with some simple data writing and reading:
I2cConnectionSettings settings = new I2cConnectionSettings(1, 0x18); // I2C BUS is 1, ADDRESS is 0x18
I2cDevice device = I2cDevice.Create(settings);
device.WriteByte(8);
Console.WriteLine("Byte written: " + device.ReadByte().ToString());
The result is giving me is always 0
, without any error, while the expected result should be 8
.
What could it be?
Upvotes: 4
Views: 1481
Reputation: 3001
On the raspberry make sure you enabled I2C in raspi-config (sudo raspi-config, Interface Options, Enable/Disable autom... I2C kernel module)
using System.Device.I2c; // Manage Nuget Packages..
The "DeviceAddress" of the I2cConnectionSettings is the address of the "Slave" you are writing a single byte to with I2cDevice.WriteByte. The Slave device e.g. an Arduino receives this byte in it's receive data event. This can be used for sending commands to the device.
When you call device.ReadByte you actually send a request to the device to answer with data. An Arduino Slave for example would get this request in it's request data event and could send back something (int the case above one byte). This should be something different than 0 since 0 is also returned by the function if no data was read.
Upvotes: 1