简体   繁体   English

Java函数通过树莓派切换套接字状态

[英]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. 我在Java中有一个函数正在树莓派上执行,并且应该发送信号将目标套接字状态切换为开/关。

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. 当我在终端中正常执行/ send命令时,它可以正常工作。 In eclipse it just works fine and throws the expected error. 在eclipse中,它可以正常工作并抛出预期的错误。

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. 请记住, Process#getInputStream()不包括该进程的标准错误流。 You should use Process#getErrorStream() . 您应该使用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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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