B4J Question AsyncStreamText class for B4J

walterf25

Expert
Licensed User
Longtime User
Is there any class such as the AsyncSreamText class in B4A but for B4J, i'd really like to know, i'm working on a simple tcp server, and the data being received by the server is a line of text with "CRLF" at the end of each line.

Thanks All,
Walter
 

walterf25

Expert
Licensed User
Longtime User
Many classes of b4a run ok in b4j. Try it (also tick library jRandomAccessFile).
Hi derez, i actually tried that prior to posting my question, but it did not work, the reason is that in the New_Data event the data received is not received one character at a time, but instead a full line of text is received, so i'm not sure how parse the strings of text, sometimes one single line is received and others 3 lines of text are received in one.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
AsyncStreamsText class should work in B4J as is.
Hi Erel, i think i understand where the problem is, i'm already using the asyncstreamtext class in the b4a application that receives the data from the gps device, as i mentioned earlier, the mobile is connected through a usb serial cable to a gps device which sends the text lines one by one.

The AsyncstreamText class takes care of detecting the end of line character in this case, then re-sends the line of text received to the tcp server, now the reason i don't think the asyncstreamtext class does not work in this case is because the data received on the tcp server has already been processed by the asyncstreamtext class on the mobile device, in other words the data being received on the tcp server is already a fully formed text line, and is not received as one character at a time, which is why the New_Text event is never raised.

this is how one line of text is received on the tcp server
and it looks like there are actually more than 3 lines in one
!AIVDM,1,1,,B,13EelUgP00Oipk:Hkv:00?w`0HOA,0*3F!AIVDM,1,1,,A,14S7ed000cOjkU8I4`7tbr1d0HOO,0*55!AIVDM,1,1,,A,1000g7wP01Oiw2THkRO8Bgw`05I@,0*5E!AIVDM,2,1,0,B,53:c@:02>jtH?@dK:208u8pPthn2222222222216=@<5:4K1?AhRC0PC,0*07!AIVDM,2,2,0,B,p88888888888880,2*6F!AIVDM,1,1,,A,18151`P002wijADHmNR:8aM`0D0V,0*0C!AIVDM,2,1,1,A,54Shfd02;?iUK84s>20iT@T62222222222222216:`?6:4ITj:hRC0PC,0*4C!AIVDM,2,2,1,A,p88888888888880,2*6D!AIVDM,1,1,,B,33GmFd0OiEwj43`Hkh19>7Sd0Q3A,0*6A!AIVDM,1,1,,B,4028jFiupKcDnOoOv>HkCJg...

but they should be received like this
!AIVDM,1,1,,B,13EelUgP00Oipk:Hkv:00?w`0HOA,0*3F
!AIVDM,1,1,,A,14S7ed000cOjkU8I4`7tbr1d0HOO,0*55
!AIVDM,2,1,0,B,53:c@:02>jtH?@dK:208u8pPthn2222222222216=@<5:4K1?AhRC0PC,0*07


but i want to be able to receive them as individual lines, if i save the lines being sent to a text file on my device each line of text gets saved the way they should, which tells me that i'm sending the data with the CRLF character at the end, but when received on the tcp server they are not seeing like that

hope this makes sense.

How can i fix this problem?
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
and is not received as one character at a time, which is why the New_Text event is never raised.
AsyncStreamsText doesn't expect the text to arrive one character at a time.

The problem in this case is that there are no end of line characters in the text you are sending. My guess is that you are saving the text with TextWriter.WriteLine which adds the end of line character.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
AsyncStreamsText doesn't expect the text to arrive one character at a time.

The problem in this case is that there are no end of line characters in the text you are sending. My guess is that you are saving the text with TextWriter.WriteLine which adds the end of line character.
Hi Erel, i know that AsyncStreamText doesn't expect the text to arrive one character at a time, i'm talking about asyncstream library which is used to concatenate each character received in Sub astreams_NewData(Buffer() as byte) event in the Asyncstreamstext class

B4X:
Private Sub astreams_NewData (Buffer() As Byte)
    Dim newDataStart As Int = sb.Length
    sb.Append(BytesToString(Buffer, 0, Buffer.Length, charset))
    Dim s As String = sb.ToString
    Dim start As Int = 0
    For i = newDataStart To s.Length - 1
        Dim c As Char = s.CharAt(i)
            If i = 0 AND c = Chr(10) Then '\n...
            start = 1 'might be a broken end of line character
            Continue
        End If
        If c = Chr(10) Then '\n
            CallSubDelayed2(mTarget, mEventName & "_NewText", s.SubString2(start, i))
            start = i + 1
        Else If c = Chr(13) Then '\r
            CallSubDelayed2(mTarget, mEventName & "_NewText", s.SubString2(start, i))
            If i < s.Length - 1 AND s.CharAt(i + 1) = Chr(10) Then '\r\n
                i = i + 1
            End If
            start = i + 1
        End If
    Next
    If start > 0 Then sb.Remove(0, start)
End Sub

each line being sent from the text file to the mobile device via the usb serial port is ended with CRLF, but it seems that the AsyncstreamsText class removes this end of line character.

Am I right?

Thanks,
Walter
 
Upvote 0
Top