简体   繁体   English

树莓派 MAX31865 Python 到 Java 转换

[英]Raspberry Pi MAX31865 Python to Java conversion

I'm trying to convert a piece of Python code reading MAX13865 sensor into Java.我正在尝试将一块读取 MAX13865 传感器的 Python 代码转换为 Java。 The Python code works well and return an expected number (1238), while the Java version always returns 32767. To simplify the reading I reduced the Python code and the Java code to the smallest. The Python code works well and return an expected number (1238), while the Java version always returns 32767. To simplify the reading I reduced the Python code and the Java code to the smallest. The Python code below still works very well.下面的 Python 代码仍然运行良好。 What am I missing?我错过了什么? It seems to be very simple, but still doesn't work...这似乎很简单,但仍然不起作用......

#!/usr/bin/python -tt

import RPi.GPIO as GPIO
import time
import datetime
import math

class MAX31865(object):

    def __init__(self, cs_pin, clock_pin, data_in_pin, data_out_pin, board = GPIO.BCM):

        self.cs_pin = cs_pin
        self.clock_pin = clock_pin
        self.data_in_pin = data_in_pin
        self.data_out_pin = data_out_pin
        self.board = board

        # Initialize needed GPIO
        GPIO.setmode(self.board)
        GPIO.setup(self.cs_pin, GPIO.OUT)
        GPIO.setup(self.clock_pin, GPIO.OUT)
        GPIO.setup(self.data_in_pin, GPIO.IN)
        GPIO.setup(self.data_out_pin, GPIO.OUT)

        # Pull chip select high to make chip inactive
        GPIO.output(self.cs_pin, GPIO.HIGH)

    def get_data(self):
        '''Acqures raw RDT data.'''
        self.address = int(0x01)    #RTD MSBs
        MSB = self.read()
        self.address = int(0x02)    #RTD LSBs
        LSB = self.read()
        MSB = MSB<<8
        raw = MSB+LSB
        raw = raw>>1
        return raw

    def read(self):
        '''Reads 16 bits of the SPI bus from a self.address register & stores as an integer in self.data.'''
        bytesin = 0

        # Select the chip
        GPIO.output(self.cs_pin, GPIO.LOW)
        # Assert clock bit
        GPIO.output(self.clock_pin, GPIO.LOW)

        # Write to address
        for i in range(8):
            bit  = self.address>>(7 - i)
            bit = bit & 1
            GPIO.output(self.data_out_pin, bit)
            GPIO.output(self.clock_pin, GPIO.HIGH)
            GPIO.output(self.clock_pin, GPIO.LOW)

        # Read in 8 bits
        for i in range(8):
            GPIO.output(self.clock_pin, GPIO.HIGH)
            bytesin = bytesin << 1
            if (GPIO.input(self.data_in_pin)):
                bytesin = bytesin | 1
            GPIO.output(self.clock_pin, GPIO.LOW)
        # Dsable clock
        GPIO.output(self.clock_pin, GPIO.HIGH)
        # Unselect the chip
        GPIO.output(self.cs_pin, GPIO.HIGH)

        # Save data
        self.data = bytesin
        return self.data

if __name__ == "__main__":
    cs_pin = 8
    clock_pin = 11
    data_in_pin = 9
    data_out_pin = 10

    # Configure RTDs
    rtd = MAX31865(cs_pin, clock_pin, data_in_pin, data_out_pin)
    log_string = ''

    # Run main loop
    running = True
    while(running):
        try:
            RTD_code = rtd.get_data()
            print '{:.0f}'.format(RTD_code * 4300 / 32768)
            time.sleep(0.1)
        except KeyboardInterrupt:
            running = False
    GPIO.cleanup()

And my tentative of Java conversion:以及我对 Java 转换的暂定:

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.Pin;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.wiringpi.Gpio;

public class MAX31865 {

  protected Pin cs_pin;
  protected Pin clock_pin;
  protected Pin data_in_pin;
  protected Pin data_out_pin;

  protected GpioPinDigitalOutput cs_pin_out;
  protected GpioPinDigitalOutput clock_pin_out;
  protected GpioPinDigitalInput data_in_pin_in;
  protected GpioPinDigitalOutput data_out_pin_out;

  protected int address;
  protected int data;
  final GpioController gpio = GpioFactory.getInstance();

