将Python代码转换为javacod

2024-04-29 09:58:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要一些将python代码翻译成java的帮助。我是Python新手,我不知道所有的函数/方法等。我使用这个Python代码从Raspberry Pi上的2个传感器读取一些数据,我需要用Java来获取这些数据。在

下面是Python代码:

#!/usr/bin/python

import spidev
import time
import os

# Open SPI bus
spi = spidev.SpiDev()
spi.open(0,0)

# Function to read SPI data from MCP3008 chip
# Channel must be an integer 0-7
def ReadChannel(channel):
  adc = spi.xfer2([1,(8+channel)<<4,0])
  data = ((adc[1]&3) << 8) + adc[2]
  return data

# Function to convert data to voltage level,
# rounded to specified number of decimal places.
def ConvertVolts(data,places):
  volts = (data * 3.3) / float(1023)
  volts = round(volts,places)
  return volts

# Function to calculate temperature from
# TMP36 data, rounded to specified
# number of decimal places.
def ConvertTemp(data,places):

  # ADC Value
  # (approx)  Temp  Volts
  #    0      -50    0.00
  #   78      -25    0.25
  #  155        0    0.50
  #  233       25    0.75
  #  310       50    1.00
  #  465      100    1.50
  #  775      200    2.50
  # 1023      280    3.30

  temp = ((data * 330)/float(1023))-50
  temp = round(temp,places)
  return temp

# Define sensor channels
light_channel = 0
temp_channel  = 1

# Define delay between readings
delay = 5

while True:

  # Read the light sensor data
  light_level = ReadChannel(light_channel)
  light_volts = ConvertVolts(light_level,2)

  # Read the temperature sensor data
  temp_level = ReadChannel(temp_channel)
  temp_volts = ConvertVolts(temp_level,2)
  temp       = ConvertTemp(temp_level,2)

  # Print out results
  print "--------------------------------------------"
  print("Light: {} ({}V)".format(light_level,light_volts))
  print("Temp : {} ({}V) {} deg C".format(temp_level,temp_volts,temp))

  # Wait before repeating loop
  time.sleep(delay)

这是我尝试过的Java代码(我使用的是Pi4j库):

^{pr2}$

我想知道这个java代码是正常的还是需要修改。在

这是我继续跑步的结果:

Exception in thread "main" java.lang.NullPointerException
    at MCP3008_TMP36.channelReading(MCP3008_TMP36.java:80)
    at MCP3008_TMP36.main(MCP3008_TMP36.java:44)

我不确定“频道阅读”功能是否正常。请帮帮我。在

非常感谢!在


Tags: to代码importspidatachannelfunctionjava
1条回答
网友
1楼 · 发布于 2024-04-29 09:58:39

这是java代码的问题,与Python无关。在

看看你的错误

Exception in thread "main" java.lang.NullPointerException at MCP3008_TMP36.channelReading(MCP3008_TMP36.java:80) at MCP3008_TMP36.main(MCP3008_TMP36.java:44)

您应该查看代码中的第80行,即

chipSelectOutput.high();

方法内

private static int channelReading(MCP3008_channels channel) {

从主方法的第44行调用

float lightLevel = channelReading(MCP3008_channels.CH0);

因此,您正在调用方法chipSelectOutput.high(),但是看看其余代码,我们有

private static GpioPinDigitalOutput chipSelectOutput = null;

而且您从未为这个变量创建/分配对象,因此当您从中调用方法时,chipSelectOutput仍然具有值null。这就是为什么你得到了一个空指针异常。在

在执行任何其他操作之前,您需要在代码中的某个位置创建一个chipSelectOutput对象,以便有一个对象可以调用函数。在

下面是一个如何初始化静态成员的示例:

static variable initialization java

private static final B a = new B(); // consider making it final too

如果这是可行的,那么您将因为同样的原因得到相同类型的错误,对于您的其他对象clockOutputmosiOutput和{},这些对象在任何地方都没有初始化(给定值)。在

相关问题 更多 >