简体   繁体   中英

Raspberry PI - I2C not writing data

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?

  1. On the raspberry make sure you enabled I2C in raspi-config (sudo raspi-config, Interface Options, Enable/Disable autom... I2C kernel module)

  2. using System.Device.I2c; // Manage Nuget Packages..

  3. The "DeviceAddress" of the I2cConnectionSettings is the address of the "Slave" you are writing a single byte to with I2cDevice. WriteByte . The Slave device eg an Arduino receives this byte in it's receive data event. This can be used for sending commands to the device.

  4. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM