我正在使用Raspberry pi和java来使LED闪烁使用pi4j,一切都清除并正常工作,LED按照代码闪烁但是当我第二次运行时会导致以下错误,我有没有明确的答案如何解决它有很多相同的问题,任何帮助将不胜感激
final GpioController gpio = GpioFactory.getInstance();
final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(
RaspiPin.GPIO_01, "PinLED", PinState.HIGH);
System.out.println("light is: ON");
Thread.sleep(2000);
pin.low();
System.out.println("light is: OFF");
Thread.sleep(1000);
System.out.println("light is: ON for 1 second");
pin.pulse(1000, true);
pin.setShutdownOptions(true, PinState.LOW, PinPullResistance.OFF);
gpio.shutdown();
这是我得到的完整错误
com.pi4j.io.gpio.exception.GpioPinExistsException: This GPIO pin already exists: GPIO 1
com.pi4j.io.gpio.impl.GpioControllerImpl.provisionPin(GpioControllerImpl.java:507)
com.pi4j.io.gpio.impl.GpioControllerImpl.provisionDigitalOutputPin(GpioControllerImpl.java:645)
com.pi4j.io.gpio.impl.GpioControllerImpl.provisionDigitalOutputPin(GpioControllerImpl.java:672)
com.pi4j.io.gpio.impl.GpioControllerImpl.provisionDigitalOutputPin(GpioControllerImpl.java:684)
com.restFulService.Controller.LedControl.ledTestFun(LedControl.java:52)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:483)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:202)
net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:180)
org.apache.catalina.filters.CorsFilter.handleNonCORS(CorsFilter.java:439)
org.apache.catalina.filters.CorsFilter.doFilter(CorsFilter.java:178)
答案 0 :(得分:13)
你需要取消设置销钉。
在你调用.shutdown()后,你需要做以下
...
gpio.shutdown();
...
gpio.unProvisionPin(pin);
...
这应该会释放pi4j的内部参考,并允许您稍后重新配置