B4R Question Out of bound error MQTT Payload ConvertBytesToArray

H.L.M. Feijen

Member
Licensed User
Longtime User
Hello all,

In experimenting with MQTT, B4R- and B4Xserializator I encountered a problem with the ConvertBytesToArray member.
I send the type mb from B4A to NodeMCu B4R.

B4A side :
Sub Proces Globals
...
Dim Ser as B4xSerializator
Type MyButton(Which As String, State As String)
...
End Sub
...
Public Sub ToggleButtonChanged(mb As MyButton)
client.Publish("esp",Ser.ConvertObjectToBytes(mb))
End Sub

B4R (NodeMcu 12E) side:
Sub Proces Globals
….
Private ObjectsBuffer(500) As Object
Private Ser As B4RSerializator
…..
End Sub
…...
Sub Mqtt_MessageArrived (Topic As String, Payload() As Byte)
Log("Message arrived. Topic=", Topic, " payload: ", Payload)
If Topic = "esp" Then
Dim Objects() As Object =Ser.ConvertBytesToArray(Payload,ObjectsBuffer)
Log(Payload.Length)
Log(Objects.Length)
Log(ObjectsBuffer.Length)
End If
End Sub
…....

The monitor says:
Connected to broker
Message arrived. Topic=esp payload: x??`?b``H2I?K?H?-?I??M??S??L*-)??c#?2?# ?b??????#vFFV?XpIbI*##?#RT?
#
??L?


The line:
Dim Objects() As Object =Ser.ConvertBytesToArray(Payload,ObjectsBuffer) creates the following error:
Out of bounds error. Array length = 500, Index = 65535

Blocking out “=Ser.ConvertBytesToArray(Payload,ObjectsBuffer)” gives the length of the arrays as 90, 0 and 500 respectively.

Question:
How to correct this error?
 

H.L.M. Feijen

Member
Licensed User
Longtime User
Hello Erel,

1. OK will do.
2. Hex output: 789C936094626060483249D44BAD48CC2DC849D5CB4DCCCC5389CFAD4C2A2D29C9CF6306CA32F20209CF62CFBCCC92CCC49CCCAAD41476064656A058704962492A230B905552549A0A160ACFC84CCE6064079909D66F0800B9B71801
Out of bounds error. Array length = 500, Index =65535
From:
B4X:
Sub Mqtt_MessageArrived (Topic As String, Payload() As Byte)
   Log("Message arrived. Topic=", Topic, " payload: ", Payload)
   Log(bc.HexFromBytes(Payload))
   If Topic= "esp" Then
     Dim Objects() As Object = Ser.ConvertBytesToArray(Payload, ObjectsBuffer)
    Log(Payload.Length)
    Log(Objects.Length)
    Log(ObjectsBuffer.Length)
End Sub
 
Last edited:
Upvote 0

H.L.M. Feijen

Member
Licensed User
Longtime User
1.Followed the link, extracted and pasted v.1.91 in B4R libraries folder. (B4R library window still shows version 1.90. Also after reload). Hex output tails not different.
2. Is the invalid data result of the bug or should the content of Payload be changed?
 
Upvote 0

H.L.M. Feijen

Member
Licensed User
Longtime User
Hex output v1.9.1: 789C936094626060483249D44BAD48CC2DC849D5CB4DCCCC5389CFAD4C2A2D29C9CF6306CA32F20209CF62CFBCCC92CCC49CCCAAD41476464656A058704962492A230B905552549A0A160ACFC84CCE6064079909D66F0800B9E11802
Same out of bound error.
For your convenience the code:

B4A side:
B4X:
#Region  Project Attributes
   #ApplicationLabel: MyB4A_Relay3
   #VersionCode: 1
   #VersionName:
   #BridgeLogger: True
   'SupportedOrientations possible values: unspecified, landscape or portrait.
   #SupportedOrientations: unspecified
   #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
   #FullScreen: False
   #IncludeTitle: True
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim Ser As B4XSerializator
   Type MyButton(Which As String, State As String)   
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.
   Private ToggleButton1 As ToggleButton
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
End Sub

Sub Activity_Resume
   SetState
End Sub

Sub SetState
   ToggleButton1.Enabled = Starter.connected
End Sub

Sub ToggleButton1_CheckedChange(Checked As Boolean)
   Dim mb As MyButton
   mb.Initialize
   mb.Which="button1"
   If Checked Then
       mb.State="true"
   Else
       mb.State="false"
   End If
   Starter.client.Publish("esp",Ser.ConvertObjectToBytes(mb))
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   Activity.Finish
End Sub

B4R side:
B4X:
#Region Project Attributes
   #AutoFlushLogs: True
   #CheckArrayBounds: True
   #StackBufferSize: 1000
#End Region

Sub Process_Globals
   Public Serial1 As Serial
   Private d1pins As D1Pins
   Private d6  As Pin       'not yet used
   Private mqtt As MqttClient
   Private wifi As ESP8266WiFi
   Private client As WiFiSocket
   Private ObjectsBuffer(500) As Object
   Private Ser As B4RSerializator
   Dim bc As ByteConverter
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   d6.Initialize(d1pins.D6, d6.MODE_OUTPUT)   'not yet used
   d6.DigitalWrite(False)
   If wifi.Connect2("TP-LINK_507A4F","02507A4F") = False Then
     Log("Error connecting to router!")
     Return
   End If
  'broker address is: 192.168.0.6:51042. Change as needed.
   mqtt.Initialize(client.Stream, Array As Byte(192, 168, 0, 100), _
     1883, "esp2", "Mqtt_MessageArrived", "Mqtt_Disconnected")
   Connect(0)
End Sub


Sub Connect(unused As Byte)
  If mqtt.Connect = False Then
  Log("trying to connect again")
  CallSubPlus("Connect", 1000, 0)
  Return
  End If
  Log("Connected to broker")
  mqtt.Subscribe("esp", 0)
 
End Sub

Sub Mqtt_MessageArrived (Topic As String, Payload() As Byte)
   Log("Message arrived. Topic=", Topic, " payload: ", Payload)
   Log(bc.HexFromBytes(Payload))
   If Topic= "esp" Then
       Dim Objects() As Object = Ser.ConvertBytesToArray(Payload, ObjectsBuffer)
   Log(Payload.Length)
   Log(Objects.Length)
   Log(ObjectsBuffer.Length)
   End If
End Sub

Sub Mqtt_Disconnected
  Log("Disconnected")
  mqtt.Close
  Connect(0)
End Sub
 
Upvote 0
Top