Raspberry Pi上的Java Run Shell命令

时间:2014-09-05 10:10:47

标签: java raspberry-pi gpio

我正在尝试使用GPIO pins上的Raspberry Pi。目前我正在使用ProcessBuilder来执行命令。但是,无论何时我运行它总是说:

Exception in thread "main" java.io.IOException: Cannot run program "echo 18 > /sys/class/gpio/export": error=2, No such file or directory
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
        at Testing.initiate(Testing.java:32)
        at Testing.main(Testing.java:8)
Caused by: java.io.IOException: error=2, No such file or directory
        at java.lang.UNIXProcess.forkAndExec(Native Method)
        at java.lang.UNIXProcess.<init>(UNIXProcess.java:135)
        at java.lang.ProcessImpl.start(ProcessImpl.java:130)
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1022)
        ... 2 more

以下是我正在使用的代码:

    ProcessBuilder pb = new ProcessBuilder("echo " + gpiopin + " > /sys/class/gpio/export");
    ProcessBuilder pb1 = new ProcessBuilder("echo out > /sys/class/gpio/gpio" + gpiopin + "/direction");
    ProcessBuilder pb2 = new ProcessBuilder("echo 1 > /sys/class/gpio/gpio" + gpiopin + "/value");
    pb.redirectErrorStream(true);
    pb1.redirectErrorStream(true);
    pb2.redirectErrorStream(true);
    Process ps = pb.start();
    Process ps1 = pb1.start();
    Process ps2 = pb2.start();

为什么会这样?另外,如果有更好的方法可以做到这一点?

由于

编辑:似乎没有更多错误,但命令没有运行。有人可以看看我的代码,以确保我不是傻瓜吗?

import java.util.Scanner;


public class Testing {
    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(System.in);
        initiate(18);

        while(true) {
            String input = scan.nextLine();

            if(input.equals("on")) {
                System.out.println("on");
                bellOn();
            }else if(input.equals("off")) {
                System.out.println("off");
                bellOff();
            }
        }
    }

    static int gpio = 18;
    static String gp = "" + gpio;
    static boolean initiated = false;

    public static boolean initiate(int gpiopin) throws Exception {
        gpio = gpiopin;
        ProcessBuilder pb = new ProcessBuilder("echo", gp, ">", "/sys/class/gpio/export");
        ProcessBuilder pb1 = new ProcessBuilder("echo", "out", ">", "/sys/class/gpio/gpio" + gp + "/direction");
        //ProcessBuilder pb2 = new ProcessBuilder("echo", "1", ">", "/sys/class/gpio/gpio" + gpiopin + "/value");
        pb.redirectErrorStream(true);
        pb1.redirectErrorStream(true);
        //pb2.redirectErrorStream(true);
        Process ps = pb.start();
        Process ps1 = pb1.start();
        //Process ps2 = pb2.start();
        initiated = true;
        return true;
    }

    public static boolean bellOn() throws Exception {
        if(initiated != true) {
            return false;
        }else{
            ProcessBuilder pb = new ProcessBuilder("echo", "0", ">", "/sys/class/gpio/gpio" + gp + "/value");
            pb.redirectErrorStream(true);
            Process ps = pb.start();
            return true;
        }
    }

    public static boolean bellOff() throws Exception {
        if(initiated != true) {
            return false;
        }else{
            ProcessBuilder pb = new ProcessBuilder("echo", "1", ">", "/sys/class/gpio/gpio" + gp + "/value");
            pb.redirectErrorStream(true);
            Process ps = pb.start();
            return true;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

这个问题很老,但这里是我如何使用 WiringPi 库让我的图钉工作。

参考 this 问题。

我下面有这段代码,它在 SSH shell 中运行时有效,但在设备重启后无效,并在重启后作为 cronjob 运行。

public static String cmdCommands()
{
    try 
    {
        //blink LED; works in shell only
        Runtime r = Runtime.getRuntime();
        r.exec("gpio mode 4 out");
        r.exec("gpio write 4 1");
        Thread.sleep(500);
        r.exec("gpio write 4 0");
        Thread.sleep(500);
        return "done";          
    }
    catch(Exception e) 
    {
        return "Exception: " + e.getMessage();
    }
}

为了解决这个问题,我必须使用 String[] 并构建命令。在努力寻找修复之后很容易

此处的工作代码示例:

public static String cmdCommands()
{
    try 
    {
        //blink LED
        Runtime r = Runtime.getRuntime();
        r.exec(new String[] {"sudo", "gpio", "mode", "4", "out"});
        r.exec(new String[] {"sudo", "gpio", "write", "4", "1"});
        Thread.sleep(500);
        r.exec(new String[] {"sudo", "gpio", "write", "4", "0"});
        return "done";          
    }
    catch(Exception e) 
    {
        return "Exception: " + e.getMessage();
    }
}

答案 1 :(得分:-1)

以下是我在C ++中的表现,我非常确定你可以翻译成Java ......

void ResetCommand::execute() {
    // Perform a hardware reset on the Arduino using GPIO pins
    ofstream fd;
    char buf[255];

    cout << "Exporting GPIO Pin 8" << endl;
    fd.open("/sys/class/gpio/export", ios::out);
    fd << "8";
    fd.close();

    // Set out direction
    cout << "Setting as output" << endl;
    fd.open("/sys/class/gpio/gpio8/direction", ios::out);
    fd << "out";
    fd.close();

    // Blip the reset pin
    cout << "Resetting... " << endl;
    fd.open("/sys/class/gpio/gpio8/value", ios::out);
    fd << 0;
    usleep(1);
    fd << 1;
    fd.close();

    // Finished, so free up the pin
    cout << "Releasing " << endl;
    fd.open("/sys/class/gpio/unexport", ios::out);
    fd << "8";
    fd.close();
}