1

因此,我正在尝试连接到将要获取模拟数据的设备(Red Pitaya)。它设置了控制设备的 scpi 命令。我可以通过labview和使用腻子来控制这些。

我正在尝试编写可以访问设备 scpi 服务器并向其发送命令以完成设备的 android 应用程序。

该设备的编程方式是,您首先必须使用 SSH 连接连接到服务器,我使用 JSch 没有问题,从那里您可以发送命令来启动 scpi 服务器并打开连接。

现在这是我正在努力解决的问题,我不明白为什么,当 SCPI 服务器启动时,它是通过设备的 Ip 和 5000 的原始端口访问的,但我似乎无法编写一段代码连接到这并执行 SCPI 命令。我不确定是连接还是我发送数据的方式。

这是代码

public class rp_command extends AsyncTask<Void,Void, Void> {
String ipAddress;
int port;
String response = "";
TextView textResponse;


rp_command(String address, int rp_port, TextView textResponse){
    ipAddress = address;
    port = rp_port;
    this.textResponse = textResponse;

}
@Override
protected Void doInBackground(Void...arg0){
    Socket socket = null;

    try{
        socket = new Socket(ipAddress, port);   
        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
        out.writeByte(1);
        out.writeUTF("DIG:PIN LED2,1");
        out.flush();


    } catch (UnknownHostException e){
        e.printStackTrace();
        response = "UnknownHostException:" + e.toString();
    } catch (IOException e){
        e.printStackTrace();
        response = "IOException:" + e.toString();
    } finally {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }return null;


}
@Override
protected void onPostExecute(Void result){
    textResponse.setText(response);
    super.onPostExecute(result);
}

}

如果有人有任何建议,将不胜感激谢谢

4

1 回答 1

0

让它在这里工作我相信问题是由于没有以 \r\n 结尾的命令结束,如果有人感兴趣,这里是工作测试代码。这是为了发送一个不接收的字符串。

public class MainActivity extends AppCompatActivity {
Socket s = new Socket();
PrintWriter s_out;
BufferedReader s_in;
String out = null;



public void sendCommand (String command){
    try {
        s_out = new PrintWriter(s.getOutputStream(), true);
        s_out.println(command);
    }catch (IOException e){
        System.out.println(e);
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button LED_ON, LED_OFF;

    LED_ON = (Button)findViewById(R.id.button);
    LED_OFF = (Button)findViewById(R.id.button2);

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    try{
        s.connect(new InetSocketAddress("192.168.0.104", 5000));
    }catch (IOException e){
        System.out.println(e);
    }

    LED_ON.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            sendCommand("DIG:PIN LED2,1\r\n");

        }
    });
    LED_OFF.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            sendCommand("DIG:PIN LED2,0\r\n");
        }
    });
}

}

当使用按钮启动命令时,我注意到如果您在其中编写发送命令代码而不是单独的函数,则应用程序会超时并崩溃。

于 2017-02-21T20:31:53.037 回答