Android Question Reading a data file 0-255

ElliotHC

Active Member
Licensed User
Upon looking through the posts in order to find out how in B4A to download and read a file, I have noticed that there may be some issues when importing the data.

In my data file I have 0-255, many of these fall outside of the standard ASCII table, some would call it extended ASCII.

My hardware which is coded using a different Basic compiler, requires that I send it a 2k data bundle created as a file (e.g. 123.ABC) by a VB App I've written.

This data file contains bytes 0-255 in value and I need to read this file which is located on my webserver (http://mydomain.com/123.ABC).

The B4A App which does all the control of the hardware via bluetooth (which is working perfectly), also needs to be able to read the data file and pass it to the hardware through the transparent serial connection that I'm using..

So basically and in as simple form as possible, I need to read from the web location, load that data in to an array that I can parse with the B4A App and send to the hardware.

Erel has given me this example, but I am not sure how to implement it.

B4X:
Dim lines As List = File.ReadList(...)
Dim s As String = File.ReadString(...)

Will that string give me access to the full 0-255 byte?
 

sorex

Expert
Licensed User
Longtime User
also check that the bytes() array is big enough and try

B4X:
job.GetInputStream.ReadBytes(bytes,0,bytes.Length)
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
by the way... I see that your msg has 7 00's prefixed. maybe you just need to add 7 extra bytes to the msg (lenght)?

or figure out where those 00's are coming from and remove them.
 
Upvote 0

ElliotHC

Active Member
Licensed User
I think I have found an issue.

When I try and send this which should be 74 bytes.

B4X:
Dim Bytes_40 As String
        Bytes_40 = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        CallSub2(Starter, "SendMessage", Bytes_40.GetBytes("utf8"))
        CallSub2(Starter, "SendMessage", Bytes_40.GetBytes("utf8"))

I get this which is only 40...



Shows that my PIC has no issues getting 40, so why are these two strings getting cut off at the 20 byte point?
 
Upvote 0

ElliotHC

Active Member
Licensed User
by the way... I see that your msg has 7 00's prefixed. maybe you just need to add 7 extra bytes to the msg (lenght)?

or figure out where those 00's are coming from and remove them.
That was just some stray 0's, I've now set my code only to output when it sees more than one byte coming in..
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
sorry my work day ended and I just got behind my laptop at home.

I'll see if I can give you an example.
 
Upvote 0

ElliotHC

Active Member
Licensed User
Thanks, I did it but it's messy.

B4X:
        Log(job.GetString)
        
        
        
        job.GetInputStream.ReadBytes(bytes,0,job.GetInputStream.BytesAvailable)
        Dim Bytes20(20) As Byte
        Log("Data Length=" & (job.GetInputStream.BytesAvailable - 1))
        
        Dim Loop_Counter As Int
        
        For x = 0 To job.GetInputStream.BytesAvailable - 1



            For X2 = 0 To 19' X2_Top
                Loop_Counter = Loop_Counter + 1
                Bytes20(X2) = bytes(x + X2)

            Next
                
            CallSub2(Starter, "SendMessage",Bytes20)
            ' Wait for the next send byte request
            For X3 = 0 To 2
                If Packet_Requested = False Then   
                    X3 = 1
                    Else
                        Log("Packet Recieved")
                End If
            Next
            Packet_Requested = False
            
            x = x + 20
        Next
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
ok, here my attempt but I couldn't verify if it's correct.

B4X:
Public Sub SendMessage(msg() As Byte)
    Dim xv As Int
    For x=0 To msg.Length-1 Step 20
        xv=20
        If msg.Length-x<20 Then    xv=msg.Length Mod 20
        Dim mybytes(xv) As Byte
        For b=0 To xv-1
            mybytes(b)=msg(x+b)
        Next
        manager.WriteData(ServiceId2, WriteChar2, mybytes)
    Next
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…