日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

本文介紹了Raspberry Pi MAX31865從Python到Java的轉(zhuǎn)換的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在嘗試將讀取MAX13865傳感器的一段Python代碼轉(zhuǎn)換為Java。Python代碼運行良好,返回一個預(yù)期的數(shù)字(1238),而Java版本總是返回32767。為了簡化閱讀,我將Python代碼和Java代碼減少到最小。下面的Python代碼仍然運行得很好。我遺漏了什么?它看起來很簡單,但仍然不起作用…

#!/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()

和我的Java轉(zhuǎn)換設(shè)想:

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);
    }
  }
}

更新

只需添加一條評論,以防它可以幫助其他人。如果您關(guān)閉系統(tǒng)電源(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

推薦答案

問題與Pi4J映射GPIO端口號與PYTHON版本的方式有關(guān)。

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

Python版本:

MAX31865(8, 11, 9, 10)

和Java等效項:

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

請注意,PIN號不同。但現(xiàn)在兩者得出了完全相同的結(jié)果。其他一切工作正常。

RTFM:(

這篇關(guān)于Raspberry Pi MAX31865從Python到Java的轉(zhuǎn)換的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,

分享到:
標(biāo)簽:Java MAX31865 Pi Python Raspberry 轉(zhuǎn)換
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運動步數(shù)有氧達(dá)人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定