Select 2 bytes out of streamed data


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:



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

I tried this without effort:

private string datain

	  device.AHRS.ReadUntil(115 + 110 + 112)  // until 1.snp
	  datain = device.AHRS.ReadUntil(115 + 110 + 112) // until 2.snp
	  ? strLastError

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


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