Modbus协议逃脱线程(Android的,Jamod) [英] Modbus protocol escapes the thread (Android,Jamod)

查看:733
本文介绍了Modbus协议逃脱线程(Android的,Jamod)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图采用MODBUS协议连接到PLC。我打电话的ModBus从螺纹连接的方法,我得到不同的是我运行在主线程通讯...
我不知道它逃脱...

例外:

     10月8日至2日:48:44.461:W / System.err的(4395):android.os.NetworkOnMainThreadException
    10月8日至2日:48:44.471:W / System.err的(4395):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
    10月8日至2日:48:44.471:W / System.err的(4395):在

code:

 
    包com.kikudjiro.android.echo;    进口net.wimpi.modbus.facade.ModbusTCPMaster;
    进口android.app.Activity;
    进口android.os.Bundle;
    进口android.util.Log;
    进口android.view.View;
    进口android.view.View.OnClickListener;
    进口android.widget.Button;    公共类设置来延长活动实现Runnable {        按钮connect_b,disconnect_b;
        螺纹COMM =新主题(本);        公共无效的onCreate(捆绑savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.settings);
            addListenerOnButton();
        }        公共无效addListenerOnButton(){            connect_b =(按钮)findViewById(R.id.button1);
            disconnect_b =(按钮)findViewById(R.id.button2);
            connect_b.setOnClickListener(新OnClickListener(){
                公共无效的onClick(查看为arg0){
                    comm.run();
                }            });            disconnect_b.setOnClickListener(新OnClickListener(){                公共无效的onClick(查看为arg0){
                    comm.interrupt();
                }            });
        }        // @覆盖
        公共无效的run(){            尝试{
                ModbusTCPMaster MB =新ModbusTCPMaster(192.168.107.29,502);
                //而(!comm.interrupted()){
                    Log.i(!!!!!!!,试试吧!);
                    MB.connect();
                    MB.writeCoil(1,1,真);
                    MB.disconnect();
                //}
            }            赶上(例外前){
                ex.printStackTrace();
                Log.i(HHH,exceptionas !!!);
            } {最后
                Log.i(!!!!!!!,终于来了!);
            }        }    }


解决方案

在按钮的点击,你叫 comm.run()刚刚执行直接运行()方法在UI线程的上下文。这是一个常见的​​错误,你永远不应该叫运行()直接在实例。使用 comm.start()在你的工作线程的上下文中执行的Modbus code。

这里的Javadoc

I'm trying to connect to PLC using ModBus protocol. I'm calling ModBus connect method from thread and I'm getting exception that I'm running communications on the main thread... I wonder where it escapes...

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 

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

        }

    }

解决方案

On button click, you call comm.run() which just executes the run() method directly in the context of the UI thread. This is a common error, you should never call run() directly on a Thread instance. Use comm.start() to execute the Modbus code in the context of your worker thread.

Here is the Javadoc

这篇关于Modbus协议逃脱线程(Android的,Jamod)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