Java Question modbus and B4A

alan1968

Active Member
Licensed User
Longtime User
hello,

I managed to use jamod (connections with WAGO plc) with Eclipse Android

I want to create a library for B4A

everything works fine but when compiling I get an error launching the application.

do you have an idea or this bug.

thank you

my code b4a

'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim port_num As Int
Dim adr_ip As Double
Dim val_bit As Boolean
Dim my_wago As MODBUS_TCP_MASTER_B4A

End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim button1 As Button
Dim label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
Activity.LoadLayout("1")
my_wago.Ip("192.168.2.150")
my_wago.Connection
End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
Dim x As String
x=my_wago.ReadInput(0,2)
label1.Text=x
End Sub



my code eclipse
package com.exemple.android;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import java.net.*;
import java.io.*;
import net.wimpi.modbus.*;
import net.wimpi.modbus.msg.*;
import net.wimpi.modbus.io.*;
import net.wimpi.modbus.net.*;

@ShortName("MODBUS_TCP_MASTER_B4A")
@Permissions(values={"android.permission.INTERNET"})
@Version(1.0f)
@Author("Alan1968")

public class modbus_ba4 {
/** Called when the activity is first created. */


/* The important instances of the classes mentioned before */
static TCPMasterConnection con = null; //the connection
static ModbusTCPTransaction trans = null; //the transaction
static ReadInputDiscretesRequest req = null; //the request
static ReadInputDiscretesResponse res = null; //the response

/* Variables for storing the parameters */
static InetAddress addr = null; //the slave's address
static int port = Modbus.DEFAULT_PORT;
static int repeat = 1; //a loop for repeating the transaction
//private static String x;

public static void Ip(String my_ip) throws IOException {
addr=InetAddress.getByName(my_ip);
con = new TCPMasterConnection(addr);
}
public static void Connection () throws Exception{
con.connect();
}
public static String ReadInput (int ref,int count) throws ModbusIOException, ModbusSlaveException, ModbusException{
req = new ReadInputDiscretesRequest(ref, count);
trans = new ModbusTCPTransaction(con);
trans.setRequest(req);
trans.execute();
res = (ReadInputDiscretesResponse) trans.getResponse();
//6. Close the connection
con.close();
return (res.getDiscretes().toString());
}

}


my xml

<?xml version="1.0" encoding="UTF-8"?>
<root>
<doclet-version-NOT-library-version>1.00</doclet-version-NOT-library-version>
<class>
<name>com.exemple.android.modbus_ba4</name>
<shortname>MODBUS_TCP_MASTER_B4A</shortname>
<owner>process</owner>
<permission>android.permission.INTERNET</permission>
<method>
<name>Ip</name>
<comment></comment>
<returntype>void</returntype>
<parameter>
<name>my_ip</name>
<type>java.lang.String</type>
</parameter>
</method>
<method>
<name>ReadInput</name>
<comment></comment>
<returntype>java.lang.String</returntype>
<parameter>
<name>ref</name>
<type>int</type>
</parameter>
<parameter>
<name>count</name>
<type>int</type>
</parameter>
</method>
<method>
<name>Connection</name>
<comment></comment>
<returntype>void</returntype>
</method>
</class>
<version>1.0</version>
<author>Alan1968</author>
</root>
 
Last edited:

alan1968

Active Member
Licensed User
Longtime User
Hello Erel
"compile & debug report"

Compiling code. 0.02
Generating R file. 0.00
Compiling generated Java code. 1.92
Convert byte code - optimized dex. 1.30
Packaging files. 0.53
Signing package file (debug key). 0.67
ZipAlign file. 0.03
Installing file to device. 0.06
Installing with B4A-Bridge.
Installation will fail if the signing key is different than the previous used key.
In that case you will need to manually uninstall the existing application.

Completed successfully.

error message
en francais sorry
------------------------------------------------------------------
/!\ désolé!
fermeture soudaine de l'appilcation ModbusTCPMaster (du processus my.modbusTCPMaster.ba4) .Merci de réésayer.
-------------------------------------------------------------------
 

alan1968

Active Member
Licensed User
Longtime User
the log

