B4J Question AsyncStreams and ASCII stream of data

fabton1963

Member
Licensed User
Longtime User
Hello,
I try to connect to a serial device using Serial lib and AsyncStreams.
B4X:
Sub btnOpen_Action
    
    
    sp.Open(cmbPort.Value)
    sp.SetParams(38400, 8,0,1)
    astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")

    btnOpen.Enabled = False

End Sub

Sub MainForm_Closed
    sp.Close
End Sub

Sub AStream_NewData (Buffer() As Byte)

    Dim i As Int   
    Dim s As String = BytesToString(Buffer, 0, Buffer.Length, "UTF-8")
    Log(s)
    For i = 0 To Buffer.Length -1   
        Log(Buffer(i) & "- "&Chr(Buffer(i)))
    Next

End Sub
The device send the following ASCII string through serial :

thi sis what I get using putty opening COM11 38400,n,8,1
{"msg":"No_Card"}
I receive the following data:
this is the log:

BytesToString --> {"[���r��Ca�X�5)

Log(Buffer(i) & "- "&Chr(Buffer(i)))
123- {
34- "
91- [
-82- ᆴ
-106- ヨ
-110- メ
18-
114- r
-67- ᄑ
-67- ᄑ
67- C
97- a
-71- ᄍ
88- X
-92- ᄂ
53- 5
41- )

How may I convert the ASCII stream to UTf-8 for reading/writing using AsyncStreams?
 
Top