简体   繁体   English

Raspberry Pi Java Pi4j gpio 对我不起作用,但可以使用 python

[英]Raspberry Pi Java Pi4j gpio isn't working for me, works with python though

I am an old java programmer, translating code from Desktop to Raspberry Pi, with the aim of embedding software in a hardware interface.我是一名老 Java 程序员,将代码从 Desktop 翻译成 Raspberry Pi,目的是将软件嵌入到硬件接口中。

I wired a 16*2 character LCD display , which worked with Python code, however when I use pi4j libraries to access the GPIO via Java, the screen is blank.我连接了一个16*2 字符的 LCD 显示器,它使用 Python 代码,但是当我使用pi4j 库通过 Java 访问 GPIO 时,屏幕是空白的。

Am I missing a some binary on/off switch?我错过了一些二进制开/关开关吗?

I am running pi4j 1.2 , on an A+ Pi , got over the 1.1 processor bug that affected wiring Pi.我在A+ Pi上运行pi4j 1.2 ,解决了影响 Pi 接线的 1.1 处理器错误。

Thanks for reading, any suggestions are appreciated.感谢您的阅读,任何建议表示赞赏。

import com.pi4j.component.lcd.LCDTextAlignment;
import com.pi4j.component.lcd.impl.GpioLcdDisplay;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.system.NetworkInfo;

public class LCD {

    public static void main(String args[]) {
        System.out.println("SYSTEM PRINT TEST");
        GpioController gpio = GpioFactory.getInstance();
        GpioLcdDisplay lcd = new GpioLcdDisplay(2,16,
            RaspiPin.GPIO_26,
            RaspiPin.GPIO_31,
            RaspiPin.GPIO_15,
            RaspiPin.GPIO_16,
            RaspiPin.GPIO_01,
            RaspiPin.GPIO_04);

        lcd.clear();
        Thread.sleep(1000);

        lcd.write(0, "LINE 1 TEST");
        lcd.write(1, "LINE 2 TEST");

        Thread.sleep(2000);
        gpio.shutdown();
    }
}

After spending several hours banging my head on the desk, I decided on this Google search花了几个小时在桌子上敲我的头后,我决定使用这个谷歌搜索

google.com >> raspberry pi java gpio not working python works google.com >> raspberry pi java gpio 不工作 python 工作

That led me to this question, which for some reason was down voted 3 times, but it was exactly what I was experiencing.这让我想到了这个问题,由于某种原因,该问题被否决了 3 次,但这正是我所经历的。

The second result of my search was this question:我搜索的第二个结果是这个问题:

Raspberry Pi4 with Pi4j Java 带有 Pi4j Java 的 Raspberry Pi4

That clued me in on the fact that I needed to add this line of code because pi4j uses a different PIN layout by default.这让我知道我需要添加这行代码,因为 pi4j 默认使用不同的 PIN 布局。

GpioFactory.setDefaultProvider(new RaspiGpioProvider(RaspiPinNumberingScheme.BROADCOM_PIN_NUMBERING));

That wasn't enough, but that question led me to this question/answer:这还不够,但这个问题让我想到了这个问题/答案:

Raspberry pi 4 controle GPIO with java 树莓派 4 用 java 控制 GPIO

Which explained I needed to update my gpio binary to v2.52这解释了我需要将我的 gpio 二进制文件更新到 v2.52


This sample Python code was working without issue:此示例 Python 代码运行正常:

https://maker.pro/raspberry-pi/projects/controlling-a-dc-motor-with-raspberry-pi4-1 https://maker.pro/raspberry-pi/projects/controlling-a-dc-motor-with-raspberry-pi4-1

import RPi.GPIO as GPIO
from time import sleep

# Pins for Motor Driver Inputs 
Motor1A = 20
Motor1B = 16
Motor1E = 21
 
def setup():
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)              # GPIO Numbering
    GPIO.setup(Motor1A,GPIO.OUT)  # All pins as Outputs
    GPIO.setup(Motor1B,GPIO.OUT)
    GPIO.setup(Motor1E,GPIO.OUT)
 
def loop():
    # Going forwards
    GPIO.output(Motor1A,GPIO.HIGH)
    GPIO.output(Motor1B,GPIO.LOW)
    GPIO.output(Motor1E,GPIO.HIGH)
    print("Going forwards")
 
    sleep(5)
    # Going backwards
    GPIO.output(Motor1A,GPIO.LOW)
    GPIO.output(Motor1B,GPIO.HIGH)
    GPIO.output(Motor1E,GPIO.HIGH)
    print("Going backwards")
 
    sleep(5)
    # Stop
    GPIO.output(Motor1E,GPIO.LOW)
    GPIO.output(Motor1B,GPIO.LOW)
    print("Stop")

def destroy():  
    GPIO.cleanup()

if __name__ == '__main__':     # Program start from here
    setup()
    try:
            loop()
    except KeyboardInterrupt:
        destroy()

But my attempts at using the Pi4J Java library were failing miserably.但是我尝试使用 Pi4J Java 库的尝试失败了。

This is my working Java code:这是我的工作 Java 代码:

//This line was initially not here, it is part of the solution
GpioFactory.setDefaultProvider(new RaspiGpioProvider(RaspiPinNumberingScheme.BROADCOM_PIN_NUMBERING));

gpio = GpioFactory.getInstance();
    
in2 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_16, "IN2", PinState.LOW);
in1 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_20, "IN1", PinState.LOW);
en = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_21, "EN", PinState.LOW);
    
//Going forwards
in1.high();
in2.low();
en.high();
   
Thread.sleep(5000);
       
//Going backwards
in1.low();
in2.high();
en.high();
    
Thread.sleep(5000);
    
// stop           
en.low();
in2.low();
    
gpio.shutdown();

The discrepancy between the pin numbering on this jargon code, and the original underlying wiringPi numbering was the cause of this frustration.此行话代码上的引脚编号与原始底层 WiringPi 编号之间的差异是造成这种挫败感的原因。 Here is the revised code, where gpio 25 corresponds to wiringPi 6, not 26!这是修改后的代码,其中gpio 25对应wiringPi 6,而不是26! Remember to update wiringPi and pi4j to the latest versions.记得更新wiringPi 和pi4j 到最新版本。

import com.pi4j.wiringpi.Gpio;
import com.pi4j.wiringpi.Lcd;

public class LCD {

    public final static int LCD_ROWS = 2;
    public final static int LCD_COLUMNS = 16;
    public final static int LCD_BITS = 4;

    public static  void main(String args[]) {

        System.out.println("SYSTEM PRINT TEST");

        if (Gpio.wiringPiSetup() == -1) {
            System.out.println("GPIO SETUP ERROR");
            return;
        }

        int lcdHandle= Lcd.lcdInit(LCD_ROWS,
                                   LCD_COLUMNS,
                                   LCD_BITS,
                                   6,
                                   5,
                                   15,
                                   16,
                                   1,
                                   4,
                                   0,
                                   0,
                                   0,
                                   0);

        if (lcdHandle == -1) {
            System.out.println("LCD INIT ERROR");
            return;
        }

        Lcd.lcdDisplay(lcdHandle,1);
        Lcd.lcdClear(lcdHandle);

        Lcd.lcdPuts (lcdHandle, "LCD TEST LINE 1") ;

        Lcd.lcdPosition (lcdHandle, 0, 1) ; 
        Lcd.lcdPuts (lcdHandle, "LCD TEST LINE 2") ;

try {
     Thread.sleep(10000);
    } catch (Exception e) {}
     Lcd.lcdDisplay(lcdHandle,0);  

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM