简体   繁体   中英

Java function to toggle socket state via raspberry pi

I have a function in java which is being executet on my raspberry pi and should send a signal to toggle the targeted sockets state to on / off.

Thats my current function:

    public static void rcswitch(int housecode,int unitcode, int onoff) throws InterruptedException, IOException {
    String housestring = Integer.toString(housecode);
    String unitstring = Integer.toString(unitcode);
    String onoffstring = Integer.toString(onoff);

    ProcessBuilder builder = new ProcessBuilder("/bin/bash", "-c", "sudo /home/pi/rcswitch-pi/send", housestring, unitstring, onoffstring);
    Process proc = builder.start();

    BufferedReader reader = 
            new BufferedReader(new InputStreamReader(proc.getInputStream()));
          String line = "";
          while((line = reader.readLine()) != null) {
               System.out.print(line + "\n");
          }
}

However, it doesn't seem like the terminal is receiving the command as it does not output anything. It should show something like "command received" and execute it then. When I normally execute the /send command in the terminal it works just fine. In eclipse it just works fine and throws the expected error.

Thanks for your answers :)

It is most likely that an error has occured while executing the command. Keep in mind that Process#getInputStream() does not include standard error stream of the process. You should use Process#getErrorStream() . Something like:

BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
String line = null;
while((line = reader.readLine()) != null) {
    System.out.print(line + "\n");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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