我正在尝试将读取 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)
天涯尽头无女友
相关分类