简体   繁体   English

Raspberry PI - I2C 不写入数据

[英]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.我正在使用 Raspberry PI 3B+ 尝试 .NET Core ( https://github.com/dotnet/iot ) 中的新System.Device.Gpio库,但 I2C 通信给我带来了一些麻烦。

I want to reproduce the behavior of some python code that is already working, starting with some simple data writing and reading:我想重现一些已经在工作的 python 代码的行为,从一些简单的数据写入和读取开始:

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 .结果给我总是0 ,没有任何错误,而预期的结果应该是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)在树莓派上确保您在 raspi-config 中启用了 I2C(sudo raspi-config,接口选项,启用/禁用自动... I2C 内核模块)

  2. using System.Device.I2c;使用 System.Device.I2c; // Manage Nuget Packages.. // 管理 Nuget 包..

  3. The "DeviceAddress" of the I2cConnectionSettings is the address of the "Slave" you are writing a single byte to with I2cDevice. I2cConnectionSettings 的“DeviceAddress”是您使用 I2cDevice 写入单个字节的“Slave”的地址。 WriteByte .写字节 The Slave device eg an Arduino receives this byte in it's receive data event.设备(例如 Arduino)在其接收数据事件中接收该字节。 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. ReadByte您实际上向设备发送请求以回答数据。 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).例如,一个 Arduino Slave会在它的请求数据事件中收到这个请求,并且可以发回一些东西(在一个字节以上的情况下)。 This should be something different than 0 since 0 is also returned by the function if no data was read.这应该与 0 不同,因为如果没有读取数据,函数也会返回 0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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