receive 2 bytes and add them together before plotting

ejleiss

Active member
Joined
Oct 1, 2012
Messages
37
Programming Experience
1-3
Hello,

I am receiving a stream of data into a serial port and picking out 2 specific bytes that are returned to ReceivedText(ByVal [text] As Byte). I would like to then add these two bytes together to be plotted as a 16 bit value. Anyone have any code suggestion to add these bytes together. Do I need a loop?
 
Private Function Combine(high As Byte, low As Byte) As Short
    Return CShort(high) << 8 Or CShort(low)
End Function
The '<<' is a bit-shift operator. In this case, it moves each bit in the binary representation of the specified number up 8 places. If you started with a Byte with value 20, that would be 00010100 in binary. Converting that to a Short adds 8 more bits to give 0000000000010100 and then shifting the bits 8 places to the left gives 0001010000000000. As you can see, the high-order 8 bits of that value are equivalent to the 8 bits of the original Byte. That is equivalent to multiplying the number by 2^8. The bitwise Or operator then combines the second Byte by pushing it into the low-order 8 bits.
 
or maybe you meant something like this:
Dim value = BitConverter.ToInt16(bytes, 0)
 
or maybe you meant something like this:
Dim value = BitConverter.ToInt16(bytes, 0)

Oh goodness! Why on earth did I not think of that? :blush:
 
Back
Top