B4R Question Asyncstream convert to right type e.g Int, String, Boolean

rwblinn

Well-Known Member
Licensed User
Longtime User
In an experiment, data is send from Node-RED running on a Raspberry Pi via the serial line to an Arduino.
The data send by Node-RED is a comma separated string, e.g. 12,string1,true.

What is the best way to get the string items converted to the right types, e.g. Int, String, Boolean.

B4X:
  Private astream As AsyncStreams
  astream.Initialize(Serial1.Stream, "Astream_NewData", "Astream_Error")

B4X:
Sub Astream_NewData (Buffer() As Byte)
If Buffer.Length < 3 Then Return
Dim bc As ByteConverter
Dim idx As UInt = 0
For Each s() As Byte In bc.Split(Buffer, ",")
  Log(idx,"=", s)
  Select idx    ' Integer
    Case 0
     ' How to convert s to Int
     Dim number1 as Int = ???
    ' String
    Case 1
     ' How to convert s to String
      Dim string1 as String = ???
    ' Boolean
    Case 2
     ' How to convert s to a boolean
      Dim bool1 as Boolean = ???
  End Select
  idx = idx + 1
Next
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Missed that. Thought @rwblinn is using B4J on the RPi.

You can use ByteConverter for that. In most cases it is better (saves memory) to use an array of bytes instead of string.

You can convert to string with bc.BytesToString.

To int:
B4X:
Dim i As Int = bc.BytesToString(s)) 'need to first convert to string and then the compiler will take care of parsing to int

How does the boolean string looks like?
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Thanks for help.
Had a solution first using B4J with JSON, but Node-RED also has a Serial Node, thus made a direct connection between Node-RED and the Arduino via Serial Node
  • Input = receiving data from B4R using async.write to build a JSON string being converted and parsed by Node-RED (working fine).
  • Output = send a comma seperated string with various values as mentioned earlier (solved, see next)
Solved = Test Code Snippet
Note: Boolean changed to 0|1
Buffer is a string containing Integer,String,Integer
B4X:
Sub Astream_NewData (Buffer() As Byte)
  Dim bc As ByteConverter
  Dim idx As UInt = 0
  For Each s() As Byte In bc.Split(Buffer, ",")
    Dim string1 As String = bc.StringFromBytes(s)
       Log(idx,"=", string1)
       Select idx
         Case 0
           Dim number1 As Int = string1
           Log(number1)
         Case 1
           Log(string1)
         Case 2
           Dim x As Int = string1
           ' Updated: See Post #6
          Dim bool1 As Boolean = x = 1
          Log(bool1)
       End Select
       idx = idx + 1
   Next
End Sub
 
Last edited:
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
YES, for the solution using B4R + B4J + Node-RED did use the B4RSerializer with MQTT e.g.
B4R to B4J
B4X:
' Send the data via the serial line
astream.Write(ser.ConvertArrayToBytes(Array(Temperature, Pressure, PressureSeaLevel, Altitude)))
B4J to Node-RED via MQTT
B4X:
Sub Astream_NewData (Buffer() As Byte)
   Dim data() As Object = ser.ConvertBytesToArray(Buffer)
   If data.Length < 4 Then Return
   If mqttConnected Then
     Dim value As String = data(0)
     mqtt.Publish(mqttTopicTemperature, value.GetBytes("UTF8"))
...

But for B4R + Node-RED via serial line (no network) found parsing JSON in Node-RED is an easy way for handling messages.
Will share my Arduino + Raspberry Pi + Node-RED experiment soon ("just" finalising the Node-RED charts).
 
Upvote 0
Top