简体   繁体   English

Raspberry PI,Java和Pi4J控制GPIO无法获取当前的PinState

[英]Raspberry PI, Java and Pi4J Controlling GPIO cant get current PinState

Iám ciurrently Working on a Little Programm wich should check the current state of an explizit GPIO Pin and than toggle ist. 目前,在进行一点编程工作时,应检查漏洞利用GPIO引脚的当前状态,而不要切换ist。 For this iám using java and PI4J. 为此,我使用Java和PI4J。 When my Programm starts, the LED is turned of. 当我的程序启动时,LED熄灭。 But as soon as i get it as a variable it automaticly sets the state to HIGH. 但是,一旦我将其作为变量获取,它就会自动将状态设置为HIGH。 Dos anyone have an idea how to avoid this ? 有谁知道如何避免这种情况? my code so far: 到目前为止,我的代码:

final GpioController gpio = GpioFactory.getInstance();
GpioPinDigitalOutput led =gpio.provisionDigitalMultipurposePin(RaspiPin.GPIO_07,PinMode.DIGITAL_OUTPUT);


            PinState ledStatus = led.getState();
            if (ledStatus.isHigh())
            {
                led.setShutdownOptions(true, PinState.LOW);
                System.out.println("Set shutdownOption LOW");
            }
            else
            {
                led.setShutdownOptions(true, PinState.HIGH);
                System.out.println("Set shutdownOption HIGH");
            } 

         if(ledStatus.isHigh())
        {
            System.out.println("LEDS aus...");
        }
        else 
        {
            System.out.println("LEDS an...");
        }
        led.toggle();

This Works quite fine. 这很好。 But as i said, before i start the programm... the LED is off! 但是正如我所说,在我开始编程之前... LED熄灭了! as soon as i get to this line : GpioPinDigitalOutput led =gpio.provisionDigitalMultipurposePin(RaspiPin.GPIO_07,PinMode.DIGITAL_OUTPUT); 一旦我到达这一行:GpioPinDigitalOutput led = gpio.provisionDigitalMultipurposePin(RaspiPin.GPIO_07,PinMode.DIGITAL_OUTPUT); The LED is ON! LED亮! i need to figure out a way to get the state before the app is running , change it and then exit my programm with the LED toggled. 我需要找出一种方法来获取状态,然后再运行该应用程序,对其进行更改,然后通过切换LED退出我的程序。

Thanks for your Help :) 谢谢你的帮助 :)

try using the provisionDigitalMultipurposePin method to set first mode INPUT in order to read the state, and then switch to OUTPUT mode to change the pin state. 尝试使用ProvisionDigitalMultipurposePin方法设置第一模式INPUT以读取状态,然后切换到OUTPUT模式以更改引脚状态。

GpioController gpio = GpioFactory.getInstance();
GpioPinDigitalMultipurpose led = gpio.provisionDigitalMultipurposePin(RaspiPin.GPIO_07, PinMode.DIGITAL_INPUT);

// read state

led.setMode(PinMode.DIGITAL_OUTPUT);

// write state

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

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