Select 2 bytes out of streamed data


Hubauer

Recommended Posts

I want to pick 2 bytes (represents the yaw-angle = 1 channel of a AHRS) out of streamed data and convert them to a 16-bit value:

data stream:

115\110\112\18332\255\254\25216\255\24100\20101\226\254\16100

\140026600\15401\19800\108\2550401\12000\176\25563\230\2521885\115\110\11218332\.....

string begins with "snp" (see 115\112\112) the 1. Channel is byte 8 and 9.

I tried this without effort:

device.AHRS.purge()
private string datain

while(1)
   try
	  device.AHRS.ReadUntil(115 + 110 + 112)  // until 1.snp
	  datain = device.AHRS.ReadUntil(115 + 110 + 112) // until 2.snp
	  Yaw.addvalue(To.Word({Mid(datain,8,1),Mid(datain,9,1)}))
   catch() 
	  ? strLastError
	  delay(0.1)
	  device.com.purge()	  
   endcatch
endwhile

Could you give me a hint?

Link to comment
Share on other sites

OK, first doing readuntil(115 + 110 + 112) is the equivalent of doing readuntil(337), and 337 is not a valid ascii code, and not what you wanted anyway.

Next, the normal readuntil only accepts one value, so you can only scan for a single character.

Now, I believe you can use the regular expression version of readUntil by doing readUntil("snp"), but truthfully I'm embarassed to say I'm not a regular expression expert.

Assuming that works, then your logic is ok, except that your addvalue() line is wrong. You can only put numbers inside of {}, so {Mid(datain,8,1),Mid(datain,9,1)} is invalid. Personally I'd simply do this: make datain a numeric variable by eliminating the "string" in its declaration. Then do:

datain = asca(device.AHRS.readuntil(...)

instead of what you have for the 2nd snp. This makes datain an array of numbers. Then skip using To.Word since the math is super easy and do:

yaw.addvalue(datain[8] * 256 + datain[9])

it may need to be flipped depending on your byte order.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.