B4R Question Serial Hex

Discussion in 'B4R Questions' started by atiaust, Sep 17, 2019.

  1. atiaust

    atiaust Active Member Licensed 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?
    Code:
    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}"

    Code:
    astream.Write(ser.ConvertArrayToBytes(bc.HexToBytes("551A0091")))
    Can some point me in the right direction.

    Thanks
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Why do you need to use B4RSerializator here?

    You can send the bytes directly:
    Code:
    astream.Write(bc.HexToBytes("551A0091"))
    If you do want to use B4RSerializator then you need to put the array of bytes in an array of objects:
    Code:
    astream.Write(ser.ConvertArrayToBytes(Array(bc.HexToBytes("551A0091"))))
     
  3. emexes

    emexes Well-Known Member 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:
    Code:
    '''astream.write(ser.ConvertArrayToBytes(Array(0x55,0x1A,0x00,0x91)))
    astream.Write(Array As Byte(0x550x1A0x000x91))
    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:
    Code:
    'global vars
    Dim TxBuffer(100As 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
     
    atiaust likes this.
  4. atiaust

    atiaust Active Member Licensed 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.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice