loading
 
merge variables
Martin Hug from Spain  [6 posts]
10 year
Hello I need to merge two 8 bit values to a 16 bit value.
The line> COG_X_10 = Lo | Hi<<8 gives an error.

My code is:
Hi = GetVariable("COG_X")/256
Lo = GetVariable("COG_X")
COG_X_HI = Hi
COG_X_LO = Lo
COG_X_10 = Lo | Hi<<8

SetVariable"COG_X_HI", COG_X_HI
SetVariable"COG_X_LO", COG_X_LO
SetVariable"COG_X_10", COG_X_10

Thanks Martin
Steven Gentner from United States  [1446 posts] 10 year
Martin,

You may have better luck trying the CScript module as VBScript does not have really easy bit operations. Still, I think you can try

Hi = CInt(GetVariable("COG_X") / 256 )
Lo = GetVariable("COG_X") AND 255

COG_X_HI = Hi
COG_X_LO = Lo
COG_X_10 = (Lo * 256) + Hi

SetVariable"COG_X_HI", COG_X_HI
SetVariable"COG_X_LO", COG_X_LO
SetVariable"COG_X_10", COG_X_10

which is a bit different from yours since I figured you are wanting to switch endian (ie swap the bytes) in the number and your's just regenerated the same number. Specifically the COG_X_10 was swapped around.

Note, if you plan to use this is the serial module to send it somewhere you can switch the \ to / and that switches the transmitted endian at least in the serial module.

STeven.

This forum thread has been closed due to inactivity (more than 4 months) or number of replies (more than 50 messages). Please start a New Post and enter a new forum thread with the appropriate title.

 New Post   Forum Index