gpt4 book ai didi

java - Raspberry Pi MAX31865 Python 到 Java 的转换

转载 作者:行者123 更新时间:2023-12-01 22:20:23 26 4
gpt4 key购买 nike

我正在尝试将一段读取 MAX13865 传感器的 Python 代码转换为 Java。 Python 代码运行良好并返回预期的数字 (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 转换的尝试:

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

更新

只需添加一条评论,以防对其他人有所帮助。如果您关闭系统(以及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 版本的方式有关。

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

请注意,引脚编号不同。但现在两者给出了完全相同的结果。其他一切都很好。

实时调频:(

关于java - Raspberry Pi MAX31865 Python 到 Java 的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58595224/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com