B4R Question Serial Text Data to String Variable

thetrueman

Active Member
Hi,

I am trying to receive a text string from PC like "New Text Line" in a serial port of Arduino UNO and want to have them same as string variable but searched the whole forum but if every clue I found is not acceptable in B4R. I asked DeepSeek AI and the code generated is not accepted by B4R. Thanks.

DeepSeek AI Generated B4R Code:
Sub Process_Globals
    Public Serial1 As Serial
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("Ready to receive data")
End Sub

Sub Serial1_NewData (Buffer() As Byte)
    Dim text As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    Log("Received: ", text.Trim)
    
    ' Echo back
    Serial1.Write(("Echo: " & text.Trim & CRLF).GetBytes("UTF8"))
End Sub


B4X:
'Add these libraries in Manage Libraries:
'rCore
'rSerial
'rAsyncStreams

Sub Process_Globals
    Public Serial1 As Serial
    Private astream As AsyncStreams
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    astream.Initialize(Serial1.Stream, "astream_NewData", "astream_Error")
    Log("Serial communication ready")
End Sub

Sub astream_NewData (Buffer() As Byte)
    Dim receivedText As String
    receivedText = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    Log("Received: ", receivedText.Trim)
    
    ' Echo back
    Dim response As String = "Echo: " & receivedText.Trim & CRLF
    astream.Write(response.GetBytes("UTF8"))
End Sub

Sub astream_Error
    Log("Stream error")
End Sub
 
Solution
i know 2 ways to store a string in a sub and to have this one accessible from others sub :
- GlobalStore.bas module
- rArduino_JSON
Thank you candide to show the direction and luckily yesterday I had been succeeded to have variable string instead of constant string by using GlobalStore after reading many posts. Actually I was misunderstanding that GlobalStore is related to ROM but when I studied a lot I realized that it uses RAM. Here is snippet to most easily way to use the GlobalStore.

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private AStream As AsyncStreams
    Private BC As ByteConverter
    Private SerLength As Byte = 20        'Serial Data Length (Characters Count)
    Private SerDataText As String =...

candide

Active Member
Licensed User
i know 2 ways to store a string in a sub and to have this one accessible from others sub :
- GlobalStore.bas module
- rArduino_JSON
 
Upvote 0

thetrueman

Active Member
i know 2 ways to store a string in a sub and to have this one accessible from others sub :
- GlobalStore.bas module
- rArduino_JSON
Thank you candide to show the direction and luckily yesterday I had been succeeded to have variable string instead of constant string by using GlobalStore after reading many posts. Actually I was misunderstanding that GlobalStore is related to ROM but when I studied a lot I realized that it uses RAM. Here is snippet to most easily way to use the GlobalStore.

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private AStream As AsyncStreams
    Private BC As ByteConverter
    Private SerLength As Byte = 20        'Serial Data Length (Characters Count)
    Private SerDataText As String = "Display Text" 'Serial Data to Display
End Sub

Private Sub AppStart
    Serial1.Initialize(19200)'(115200)
    AStream.Initialize(Serial1.Stream, "AStream_NewData", "AStream_Error")
'    AStream.WaitForMoreDataDelay = 5
    Log("AppStart")
    GlobalStore.Put(0, "Display Text")
    Private SerNewText As String = BC.StringFromBytes(GlobalStore.slot0)
    AddLooper("B4Rloop")
End Sub

Sub B4Rloop()
    Private SerNewText As String = BC.StringFromBytes(GlobalStore.slot0)
    log (SerNewText)
End Sub

Sub AStream_NewData (Buffer() As Byte)
    Log (Buffer)
    SerLength = Buffer.Length
    GlobalStore.Put(0,Buffer)
End Sub

@candide I just reviewed rArduino_JSON and would like to have a smart snippet as I shared above. Thanks.
 
Upvote 0
Solution
Top