Android Question Sending values larger than 0x7F through Asyncstreams

Alejandro Gerosa

New Member
Licensed User
Hi, I am working with XBEE in API mode and frame data requires values larger than 0x7F. "IE:0xA2"

When I tried with Chr(0xA2), but it made a negative two byte number.

Finally I've tried making a list to store the whole frame and then converted it to bytes, but it sent 4 byte values.

B4X:
            Dim StreamTX(Frame.Size) As Int
            For i= 0 To Frame.Size-1
                StreamTX(i)=Bit.And(0xff,Frame.Get(i))
            Next
            by=bc.IntsToBytes(StreamTX)
            Main.AStreams.Write(by)
StreamTX(): 0x7E, 0x00, 0x23, 0x10, 0x01, 0x00, 0x13, 0xA2, 0x00, 0x41, 0x7B, 0x32

This is the serial output
00 00 00 7E 00 00 00 00 00 00 00 23 00 00 00 10 00 00 00 01 00 00 00 00
00 00 00 13 00 00 00 A2 00 00 00 00 00 00 00 41 00 00 00 7B 00 00 00 32

Is there a way to send only one byte? Thanks
 
Last edited:

Alejandro Gerosa

New Member
Licensed User
Please use [code]code here...[/code] tags when posting code.

You don't need to do anything special.
B4X:
Main.Astreams.Write(Array As Byte(0xff, 0xfe, 0x34)) 'this will send the correct bytes

Thank you Erel, values of the stream are not constant so I can not use a fix declaration as you suggested (or more likely, maybe I didn't undertstand you code)

I have worked around with this code

I've put the stream in a list named TramaTX and then...


B4X:
Dim StreamTX(TramaTX.Size) As Int
            For i= 0 To TramaTX.Size-1
                StreamTX(i)=Bit.And(0xff,TramaTX.Get(i))
            Next
            by=bc.IntsToBytes(StreamTX)    'Here it's like 00 00 00 7E 00 00 00 00 00 00 00 23 00 00 00 10 00 00 00 01 00 00 00 00 00 00 00 13 00 00 00 A2 00 00 00 00 00 00 00 41 00 00 00 7B 00 00 00 32
            For i=0 To (by.Length-1)/4
                by(i)=by(4*i+3) 'Rearranged 7E 00 23 10 01 00 13 A2 0 41 7B 32 ... and extra garbage
            Next
            Dim by0(by.Length/4) As Byte
            bc.ArrayCopy(by,0,by0,0,by.Length/4) 'Cleaning up extra garbage
            Main.AStreams.Write(by0) 'Sending 7E 00 23 10 01 00 13 A2 0 41 7B 32
'this way it worked like a charm

but now I am facing Astreams_NewData (Buffer As Byte()) it's giving two byte negative numbers above 127 and I have to convert 2's complement.
 
Last edited:
Upvote 0

Alejandro Gerosa

New Member
Licensed User
In most cases you can ignore the fact that bytes are signed.

If you do need to get the unsigned value then you can do it with:
B4X:
Dim v As Int = Bit.And(0xff, YourByte)

Thanks Erel: I did it with your code alrigth

B4X:
Dim v As Int = Bit.And(0xff, YourByte)

taken from https://www.b4x.com/android/forum/threads/signed-unsigned-bytes-confusion.61290/

Mi reception code is this.
B4X:
Sub AStreams_NewData (Buffer() As Byte)
    Dim bu(Buffer.Length) As Int
    For i = 0 To Buffer.Length-1
        bu(i)=Bit.And(0xFF, Buffer(i))
    Next   
    XBEEAPI.NewtTransmision(bu)
      Buffer=Null
End Sub

Finally it is working fine. Thanks a lot again.
 
Upvote 0
Top