1

So I am trying to connect to a device that is going to acquire analogue data(Red Pitaya). It has set scpi commands that control the device. I can control these through labview and using putty.

I am trying to write and android application that can access the devices scpi server and send commands to it for the device to complete.

The device is programmed in a way in that you first have to connect to the server using a SSH connection which i have no problem doing using JSch, from there you can send a command to start the scpi server and open a connection.

Now this is the bit i am struggling with and i dont understand why, when the SCPI server has started it is accessed through the Ip of the device and a raw port of 5000, but i cannot seem to write a piece of code that connects to this and performs a SCPI command. I am not to sure whether it is the connection or the way i have sent the data.

This is the code

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);
}

}

If anyone has any advice it would be appreciated thanks

RedP
  • 25
  • 3

1 Answers1

0

Got it working here i believe the issue was due to not ending a command with \r\n being at the end of the string here is the working test code if anyone is interested. This is for sending a string not receiving.

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");
        }
    });
}

}

When using buttons to initiate a command i noticed that if you write the send command code in there instead of a separate function the app would time out and crash.

RedP
  • 25
  • 3
  • `println()` already appends a line terminator. Now you have two. NB The exception handling is incorrectly structured. You should only add the listeners if the connect succeeded. Code that depends on the success of code in a prior `try` block should be inside that `try` block. – user207421 Feb 22 '17 at 00:43
  • Thanks for your advice, definitely need this to help me get better. – RedP Feb 22 '17 at 20:20