Raspberry Pi MAX31865 Python到Java的转换
我正在尝试将一段读取MAX13865传感器的Python代码转换为Java。Python代码运行良好并返回预期的数字(1238),而Java版本总是返回32767。为了简化阅读,我将Python代码和Java代码缩减到最小。下面的Python代码仍然运行良好。我错过了什么?这似乎很简单,但仍然不起作用Raspberry Pi MAX31865 Python到Java的转换,java,python,raspberry-pi,sensors,Java,Python,Raspberry Pi,Sensors,我正在尝试将一段读取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 MAX3
#!/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()
这个问题与Pi4J将GPIO引脚号映射到Python版本的方式有关 Python版本:
MAX31865(8, 11, 9, 10)
和java等价物:
MAX31865 max = new MAX31865(RaspiPin.GPIO_10, RaspiPin.GPIO_14, RaspiPin.GPIO_13, RaspiPin.GPIO_12);
请注意,pin码是不同的。但现在两者给出了完全相同的结果。其他一切都很好
RTFM:(请检查
get_data
中读取的值(msb
,lsb
,msb
,lsb
)在java版本中,msb和lsb的值都是255。在Python版本中,我得到75和69(结果是1264,即68.22C)嗯,好的。那么,当你在写地址时,请检查每次迭代中的bit
,当你进行位移位时,请检查bytesin
。地址正确吗?在Python中:[code]0 0 0 0 0 0 0 1 0 0 0 2 6 30 60 0 0 0 0 0 0 0 1 0 0 2 6 28 56[code]在Java中:[code]0 0 0 0 0 0 1 0 0 0 2 6 14 30 60 0 0 0 0 0 0 0 0 1 0 0 2 6 14 28 56[代码]地址似乎很好,但读取结果似乎错误…当我打印GPIO.input(自我数据输入)时我明白了:0 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
MAX31865(8, 11, 9, 10)
MAX31865 max = new MAX31865(RaspiPin.GPIO_10, RaspiPin.GPIO_14, RaspiPin.GPIO_13, RaspiPin.GPIO_12);