[英]Java Run Shell Command on Raspberry Pi
I am trying to use the GPIO pins
on the Raspberry Pi
.我正在尝试使用
Raspberry Pi
上的GPIO pins
。 Currently I am using ProcessBuilder
to execute the commands.目前我正在使用
ProcessBuilder
来执行命令。 However, whenever I run it it always says:但是,每当我运行它时,它总是说:
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
Here is the code I am using:这是我正在使用的代码:
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();
Why is this happening?为什么会这样? Also, if there is a better way of doing this how?
另外,如果有更好的方法如何做到这一点?
Thanks谢谢
Edit: Seems like there are no more errors, but the commands are not running.编辑:似乎没有更多错误,但命令没有运行。 Could someone please look at my code to make sure i'm not being stupid?
有人可以看看我的代码以确保我不是愚蠢的吗?
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;
}
}
}
This question is old but here is how I got my pins to work using the WiringPi library.这个问题很老,但这是我如何使用WiringPi库让我的引脚工作。
Referring to this question.参考这个问题。
I have this code below which works when running in a SSH shell but not after the device reboots and runs as a cronjob after reboot.我下面有这段代码,它在 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();
}
}
To fix this basically I had to use String[]
and build the command.为了解决这个问题,我必须使用
String[]
并构建命令。 Very easy after struggling to find a fix在努力寻找修复之后很容易
Working Code example here:此处的工作代码示例:
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();
}
}
Here is how i do this in C++, I'm pretty sure you can translate to Java...这是我如何在 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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.