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
it's faster since you don't have the overhead of calling and ending the subs which does extra stack operations but for 64K we probably talk about mili or nanoseconds.
 
Upvote 0

ElliotHC

Active Member
Licensed User
Those kinds of seconds I like!

Why is it that if I feed it back in to a byte array I lose it again?


B4X:
        For x = 0 To job.GetInputStream.BytesAvailable - 1
            Bytes_to_Send(x) = Bit.And(bytes(x),255)
            Log(Bytes_to_Send(x))
        Next

When I look at this it goes back as before.

The reason this is an issue is that I need to use this Sub to send it out of the BT

B4X:
Public Sub SendMessage(msg() As Byte)
    messagesToSend.Add(msg)
    If messagesToSend.Size > 1 Then
        manager.WriteData(ServiceId2, WriteChar2, msg)
    End If
End Sub

So I am trying to use this:

B4X:
For x = 0 To job.GetInputStream.BytesAvailable - 1
            Bytes_to_Send(x) = Bit.And(bytes(x),255)
            Log(Bytes_to_Send(x))
        Next
        
  
        CallSub2(Starter, "SendMessage", Bytes_to_Send)

But I'm only sending Characters again :(
 
Upvote 0

ElliotHC

Active Member
Licensed User
upload_2019-5-29_12-2-35.png
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
Why is it that if I feed it back in to a byte array I lose it again?

each time you read out a byte from a byte array it's signed again so you need to apply that bit.and again.

if you store it into an int array you will read out the value as you stored it.

what is the highlighted yellow line in the log and what is it supposed to be?
 
Last edited:
Upvote 0

ElliotHC

Active Member
Licensed User
That's my data coming in to the Micro via the Bluetooth serial module.. So B4A App(BT Transparent Serial Connection) <OTA> BT Module > PICMICRO

So in this sub:

B4X:
Public Sub SendMessage(msg() As Byte)
    messagesToSend.Add(msg)
    If messagesToSend.Size > 1 Then
        manager.WriteData(ServiceId2, WriteChar2, msg)
    End If
End Sub

How do I get that byte array 'msg' to be unsigned bytes when it goes out?
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
I guess you'll need to do it on the other side when/where you read it out again.

I guess the writedata sends it correct unless the source (bytes array) has incorrect data.
 
Upvote 0

ElliotHC

Active Member
Licensed User
I go from here:

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

        CallSub2(Starter, "SendMessage", bytes)

To here:

B4X:
Public Sub SendMessage(msg() As Byte)
    messagesToSend.Add(msg)
    If messagesToSend.Size > 1 Then
        manager.WriteData(ServiceId2, WriteChar2, msg)
    End If
End Sub

I don't see how I can get it to send out in individual bytes.

Surely If I just to a for next loop and load the Bytes array in to the msg array I'm going to face the same issue where it's sending it out as a string?
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
but the log that you posted where is that coming from?

is that pulled from that micro controller's console?
 
Upvote 0

ElliotHC

Active Member
Licensed User
Yes, the programmer is connected to the PIC and I'm sending it out of the PIC. It's sending the decimal value of the bytes coming in. That is 100% sound as it doesn't to any singing or anything like that.
 
Upvote 0

ElliotHC

Active Member
Licensed User
I've been coding with the Firmware Basic compiler since 2005 so I know my way around that, VB since about 2008. I've only been using B4A and B4J for a short while so I know little about how it works.

I can tell you that I'm only sending ASCII characters using this method.. It's not been a problem up to now as the App data has been text.. Now I need to send byte values 0-255 as I'm sending the MIcro HEX file which is encrypted.

The other option is to send the values as comma separated text values.. But seriously? That will make the file 4 times larger.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
it seem to be correct. You're confused with hex & int values.

B4X:
Dim t As String="84 72 73 83 32 73 83 32 84 72 69 32 70 73 82 77 87 65 82 69"
Dim data() As String
data=Regex.Split(" ",t)
t=""
For x=0 To data.Length-1
 t=t & Chr(data(x))
Next
Log(t)

the log shows: THIS IS THE FIRMWARE

which is also seen in a screenshot of your in an earlier post.
 
Upvote 0

ElliotHC

Active Member
Licensed User
I'm sending "THIS IS FIRMWARE<009>12<240>45" but you can't see the 9 or 240 in the log

upload_2019-5-29_13-35-4.png

upload_2019-5-29_13-35-24.png

What is actually coming in:

upload_2019-5-29_13-35-53.png
 

Attachments

  • upload_2019-5-29_13-34-29.png
    upload_2019-5-29_13-34-29.png
    36.1 KB · Views: 191
Upvote 0

ElliotHC

Active Member
Licensed User
It's almost as if it's stopped sending the data or stopped filling msg when it hits a byte that isn't stringable.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
are you sure that part of the data is in the file that is on that webserver and that you don't pull in a cached version of that file
 
Upvote 0

ElliotHC

Active Member
Licensed User
I think that when I send bytes over to here

upload_2019-5-29_13-46-11.png


Using this

upload_2019-5-29_13-46-34.png


I am signing the bytes? This means that it's not sending the 9, in fact it stops filling the array or stops sending.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
compare log( job.GetInputStream.BytesAvailable ) with the filesize on the server
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
I am signing the bytes? This means that it's not sending the 9, in fact it stops filling the array or stops sending.

you're doing not but just logging the data. the data is still signed in the array which is normal.

the problem is somewhere else
 
Upvote 0
Top