B4R Question Serial Hex

atiaust

Active Member
Licensed User
Longtime User
Hi All,

I am trying to send 4 pieces of hex data as bytes of data out on a serial port.

I am unsure if the following block of code is correct?
B4X:
astream.write(ser.ConvertArrayToBytes(Array(0x55,0x1A,0x00,0x91)))

When I try to use the following block copied from the forum I get an error:
"Cannot cast type: {Type=Byte,Rank=1,RemoteObject=True} to: {Type=Object,Rank=1, RemoteObject=True}"

B4X:
astream.Write(ser.ConvertArrayToBytes(bc.HexToBytes("551A0091")))

Can some point me in the right direction.

Thanks
 

emexes

Expert
Licensed User
Given that astream is probably an AsyncStreams and thus probably sending bytes over a communication channel, perhaps this is where you were headed:
B4X:
'''astream.write(ser.ConvertArrayToBytes(Array(0x55,0x1A,0x00,0x91)))
astream.Write(Array As Byte(0x55, 0x1A, 0x00, 0x91))
The .Write2 method lets you send part of the array, rather than all of it. This is useful when you have a fixed size buffer for constructing packets to send, and the packet is smaller than the buffer, eg:
B4X:
'global vars
Dim TxBuffer(100) As Byte
Dim TxLength As UInt = 0

Sub AddTx(B As Byte)
    If TxLength < TxBuffer.Length Then
        TxBuffer(TxLength) = B
        TxLength = TxLength + 1
    End If
End Sub

AddTx(0x55)    'U
AddTx(0x1A)    'Ctrl+Z aka EOF (?)
AddTx(0x00)
AddTx(0x91)    'a

astream.Write2(TxBuffer, 0, TxLength)    'only writes 4 byte packet, not the entire 100 byte array
TxLength = 0    'clear buffer for next packet
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Thank you both for your replies.

Yes I am sending a Hex string over AsyncStreams and then reading the returned data.

This will work for me.
 
Upvote 0
Top