Modbus protocol speeds up flow (Android, Jamod)

I am trying to connect to a PLC using the ModBus protocol. I call the ModBus connection method from the stream, and I get the exception that I run messages in the main stream ... I wonder where it runs ...

An exception:



    08-02 10:48:44.461: W/System.err(4395): android.os.NetworkOnMainThreadException
    08-02 10:48:44.471: W/System.err(4395):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
    08-02 10:48:44.471: W/System.err(4395):     at 

the code:




    package com.kikudjiro.android.echo;

    import net.wimpi.modbus.facade.ModbusTCPMaster;
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class settings extends Activity implements Runnable {

        Button connect_b, disconnect_b;
        Thread comm = new Thread(this);

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.settings);
            addListenerOnButton();
        }

        public void addListenerOnButton() {

            connect_b = (Button) findViewById(R.id.button1);
            disconnect_b = (Button) findViewById(R.id.button2);
            connect_b.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    comm.run();
                }

            });

            disconnect_b.setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    comm.interrupt();
                }

            });
        }

        // @Override
        public void run() {

            try {
                ModbusTCPMaster MB = new ModbusTCPMaster("192.168.107.29", 502);
                //while (!comm.interrupted()) {
                    Log.i("!!!!!!!", "try!");
                    MB.connect();
                    MB.writeCoil(1, 1, true);
                    MB.disconnect();
                //}
            }

            catch (Exception ex) {
                ex.printStackTrace();
                Log.i("hhh", "exceptionas!!!");
            } finally {
                Log.i("!!!!!!!", "finally!");
            }

        }

    }

0
source share
1 answer

At the click of a button, you call comm.run()which simply executes the method run()directly in the context of the user interface thread. This is a common mistake, you should never call run()directly on the instance Thread. Use comm.start()to execute Modbus code in the context of a workflow.

Here is the javadoc

0
source

All Articles