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.
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