>>> UpdateNote: netType:1 thisday:15148 uptime:11813367 rxbytes:2140536 txbytes:867210
Installing file.
Default buffer size used in BufferedOutputStream constructor. It would be better to be explicit if an 8k buffer is required.
Starting activity: Intent { act=android.intent.action.VIEW dat=file:///sdcard/temp.apk typ=application/vnd.android.package-archive flg=0x10000000 cmp=com.android.packageinstaller/.PackageInstallerActivity }
Unexpected resume of com.android.packageinstaller while already resumed in anywheresoftware.b4a.b4abridge
free_cache(403776) avail 49061888
Displayed activity com.android.packageinstaller/.PackageInstallerActivity: 348 ms (total 454439 ms)
showStatusIcon on inactive InputConnection
[unregister] handle 0x35b228 still locked (state=40000001)
>>> UpdateNote: netType:1 thisday:15148 uptime:11823487 rxbytes:2248499 txbytes:871348
>>> Enter set_buttons_light
*** set_screen_state 1
enableSensor() : Entered : binder = android.hardware.SensorManager$ListenerDelegate@4c220108, name = Rockchip 3-axis Accelerometer, sensor = 2,enable = 200
No focus window, dropping: KeyEvent{action=1 code=6 repeat=0 meta=0 scancode=62 mFlags=8}
enableSensor() : about to return 'true'.
No window to dispatch pointer action 0
ACTION_SCREEN_ON
>>> Enter set_battery_light
>>> Enter set_notifications_light
[DataConnection] Stop poll NetStat
!!! ACTION_SCREEN_ON
Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@4c0647a8
Global Control: cmd=[DRIVER RSSI-APPROX] reply=[WIFI RSSI -41
]
Global Control: cmd=[DRIVER LINKSPEED] reply=[LINKSPEED 48
]
>>> UpdateNote: netType:1 thisday:15148 uptime:11833628 rxbytes:2252129 txbytes:874286
Starting activity: Intent { dat=file:///sdcard/temp.apk cmp=com.android.packageinstaller/.InstallAppProgress (has extras) }
Replacing package:my.ModbusTCPMaster.ba4
Scanning package: /data/app/vmdl10083.tmp
Displayed activity com.android.packageinstaller/.InstallAppProgress: 309 ms (total 309 ms)
Removing non-system package:my.ModbusTCPMaster.ba4
Removing package my.ModbusTCPMaster.ba4
Activities: my.ModbusTCPMaster.ba4.main
Scanning package my.ModbusTCPMaster.ba4
/data/app/vmdl10083.tmp changed; unpacking
DexInv: --- BEGIN '/data/app/vmdl10083.tmp' ---
DexOpt: load 121ms, verify 464ms, opt 12ms
DexInv: --- END '/data/app/vmdl10083.tmp' (success) ---
Uninstalling process my.ModbusTCPMaster.ba4
Activities: my.ModbusTCPMaster.ba4.main
move /data/dalvik-cache/data@[email protected]@classes.dex -> /data/dalvik-cache/data@[email protected]@classes.dex
New package installed in /data/app/my.ModbusTCPMaster.ba4.apk
Uninstalling process my.ModbusTCPMaster.ba4
Resources don't contain package for resource number 0x7f0700e5
Resources don't contain package for resource number 0x7f020031
Resources don't contain package for resource number 0x7f020030
Resources don't contain package for resource number 0x7f050000
Resources don't contain package for resource number 0x7f060001
Checking component ComponentInfo{com.google.android.providers.enhancedgooglesearch/com.google.android.providers.enhancedgooglesearch.Launcher}
GC freed 46 objects / 1880 bytes in 201ms
Global Control: cmd=[DRIVER RSSI-APPROX] reply=[WIFI RSSI -52
]
Global Control: cmd=[DRIVER LINKSPEED] reply=[LINKSPEED 36
]
GC freed 9290 objects / 650872 bytes in 207ms
GC freed 423 objects / 33616 bytes in 168ms
PackageAdded: package:my.ModbusTCPMaster.ba4
Resources don't contain package for resource number 0x7f0700e5
Resources don't contain package for resource number 0x7f020031
Resources don't contain package for resource number 0x7f020030
Resources don't contain package for resource number 0x7f050000
Resources don't contain package for resource number 0x7f060001
Checking component ComponentInfo{com.google.android.providers.enhancedgooglesearch/com.google.android.providers.enhancedgooglesearch.Launcher}
Global Control: cmd=[DRIVER RSSI-APPROX] reply=[WIFI RSSI -43
]
Global Control: cmd=[DRIVER LINKSPEED] reply=[LINKSPEED 48
]
>>> Enter set_buttons_light
Starting activity: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=my.ModbusTCPMaster.ba4/.main }
Start proc my.ModbusTCPMaster.ba4 for activity my.ModbusTCPMaster.ba4/.main: pid=30786 uid=10024 gids={3003}
GC freed 44 objects / 2096 bytes in 268ms
GC freed 277 objects / 10520 bytes in 203ms
Debugger thread not active, ignoring DDM send (t=0x41504e4d l=38)
Debugger thread not active, ignoring DDM send (t=0x41504e4d l=48)
GC freed 57 objects / 2440 bytes in 187ms
GC freed 2 objects / 48 bytes in 143ms
Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@4c13f258 (uid=10024 pid=30786)
Global Control: cmd=[DRIVER RSSI-APPROX] reply=[WIFI RSSI -39
]
Global Control: cmd=[DRIVER LINKSPEED] reply=[LINKSPEED 48
]
Client not active, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@4c115438
Total arena pages for JIT: 2
Could not find method net.wimpi.modbus.net.TCPMasterConnection.connect, referenced from method com.exemple.android.modbus_ba4.Connection
VFY: unable to resolve virtual method 1870: Lnet/wimpi/modbus/net/TCPMasterConnection;.connect ()V
VFY: replacing opcode 0x6e at 0x0002
Making a copy of Lcom/exemple/android/modbus_ba4;.Connection code (28 bytes)
Could not find class 'net.wimpi.modbus.net.TCPMasterConnection', referenced from method com.exemple.android.modbus_ba4.Ip
VFY: unable to resolve new-instance 415 (Lnet/wimpi/modbus/net/TCPMasterConnection;) in Lcom/exemple/android/modbus_ba4;
VFY: replacing opcode 0x22 at 0x0006
Making a copy of Lcom/exemple/android/modbus_ba4;.Ip code (48 bytes)
Could not find class 'net.wimpi.modbus.msg.ReadInputDiscretesRequest', referenced from method com.exemple.android.modbus_ba4.ReadInput
VFY: unable to resolve new-instance 413 (Lnet/wimpi/modbus/msg/ReadInputDiscretesRequest;) in Lcom/exemple/android/modbus_ba4;
VFY: replacing opcode 0x22 at 0x0000
Making a copy of Lcom/exemple/android/modbus_ba4;.ReadInput code (124 bytes)
** Activity (main) Create, isFirst = true **
Shutting down VM
threadid=3: thread exiting with uncaught exception (group=0x4001b168)
Uncaught handler: thread main exiting due to uncaught exception
java.lang.NoClassDefFoundError: net.wimpi.modbus.net.TCPMasterConnection
at com.exemple.android.modbus_ba4.Ip(modbus_ba4.java:36)
at my.ModbusTCPMaster.ba4.main._activity_create(main.java:208)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:104)
at my.ModbusTCPMaster.ba4.main.afterFirstLayout(main.java:84)
at my.ModbusTCPMaster.ba4.main.access$100(main.java:16)
at my.ModbusTCPMaster.ba4.main$WaitForLayout.run(main.java:72)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
>>> UpdateNote: netType:1 thisday:15148 uptime:11843790 rxbytes:2256359 txbytes:881744
GC freed 9621 objects / 414504 bytes in 120ms
Sending signal. PID: 30786 SIG: 3
threadid=7: reacting to signal 3
Wrote stack trace to '/data/anr/traces.txt'
Global Control: cmd=[DRIVER RSSI-APPROX] reply=[WIFI RSSI -35
]
Global Control: cmd=[DRIVER LINKSPEED] reply=[LINKSPEED 48
]
Global Control: cmd=[DRIVER RSSI-APPROX] reply=[WIFI RSSI -40
]
Global Control: cmd=[DRIVER LINKSPEED] reply=[LINKSPEED 36
]
Global Control: cmd=[DRIVER RSSI-APPROX] reply=[WIFI RSSI -40
]
Global Control: cmd=[DRIVER LINKSPEED] reply=[LINKSPEED 36
]
Sending signal. PID: 30786 SIG: 9
Process my.ModbusTCPMaster.ba4 (pid 30786) has died.
WIN DEATH: Window{4c155d78 my.ModbusTCPMaster.ba4/my.ModbusTCPMaster.ba4.main paused=false}
WIN DEATH: Window{4c1625b8 my.ModbusTCPMaster.ba4/my.ModbusTCPMaster.ba4.main paused=false}
** Activity (main) Resume **
Unexpected resume of anywheresoftware.b4a.b4abridge while already resumed in my.ModbusTCPMaster.ba4
>>> UpdateNote: netType:1 thisday:15148 uptime:11853987 rxbytes:2261519 txbytes:886720
Global Control: cmd=[DRIVER RSSI-APPROX] reply=[WIFI RSSI -33
]
Global Control: cmd=[DRIVER LINKSPEED] reply=[LINKSPEED 48
]
Global Control: cmd=[DRIVER RSSI-APPROX] reply=[WIFI RSSI -42
]
Global Control: cmd=[DRIVER LINKSPEED] reply=[LINKSPEED 48
]
GC freed 3030 objects / 211888 bytes in 113ms
Global Control: cmd=[DRIVER RSSI-APPROX] reply=[WIFI RSSI -43
]
Global Control: cmd=[DRIVER LINKSPEED] reply=[LINKSPEED 48
]
 

agraham

Expert
Licensed User
Longtime User
It looks like you are compiling against an external jar in your code so I guess that jar has not been included in the final apk.

You need to include that jar in your apk like the AdMob jar. Copy and rename the AdMob xml file to match the name of the external jar and put it and the jar in your additional libraries folder and select it in the Libs tab. This will make Basic4android include the jar in your apk.
 

alan1968

Active Member
Licensed User
Longtime User
it's ok "ca marche !!!!!!" :sign0098::sign0098::sign0098:

I will complete the feature to begin reading writing
 
Last edited:

oymyakon

Member
Licensed User
Longtime User
Hi,

Is it possible to get to the modbus addresses direct as that is what i am trying to code up. Cheers Paul.
 
Top