我正在尝试将读取 MAX13865 传感器的一段 Python 代码转换为 Java。Python 代码运行良好并返回预期的数字 (1238),而 Java 版本始终返回 32767。为了简化阅读,我将 Python 代码和 Java 代码减少到最小。下面的 Python 代码仍然运行得很好。我缺少什么?看起来很简单,但是还是不行……#!/usr/bin/python -ttimport RPi.GPIO as GPIOimport timeimport datetimeimport mathclass 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)
1 回答
天涯尽头无女友
TA贡献1831条经验 获得超9个赞
问题与 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);
请注意,引脚编号不同。但现在两者给出了完全相同的结果。其他一切都很好。
实时调频:(
添加回答
举报
0/150
提交
取消