  public MAX31865(Pin cs_pin, Pin clock_pin, Pin data_in_pin, Pin data_out_pin, int address, int data) {
    this.cs_pin = cs_pin;
    this.clock_pin = clock_pin;
    this.data_in_pin = data_in_pin;
    this.data_out_pin = data_out_pin;
    this.address = address;
    this.data = data;

    // Initialize needed GPIO
    cs_pin_out       = gpio.provisionDigitalOutputPin(cs_pin, "SPI_CS");
    clock_pin_out    = gpio.provisionDigitalOutputPin(clock_pin, "SPI_CLCK");
    data_in_pin_in   = gpio.provisionDigitalInputPin(data_in_pin, "SPI_IN");
    data_out_pin_out = gpio.provisionDigitalOutputPin(data_out_pin, "SPI_OUT");

    // Pull chip select high to make chip inactive
    cs_pin_out.setState(PinState.HIGH);
  }

  /**
   * Reads 16 bits of the SPI bus from an address register & stores as an integer in data.
   * @return
   * @throws InterruptedException 
   */
  public int read() throws InterruptedException {
    int bytesin = 0;

    // Select the chip
    cs_pin_out.setState(PinState.LOW);
    // Assert clock bit
    clock_pin_out.setState(PinState.LOW);

    // Write to address
    for (int i = 0; i < 8; i++) {
      byte bit = (byte) ((address >> (7 - i)) & 1);
      data_out_pin_out.setState(bit == 1);
      clock_pin_out.setState(PinState.HIGH);
      clock_pin_out.setState(PinState.LOW);
    }

    // Read in 8 bits
    for (int i = 0; i < 8; i++) {
      clock_pin_out.setState(PinState.HIGH);
      bytesin = bytesin << 1;
      if (data_in_pin_in.getState() == PinState.HIGH) {
        bytesin = bytesin | 1;
      } else {
        System.out.println("Is low!");
      }
      clock_pin_out.setState(PinState.LOW);
    }

    // Disable Clock
    clock_pin_out.setState(PinState.HIGH);
    // Unselect the chip
    cs_pin_out.setState(PinState.HIGH);
    Thread.sleep(1);

    // Save data
    data = bytesin;
    return data;
  }

  public int getData() throws InterruptedException {
    address = 0x01;
    int msb = read();
    address = 0x02;
    int lsb = read();
    System.out.println(msb + " " + lsb);
    msb = msb << 8;
    int raw = msb + lsb;
    raw = raw >> 1;
    return raw;
  }

  public static void main(String args[]) throws InterruptedException {
    MAX31865 max = new MAX31865(RaspiPin.GPIO_08, RaspiPin.GPIO_11, RaspiPin.GPIO_09, RaspiPin.GPIO_10, (int)0x80, (int)0xC2);
    // max.write();
    while (true) {
      System.out.println(max.getData());
      Thread.sleep(100);
    }
  }
}

Update更新

Just adding one comment in case it can help others.只需添加一条评论,以防它可以帮助其他人。 If you power-off the system (and so the MAX31865) it will stop working back until you WRITE to it.如果您关闭系统(以及 MAX31865),它将停止工作,直到您写入它。

    # Configure RTDs
    rtds = []
    address = int(0x80)    # RTD control register, see datasheet for details
    data =  int(0xC2)      # RTD condrol register data, see datasheet for details
    for cs_pin in cs_pins:
        rtds.append(MAX31865(cs_pin, clock_pin, data_in_pin, data_out_pin, address, data))
    print rtds
    for rtd in rtds:
        rtd.write()
    print rtd

Issue was related to the way Pi4J maps the GPIO pin numbers vs the Python version.问题与 Pi4J 映射 GPIO 引脚号与 Python 版本的方式有关。

https://www.pi4j.com/1.2/pins/model-3b-rev1.html https://www.pi4j.com/1.2/pins/model-3b-rev1.html

The Python version: Python 版本:

MAX31865(8, 11, 9, 10)

And the java equivalent:和 java 等效:

MAX31865 max = new MAX31865(RaspiPin.GPIO_10, RaspiPin.GPIO_14, RaspiPin.GPIO_13, RaspiPin.GPIO_12);

Notice that the pin numbers are different.请注意,引脚编号不同。 But both give the exact same result now.但是现在两者都给出了完全相同的结果。 Everything else works just fine.其他一切都很好。

RTFM:(实时调频:(

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

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