PDA

View Full Version : Clamping a value to 16bits.



HenrikOlsson
- 17th January 2010, 14:53
Hi,
Sorry for the vague title.... Here's the scenario:

I'm receiving a variable length string over serial, starting with a letter, continuing with any number of decimal digits and ending with a CR. I stuff the incomming characters into an array and increments a "pointer" as I fill it up.

I need to take the value between the letter and the CR and put that into a variable, I use the "pointer" to determine how many characters I need to grab and then use something like:

ArrayRead RxBuffer [SKIP 1, DEC5 myValue] 'Skip the letter, then grap 5 digits.
No problem so far. But, since myValue is a word I need to make sure that I don't overflow it and I'm trying to come up with a solution to do that. If I receive more than 5 digits it's a done deal but if I receive 5 digits I need to know if those digits represents more than what can be stored in a word.

Anyone got a slick way of doing it?

Thanks!
/Henrik.

anonymouse
- 17th January 2010, 15:30
split your buffer into two words variables using skip, then do your test, e.g 90500 var1=905 var 2=00
all the best

Hi,
Sorry for the vague title.... Here's the scenario:

I'm receiving a variable length string over serial, starting with a letter, continuing with any number of decimal digits and ending with a CR. I stuff the incomming characters into an array and increments a "pointer" as I fill it up.

I need to take the value between the letter and the CR and put that into a variable, I use the "pointer" to determine how many characters I need to grab and then use something like:

ArrayRead RxBuffer [SKIP 1, DEC5 myValue] 'Skip the letter, then grap 5 digits.
No problem so far. But, since myValue is a word I need to make sure that I don't overflow it and I'm trying to come up with a solution to do that. If I receive more than 5 digits it's a done deal but if I receive 5 digits I need to know if those digits represents more than what can be stored in a word.

Anyone got a slick way of doing it?

Thanks!
/Henrik.