<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Modbus4j and Android application]]></title><description><![CDATA[<p dir="auto">I am using modbus4j in Android application. Before I tested it in java, and works fine. But in Android I have problems with the master.init(), it does not work. In the manifest I add permissions for Internet and Access network state. I have a code more or less like this:</p>
<pre><code>// Perform action on click
            // Read IP from layout***
            EditText edittext_IP = (EditText) findViewById(R.id.editText1);
            TextView MWReaded = (TextView) findViewById(R.id.textView4);
            // **********************

            //
            String slaveIP;
            slaveIP = edittext_IP.getText().toString();

            ModbusFactory factory = new ModbusFactory();
            IpParameters params = new IpParameters();
            params.setHost(slaveIP);
            params.setPort(502);
            params.setEncapsulated(false);
            ModbusMaster master = factory.createTcpMaster(params, true);
            master.setTimeout(500);
            master.setRetries(2);

            try {
                master.init();
            } catch (ModbusInitException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(getApplication(),
                        "Error: " + e.getMessage(), 10).show();
            }
            ModbusLocator locator = new ModbusLocator(1,
                    RegisterRange.HOLDING_REGISTER, 0,
                    DataType.TWO_BYTE_INT_UNSIGNED);

            Object xx = null;
            try {
                xx = master.getValue(locator);
            } catch (ModbusTransportException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ErrorResponseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            int Register = 0;

            if (xx != null){
            Register = Integer.parseInt(xx.toString());
            } else{
                Register = 6969;
            }

            MWReaded.setText(String.valueOf(Register));
            TextView txttest_n = (TextView) findViewById(R.id.textView5);
            txttest_n.setText("n=" + n);
            n = n + 1;

        }
    });

</code></pre>
<p dir="auto">But the master is never initialized. I can see a exception. "android.os.NetworkOnMainThreadException"<br />
**<br />
Do you know how can I solve this problem?**</p>
]]></description><link>https://forum.mango-os.com/topic/1185/modbus4j-and-android-application</link><generator>RSS for Node</generator><lastBuildDate>Tue, 12 May 2026 18:27:32 GMT</lastBuildDate><atom:link href="https://forum.mango-os.com/topic/1185.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 18 Nov 2012 17:30:50 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Modbus4j and Android application on Thu, 22 Nov 2012 15:00:36 GMT]]></title><description><![CDATA[<p dir="auto">Hello.<br />
Thanks for the information.<br />
I have created and asynctask and I use the master.ini() in this new thread. Works fine.</p>
<p dir="auto">Best regards.</p>
]]></description><link>https://forum.mango-os.com/post/7172</link><guid isPermaLink="true">https://forum.mango-os.com/post/7172</guid><dc:creator><![CDATA[ramonhl]]></dc:creator><pubDate>Thu, 22 Nov 2012 15:00:36 GMT</pubDate></item><item><title><![CDATA[Reply to Modbus4j and Android application on Sun, 18 Nov 2012 19:51:12 GMT]]></title><description><![CDATA[<p dir="auto">I would suppose it's pretty much what the exception says: you're running network operations on the main thread, which Android frowns upon. Try running init in a service thread or something.</p>
]]></description><link>https://forum.mango-os.com/post/7166</link><guid isPermaLink="true">https://forum.mango-os.com/post/7166</guid><dc:creator><![CDATA[mlohbihler]]></dc:creator><pubDate>Sun, 18 Nov 2012 19:51:12 GMT</pubDate></item></channel></rss>