当前位置: 首页 > 工具软件 > Archive4J > 使用案例 >

树莓派使用pi4j运行Java文件出错误解决方法

何昆
2023-12-01

树莓派使用pi4j运行Java文件出错误解决方法

今天在树莓派上用Java写了一段代码,想让LED闪烁,具体代码如下:

System.out.println("<--pi4j--> GPIO Blink...");
        final GpioController gpio = GpioFactory.getInstance();

        final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(
                RaspiPin.GPIO_08
        );
        while (true) {
            pin.high();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            pin.low();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

把代码文件传到树莓派中,使用如下命令编译:

javac -encoding utf8 -classpath .:classes:/opt/pi4j/lib/'*' *

然后运行编译好的文件:

java -classpath .:classes:/opt/pi4j/lib/'*' test

发现树莓派报错:

Unable to determine hardware version. I see: Hardware   : BCM2835
,
 - expecting BCM2708 or BCM2709.
If this is a genuine Raspberry Pi then please report this
to projects@drogon.net. If this is not a Raspberry Pi then you
are on your own as wiringPi is designed to support the
Raspberry Pi ONLY.

在网上看issue了解到,因为pi4j是基于wiringpi的,最新的wiringpi已经支持BCM2835格式,然而树莓派当前的pi4j版本是1.1还没有包含最新的wiringpi特性。所以解决方法就是安装最新的还未正式发布的pi4j,点击如下链接下载:
pi4j-1.2-SNAPSHOT.deb,然后输入如下代码:

sudo dpkg -i pi4j-1.2-SNAPSHOT.deb

之后就可以了,最后贴上我在网上查到的结果:Hardware : BCM2835 error on RaspberryPi B+

 类似资料: