PDA

View Full Version : Code Problem



computer guy
09-18-2007, 05:36 AM
I have a controller and a robot.
The controller send the robot some variables and the robot acts accordingly.
However when the controller sends 1500 the robot displays it as 236.

What could be wrong with my code.


Code is attached.

Thank you http://forums.parallax.com/images/smilies/smile.gif

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Check out my robot using the propeller robot.tmcp.com.au (http://robot.tmcp.com.au)
If you offer cheap PCB fabrication, perl programming or any other helpful services please email me at.
anthonybmyatt@yahoo.com.au (mailto:anthonybmyatt@yahoo.com.au)

Graham Stabler
09-18-2007, 06:06 AM
Robot displays value and you defined this as a byte which is only 8 bits, the maximum value being 255.

Always try to think of ways to test your code for example if you had done:

Value := 1500
tv.dec(Value)

It would have still given the wrong answer and shown it was not the data transfer at fault (or not the only fault).

Graham

Mark Bramwell
09-18-2007, 10:01 AM
I am surprised it compiled.




VAR

long range, ID, Value
word mode
byte RSSI, Message[MaxStrLen], ID, Value





You have "ID" and "Value" defined twice. Which one is supposed to be there?

computer guy
09-18-2007, 02:36 PM
byte is the correct one.
I added long by accident whilst trying to fix the problem.
I never tried to compile it. As you stated it probably wouldn't.

Thank you. http://forums.parallax.com/images/smilies/smile.gif

Graham I don't get what you are trying to say.
Please explain it more clearly for the dumb people like me.

Thank you. http://forums.parallax.com/images/smilies/smile.gif

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Check out my robot using the propeller robot.tmcp.com.au (http://robot.tmcp.com.au)
If you offer cheap PCB fabrication, perl programming or any other helpful services please email me at.
anthonybmyatt@yahoo.com.au (mailto:anthonybmyatt@yahoo.com.au)

deSilva
09-18-2007, 02:55 PM
What Graham said was:

In line 95 you write:
Value := num.FromStr(Xb.RxData+4,num#dec) ' get and convert value from rest of string

As you have confirmed in your recent posting "Value" is a BYTE, so this assignment will truncate the transmitted data wrt modulus 256

Graham Stabler
09-18-2007, 05:11 PM
What deSilva was trying to say that I was trying to say is:

A variable declared as a byte has only 8 bits and can only hold numbers up to 255, you try to load 1500 into value which is a byte so the 32 bit binary number gets truncated (shortend) down to 8 bit loosing several bits. Make value a long and it will probably work.

The second thing I was trying to say was that by adding

value := 1500 just before you displayed it would have shown the same error and would have proven that the comms was not the problem.

That may have made you look at the way you were displaying it or the variable itself.

Debugging code is a matter of experimentation and thinking of ways to isolate the problem and I'm trying to show you one way you might have done that for this problem. Another way of looking at debuging is modifying the code so that one element "must work" then finding out why it doesn't.

Graham

computer guy
09-18-2007, 08:40 PM
Ah I get it now thank you.
I am assuming that I would have to make Message[MaxStrLen] a long as well.
And change 'byteMove' to 'longMove' and 'bytefill' to 'longfill'. Is this correct, because after doing so Value becomes NULL on the robot side.

Thank you http://forums.parallax.com/images/smilies/smile.gif

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Check out my robot using the propeller robot.tmcp.com.au (http://robot.tmcp.com.au)
If you offer cheap PCB fabrication, perl programming or any other helpful services please email me at.
anthonybmyatt@yahoo.com.au (mailto:anthonybmyatt@yahoo.com.au)

Graham Stabler
09-18-2007, 09:11 PM
You have now defined the value as a long and a byte.

It looks like when communicating you are sending a string of bytes and then using num.FromStr to convert that in to a number, it is that number, "value" that needs to be a float the string you send will still be bytes because you will send:

"1" "5" "0" "0" using FromStr becomes 1500

Try to think about how your program works rather than just try random things out.

Graham

computer guy
09-20-2007, 03:33 PM
All works well.

Thank you both. http://forums.parallax.com/images/smilies/smile.gif

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Check out my robot using the propeller robot.tmcp.com.au (http://robot.tmcp.com.au)
If you offer cheap PCB fabrication, perl programming or any other helpful services please email me at.
anthonybmyatt@yahoo.com.au (mailto:anthonybmyatt@yahoo.com.au)