A atiaust Active Member Licensed User Longtime User Sep 17, 2019 #1 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
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
Erel B4X founder Staff member Licensed User Longtime User Sep 18, 2019 #2 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")))) Upvote 0
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"))))
E emexes Expert Licensed User Longtime User Sep 18, 2019 #3 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(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: Code: '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
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(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: Code: '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
A atiaust Active Member Licensed User Longtime User Sep 18, 2019 #4 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
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.