Modbus connect with Android via TCP

  • Replies:0
  • OpenNot stickiedUnanswered

May 6, 2014 9:18:51 AM via Website

Hello,
We are developing an android app for device communication using MODBus Protocal.
Please refer link for solution architecture diagram

We have used the following downloaded libraries Tools.

jamod-1.2-SNAPSHOT.jar has been downloaded from sourceforge
mod_RSsim.exe has been downloaded from sourceforge

PROBLEM
we can connect android APP with modbus simulator(mod_RSsim.exe) using socket connection. but we couldn't write and read values from registers using functions from "jamod-1.2-SNAPSHOT.jar".

we got output like this 00 00 00 00 00 06 00 03 00 00 00 00 when we use

WriteMultipleRegistersRequest wrq = new WriteMultipleRegistersRequest();
wrq.getHexMessage();

what is the correct function to read and write for registers in modbus simulator using "jamod-1.2-SNAPSHOT.jar".

Our Program

package com.example.smodbus;

import java.io.BufferedWriter;
import java.io.DataOutput;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import net.sourceforge.jmodbus.Modbus;
import net.wimpi.modbus.io.ModbusTCPTransaction;
import net.wimpi.modbus.msg.ReadInputRegistersResponse;
import net.wimpi.modbus.msg.ReadMultipleRegistersRequest;
import net.wimpi.modbus.msg.WriteMultipleRegistersRequest;
import net.wimpi.modbus.msg.WriteSingleRegisterRequest;
import net.wimpi.modbus.procimg.Register;
import net.wimpi.modbus.procimg.SimpleRegister;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements Runnable
{
private Button buttonSum;
ModbusTCPTransaction trans=null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

        addListenerOnButton(); 

    } 
  public void addListenerOnButton(){ 

      buttonSum=(Button)findViewById(R.id.button1); 

        buttonSum.setOnClickListener(new OnClickListener(){ 

            @Override 
            public void onClick(View view) { 

                new conect().execute();


            } 

        }); 
  }
  public boolean onCreateOptionsMenu(Menu menu) { 
        // Inflate the menu; this adds items to the action bar if it is present. 
        getMenuInflater().inflate(R.menu.activity_main, menu); 
        return true; 
    }
@Override
public void run() {
    // TODO Auto-generated method stub

} 


private class conect extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {

         try {
             int port=502;
             Socket s = new Socket("192.168.56.2", port);

             BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

             String outMsg = "TCP connecting to " + port + System.getProperty("line.separator");
              Log.i("TcpClient", "sent: " + outMsg);
              SimpleRegister[] registers = new SimpleRegister[1];
              registers[0] = new SimpleRegister(15);
              WriteMultipleRegistersRequest req = new WriteMultipleRegistersRequest(400001, registers);

              Register ras = new Register() {

                @Override
                public int toUnsignedShort() {
                    // TODO Auto-generated method stub
                    return 0;
                }

                @Override
                public short toShort() {
                    // TODO Auto-generated method stub
                    return 0;
                }

                @Override
                public byte[] toBytes() {
                    // TODO Auto-generated method stub
                    return null;
                }

                @Override
                public int getValue() {
                    // TODO Auto-generated method stub
                    return 0;
                }

                @Override
                public void setValue(byte[] arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void setValue(short arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void setValue(int arg0) {
                    // TODO Auto-generated method stub

                }
            };

              ras.setValue(40001);
              ras.getValue();

              WriteSingleRegisterRequest wrq = new WriteSingleRegisterRequest(Integer.decode("5").intValue(), ras);




            ReadMultipleRegistersRequest rea=new ReadMultipleRegistersRequest();
            rea.getHexMessage();

            DataOutput dout = new DataOutput() {

                @Override
                public void writeUTF(String str) throws IOException {
                    // TODO Auto-generated method stub

                }

                @Override
                public void writeShort(int val) throws IOException {
                    // TODO Auto-generated method stub

                }

                @Override
                public void writeLong(long val) throws IOException {
                    // TODO Auto-generated method stub

                }

                @Override
                public void writeInt(int val) throws IOException {
                    // TODO Auto-generated method stub

                }

                @Override
                public void writeFloat(float val) throws IOException {
                    // TODO Auto-generated method stub

                }

                @Override
                public void writeDouble(double val) throws IOException {
                    // TODO Auto-generated method stub

                }

                @Override
                public void writeChars(String str) throws IOException {
                    // TODO Auto-generated method stub

                }

                @Override
                public void writeChar(int val) throws IOException {
                    // TODO Auto-generated method stub

                }

                @Override
                public void writeBytes(String str) throws IOException {
                    // TODO Auto-generated method stub

                }

                @Override
                public void writeByte(int val) throws IOException {
                    // TODO Auto-generated method stub

                }

                @Override
                public void writeBoolean(boolean val) throws IOException {
                    // TODO Auto-generated method stub

                }

                @Override
                public void write(byte[] buffer, int offset, int count) throws IOException {
                    // TODO Auto-generated method stub

                }

                @Override
                public void write(int oneByte) throws IOException {
                    // TODO Auto-generated method stub

                }

                @Override
                public void write(byte[] buffer) throws IOException {
                    // TODO Auto-generated method stub

                }
            };

            rea.writeData(dout);
            System.out.println();

//register values
System.out.println(rea.getHexMessage());

              out.write(outMsg);
             out.flush(); 
             s.close();

         } catch (UnknownHostException e) {

             e.printStackTrace();

         } catch (IOException e) {

             e.printStackTrace();

         }


        return null;
    }

    @Override
    protected void onPostExecute(String result) {

    }
  }

}

Looking forward to hear from someone.
Thanks for your help.
Please reply
sabarreesh@mindnotix.com