Sub AStreams_NewData subroutine problem

csjoe72

Member
Licensed User
Longtime User
Hi,

If I use "Sub AStreams_NewData (Buffer() As Byte)" subroutine and
I receive next data stream: $ModeAcknowledge:1* the program will work.
But when I get a shorter data stream for example : $ModeAckno:4* my program will stoped.What is the difference beetween two messages?
Thanks, Joe
B4X:
Sub AStreams_NewData (Buffer() As Byte)
    Dim msg As String
    msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    Log(msg)
    
   If msg.SubString2(1,5) ="Mode" Then
     Log (msg.SubString2(1,5))
    End If
    
  msg = ""
  
End Sub
There is the error message, and logs:

$PMode:1*
$ModeAcknowledge:1*
Mode
$PMode:2*
$ModeAcknowled:2*
Mode
$PMode:3*
$ModeAcknowl:3*
Mode
$PMode:4*
$ModeAckno:4*
main_astreams_newdata (java line: 367)
java.lang.StringIndexOutOfBoundsException
at java.lang.String.substring(String.java:1579)
at tcp.joe.home.main._astreams_newdata(main.java:367)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:105)
at anywheresoftware.b4a.BA$1.run(BA.java:210)
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:4644)
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:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
at dalvik.system.NativeStart.main(Native Method)
java.lang.StringIndexOutOfBoundsException
 

csjoe72

Member
Licensed User
Longtime User
The incoming stream is longer than 5 characters (always), and if delete the logs from the program, the problem is existing....

Joe
 
Upvote 0

csjoe72

Member
Licensed User
Longtime User
I have changed the log command but I didn't receive empty strings...
I think, the error depends on length of string, but I don't know why...

When I send long string, no error...

Log("Msg:" & msg & ".")

Start Program
Connecting...
** Activity (main) Resume **
Sub Socket1_Connected
Msg:$UploadLimit:165,150,145,120,156,150,145,140,155,145,150,110,170,155,155,145,165,149,*.
Msg:$UploadNowLimit:165,120,156,140,155,110,155,155,165,*.
Msg:$UploadTemp:155,120,157,156,153,109,155,155,150,076,121,949,372,346,307,*.
Msg:$UploadValve:011100110,40*.
Msg:$UploadSettings:1,15,0*.
$SetLimit:01165*
$SetLimit:02149*
Msg:$LimitAcknowledge:01165*.
Msg:$LimitAcknowledge:02149*.
Msg:$UploadNowLimit:165,120,156,140,155,110,155,155,165,*.
Msg:$UploadNowLimit:165,120,156,140,155,110,155,155,165,*.
Msg:$UploadTemp:155,120,158,157,153,109,154,155,150,076,121,949,339,302,306,*.
Msg:$UploadValve:011100010,70*.
 
Upvote 0

csjoe72

Member
Licensed User
Longtime User
There is a code:

B4X:
Sub AStreams_NewData (Buffer() As Byte)
    Dim msg As String
   msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
   Log("Msg:" & msg & "." & " Length:" & Buffer.Length)
   edittext3.text = edittext3.text & msg & Chr (10)

       If msg.SubString2(1,15) ="UploadNowLimit" AND Buffer.Length = 53 Then
    listview2.AddSingleLine("UploadNowLimit")
    a = 1
    For q = 16 To 50 Step 4
     Limitnow(a)= ((msg.SubString2((q),(q+3))+50)/10 )
     LblLimitNow(a).text ="Limit:" & Limitnow(a)& " C" & Chr (176)
     listview2.AddSingleLine(Limitnow(a))
     a=a+1
    Next
    If FirstUpload = True Then 
     Waitms ( 300 )
     Send_Command ("$UploadTemp*")
     Return
    End If
   End If


End Sub



and logs:
Msg:123456789112345*. Length:16
Msg:12345678911234*. Length:15
Msg:1234567891123*. Length:14
main_astreams_newdata (java line: 362)
java.lang.StringIndexOutOfBoundsException
at java.lang.String.substring(String.java:1579)
at tcp.joe.home.main._astreams_newdata(main.java:362)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:105)
at anywheresoftware.b4a.BA$1.run(BA.java:210)
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:4644)
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:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
at dalvik.system.NativeStart.main(Native Method)
java.lang.StringIndexOutOfBoundsException

In this case I think the length of stream connection with error.
If the stream shorter than 15 (see is the if statement) error will happen, but I dont know why... will I use "else"?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…