Coverting ASCII code to value

sbscott

Senior Member
I am using a MaxSonar module to determine distances with a 28x2. I capture the code but not sure how convert it to a value I can do something with. The plan is to use this to determine a "clear path" for the object-avoiding bot.

The output is an ASCII capital “R”, followed by three ASCII character digits representing the range in inches up to a maximum of 255, followed by a carriage return (ASCII 13). The baud rate is 9600, 8 bits, no parity, with one stop bit.

After reading some threads, I am still not clear on this. I tried new code


Code:
h:
debug
serin a.3,n9600,("R"), b0,b1,b2
w0 = b2 * 10 + b1 * 10 + b0 - 5328
pause 400
goto h
If w0 is the decimal value, it does not make sense. Where am I going astray here?
 
Last edited:

BeanieBots

Moderator
b0 and b1 are the two bytes which make up w0. So your calculation is mucking up your original values.
Try replacing w0 with a word variable that does not include the bytes you are using. eg w2.
 

westaust55

Moderator
Also had a look at one of the MAXSonar module datasheets (would be nice if folks gave a link to the exact unit being used) and there was no indication as to which digit units or hundreds as sent first.
The sheet I looked at just states:
“The output is an ASCII capital “R”, followed by three ASCII character digits representing the range in inches up to a maximum of 255, followed by a carriage return (ASCII 13).”

If this was intended by the manufacturer to go straight to a display I would have thought the first ASCII character was the hundreds digit.

So you might also try:

Code:
h:
debug
serin a.3,n9600,("R"), #b0, #b1, #b2
w4 = b0 * 10 + b1 * 10 + b2
pause 400
goto h
 

hippy

Ex-Staff (retired)
The output is an ASCII capital “R”, followed by three ASCII character digits representing the range in inches up to a maximum of 255, followed by a carriage return (ASCII 13).
I'm not sure why ...

SERIN A.3, N9600,("R"), #b0

wouldn't work, and I could have sworn that's what you originally had in post #1 but I really can't remember now !
 

sbscott

Senior Member
I am happy to report that westaust55's suggestion worked! I now have the ability to do some ranging with my bot. Now to figure out how it integrate it into the overall operation.

I will start a new post once I get the critter running.

Thanks as always!
 

westaust55

Moderator
Great to see/read that you have it working.
Don't forget to provide a link to your external hardware in future for ease of others helping you.
 
Top