【问题标题】:How can read more bytes via I2c如何通过 I2c 读取更多字节
【发布时间】:2017-05-12 14:36:11
【问题描述】:

我正在寻找有关如何通过 I2C 在 Raspberry Pi 上读取超过 1 个字节的示例。我只看到这个,但仅适用于发送的第一个字节:

package i2crpiarduino;

import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CDevice;
import com.pi4j.io.i2c.I2CFactory;

import java.io.IOException;

import java.text.DecimalFormat;
import java.text.NumberFormat;


public class Arduino
{
  public final static int ARDUINO_ADDRESS = 0x04; // See RPi_I2C.ino
  private static boolean verbose = "true".equals(System.getProperty("arduino.verbose", "false"));

  private I2CBus bus;
  private I2CDevice arduino;

  public Arduino() throws I2CFactory.UnsupportedBusNumberException
  {
    this(ARDUINO_ADDRESS);
  }

  public Arduino(int address) throws I2CFactory.UnsupportedBusNumberException
  {
    try
    {
      // Get i2c bus
      bus = I2CFactory.getInstance(I2CBus.BUS_1); // Depends onthe RasPI version
      if (verbose)
        System.out.println("Connected to bus. OK.");

      // Get device itself
      arduino = bus.getDevice(address);
      if (verbose)
        System.out.println("Connected to device. OK.");
    }
    catch (IOException e)
    {
      System.err.println(e.getMessage());
    }
  }

  public void close()
  {
    try { this.bus.close(); }
    catch (IOException ioe) { ioe.printStackTrace(); }    
  }

  /*
   * methods readArduino, writeArduino
   * This where the communication protocol would be implemented.
   */
  public int readArduino()
    throws Exception
  {
    int r  = arduino.read();
    return r;
  }

  public void writeArduino(byte b)
    throws Exception
  {
    arduino.write(b);
  }

  private static void delay(float d) // d in seconds.
  {
    try { Thread.sleep((long)(d * 1000)); } catch (Exception ex) {}
  }

  public static void main(String[] args) throws I2CFactory.UnsupportedBusNumberException
  {
    final NumberFormat NF = new DecimalFormat("##00.00");
    Arduino sensor = new Arduino();
    int read = 0;

    while(true) 
   {
      try
      {
        read = sensor.readArduino();
      }
      catch (Exception ex)
      {
        System.err.println(ex.getMessage());
        ex.printStackTrace();
      }

      System.out.println("Read: " + NF.format(read));
      delay(1);
    }

  }
}

目标:
我通过 I2C 从我的 Arduino 板向 Raspberry Pi 发送了一个 long 号码,但我试图让这个示例工作但没有成功。如何获取从树莓派读取的long 号码?

在 javadoc 中我看到了这个:

public int read(byte[] buffer, int offset, int size) throws IOException
This method reads bytes directly from the i2c device to given buffer at asked offset.
Parameters:
buffer - buffer of data to be read from the i2c device in one go offset - offset in buffer size - number of bytes to be read 
Returns:
number of bytes read 
Throws:
IOException - thrown in case byte cannot be read from the i2c device or i2c bus

如何使用它?

【问题讨论】:

    标签: java arduino raspberry-pi i2c pi4j


    【解决方案1】:

    I2C 在“从属”系统下工作,您必须在其中发送字节以确认您想要从中获取数据的连接设备,并且设备会做出相应的响应。正在使用的读取方法不发送标志字节,因此永远不会取回数据; I2C 是一种按需通信方式。

    这里是 I2C 的概述:I2C bus protocol Tutorial, Interface with applications

    【讨论】:

      猜你喜欢
      • 2012-01-19
      • 2020-11-04
      • 2014-09-17
      • 2022-08-13
      • 1970-01-01
      • 1970-01-01
      • 2020-09-13
      • 2012-03-09
      • 2012-03-03
      相关资源
      最近更新 更多