Android Question AsyncStreamText Class

walterf25

Expert
Licensed User
Longtime User
Hello All, i'm working on a project which requires a mobile device to be connected to an usb serial cable and which receives text strings from a gps device, the mobile re-sends the data received to a TCP server.

All of this works just the way it should with the exception that i now need to implement a feature where if the mobile's mobile connection or wifi connection drops, the data being received should be saved to a temporary file, and when the mobile device re-gains connection it should start re-sending the saved data saved in the temp file.

The issue i'm having is that while trying to save the information received, it doesn't save all the information, for example i'm using a program called Tera Term which is a serial terminal which can send and receive information via a serial port to and from a pc or mobile device.

The text file i'm using to send the data to the mobile via the USB library contains 52,946 bytes, i send line by line to the device.

I'm using Erel's AsyncStreamText class which makes it easier to handle this kind of data since is pure text data.

I know for a fact that the information from the file is being received on the device since i can log every single line being received, the problem is when i try to save every single line received on a text file.

When i open the text file where the data should be saved, everytime only 40,960 bytes are saved, i can't figure out why this is, if anyone here please could maybe point me in the right direction i will really appreciate it, to me it sounds by the amounts of bytes written on the file (40,960) as if there's a limitation or a buffer that can maybe be expanded somehow.

Maybe Erel can comment on this, i really need to able to saved any amount of data coming through the usb port on the device.

Thanks All!
Walter
 

stevel05

Expert
Licensed User
Longtime User
Without seeing the code it is difficult to help, are you using an outputstream to save the file, and are you closing it once it's written?
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Without seeing the code it is difficult to help, are you using an outputstream to save the file, and are you closing it once it's written?
Code is very standard but here it is!

so here is where i look to see whether the usb connection is successful, if it is then i initialize the astream varaible

B4X:
Sub Start_Serial As Boolean
'Start_Serial=False
        If usb.UsbPresent = usb.USB_NONE Then
        Log("Msgbox - no device")
        'Msgbox("No USB device or accessory detected!", "Error")
        Log("Msgbox - returned")
        Return
    End If
    Log("Checking permission")
    If (usb.HasPermission) Then
    '    Msgbox(usb.DeviceInfo, "Device Information")
        Dim dev As Int
        dev = usb.Open(34800)       
        If dev <> usb.USB_NONE Then
            Log("Connected successfully!")
            SERIAL_CONNECTED = "Conectado"
            astreams.Initialize(Me, "astreams", usb.GetInputStream, usb.GetOutputStream)
        Else
            Log("Error opening USB port")
            SERIAL_CONNECTED = "Desconectado"
        End If
    Else
        usb.RequestPermission
    End If
   
    If astreams.IsInitialized Then
    astreams.Write("WALTER FLORES" & Chr(10) & Chr(13))
    astreams.Write("GENESIS FLORES" & Chr(10) & Chr(13))
    End If
CallSubDelayed3(Main, "updatescreen", SERIAL_CONNECTED, "")
End Sub

then here is the sub that checks for when a new string is received

B4X:
Sub astreams_NewText(Text As String)
buftext = buftext + 1
tmpfile.WriteLine(buftext & " " & Text)
Log(buftext & " " & Text)
  Dim data() As Byte
  data = Text.GetBytes("UTF8")
  If CONNECTION_STATUS = True AND reconnected = False Then
  ASocketStream.Write(data)
  Else If CONNECTION_STATUS = False AND reconnected = False Then
  'tempfile.WriteLine(Text)
  tmpfile2.WriteLine(Text)
  tmpdata.Add(Text)
  End If
End Sub

as you can see i even save the data as it comes in, the strange thing is that i can see all the data being received if I log it with
Log(buftext & " " & text)

so if i can see the data being received on the log it should save it at the same time to the file, the thing is that when i open the file not all the data gets saved, out 1059 lines of text only 930 get saved.

this is how i initialize the temp file
B4X:
Dim tmpfile As TextWriter
tmpfile.Initialize(File.OpenOutput(File.DirDefaultExternal, "tmp.txt", False))

Hope this makes sense, and thanks for your time!

Walter
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I can't see in the code where you are closing the file:
B4X:
tmpfile.close
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
I can't see in the code where you are closing the file:
B4X:
tmpfile.close
I didn't include it, but is there, i don't see why that would cause for the entire data not to be saved to the file?

Am i missing something?

:)
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
If you don't close the file, any thing not yet written will be lost. the data doesn't necessarily get written straight away, so if there is a way that the close method doesn't get called, you could easily lose the end of the file.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
If you don't close the file, any thing not yet written will be lost. the data doesn't necessarily get written straight away, so if there is a way that the close method doesn't get called, you could easily lose the end of the file.
Ah, really, wow, ok thanks for that information, i had no idea that was the case, uhmmm ok, let me try that then

Thanks for your Help Steve

Walter
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Walter this line is in error
B4X:
dev = usb.Open(34800)
....it should be 38400
Hi Tom, Yes you are correct, i'm actually using 9600 instead, for some reason 38400 doesn't work!

Thanks,
Walter
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
I'm also unclear about the following code:
B4X:
  'tempfile.WriteLine(Text)
  tmpfile2.WriteLine(Text)
that's irrelevant, don't pay attention to that line of code.
What i was trying to do is save the data being sent to the tcp server while there is mobile connection, and the tmpfile2 is being used to save the data being received while there is no mobile connection so i can re-send it later when the mobile connection is re-established.
 
Upvote 0
Top