Android Question Reading a data file 0-255

sorex

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

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

sorex

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

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

upload_2019-5-29_15-14-33.png


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

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

sorex

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

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
 

sorex

Expert
Licensed 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
 
Top