Shop OBEX P1 Docs P2 Docs Learn Events
Variable formatting and manipulating — Parallax Forums

Variable formatting and manipulating

JimCookJimCook Posts: 3
edited 2009-06-03 23:02 in BASIC Stamp
·Below is a simple routine grabbing some data from the HEX string (below code) which belongs to the serial output of a weather station. What I have works perfectly but here is where the problem lies:
·The variables (SerData, SerStr, Tempe) display perfectly in the debug screen·because I can format them with the DEC formatter (or for that matter HEX·and binary also) but you cannot use those formatters with the variable names to perform math on them (like SerData * 621 / 10000). Actually it cannot be formatted at all except with a very few commands which are not useful to manipulate the data. The variables contain no data or just a single letter ASCII character without the formatter or may even produce a null which clears the debug screen.·
·Any ideas you may have would be a great help.
Thanks in advance


DO
SERIN 3, Baud, [noparse][[/noparse]WAIT ("!!"), HEX4 SerData]
PAUSE 500
DEBUG· CR, "Wind",· DEC4 SerData
SERIN 3, Baud, [noparse][[/noparse]WAIT ("!!"), SKIP 4, HEX4 SerStr]
PAUSE 500
DEBUG CR, "Direction", DEC4 SerStr
SERIN 3, Baud, [noparse][[/noparse]WAIT ("!!"), SKIP 8, HEX4 Tempe]
PAUSE 500
DEBUG CR, "Temp", DEC4 Tempe
LOOP


!!008100FA02BE0000
0000002300000080

Comments

  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-06-01 17:17
    Really? When you use the modifier HEX4 serData, PBASIC is translating the input HEX characters to the internal 16 bit binary representation that you can manipulate with math operators.

    What is this:
    >>!!008100FA02BE0000
    0000002300000080
    The numbers on the right are not a decimal translation of the numbers on the left. What is the Baud rate? The Stamp can do the Waits, Skips, and HEX4 inputs correctly only at relatively low baud rates, like 2400 and under.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • JimCookJimCook Posts: 3
    edited 2009-06-01 22:37
    Obviously I didn't explain this properly. The serin is 2400 8,N,1 and the 16 digits to the right of the dashes are not used, probably options with the weather station I don't need. The first 4 digits after the "!!" is wind speed, the next 4 digits is wind direction and the next four digits are temperature, all in hex, the last 4 are not used. The code example works fine, gets the hex value from the string and can view it's contents with a debug screen using any allowed formatter. What I need to do is get the actual value of the variable without using the formatter which Pbasic doesn't allow except in DEBUG, SERIN, I2CIN, LCDIN and OWIN.
    Thanks for your reply, Jim
  • Mike GreenMike Green Posts: 23,101
    edited 2009-06-02 01:09
    The formatter converts the hex characters into the corresponding binary values so the actual value of the variable should be what you want. If you have an input string of "!!008100FA02BE" (without the quotes) and your SERIN statement is

    SERIN 3, Baud, [noparse][[/noparse]WAIT ("!!"), HEX4 SerData]

    then SerData will contain $0081 or 129 (the same in hex or decimal) after the SERIN. You could even get all three values in one statement with

    SERIN 3, Baud, [noparse][[/noparse]WAIT("!!"), HEX4 SerData, HEX4 SerStr, HEX4 Tempe]

    After the SERIN, SerData will contain $0081, SerStr will contain $00FA, and Tempe will contain $02BE. Since the first two values are only 8 bits, you could define SerData and SerStr as bytes. Temp3 is 10 bits, so you will need a word for that value.
  • jccjcc Posts: 4
    edited 2009-06-03 01:18
    Please explain what these Hex values represent in real world units of measure. Does $0081 mean 81 or 129 miles, feet, knots, kilometers per hour, minute, seconds. Then explain the math you wish to perform prior to displaying.
  • SRLMSRLM Posts: 5,045
    edited 2009-06-03 02:11
    First off, you'll want to get rid of the concept of intrinsically tying units to the numbers. The uC has no idea and doesn't care what a number means.

    Hex is just another method to represent a number. We're taught growing up the decimal counting system (0,1,2,...,9,10,11,...,99,100,...) but there are other bases available. Hex just counts from 1,2,3,...,9,A,B,C,D,E,F,10,11,...,19,1A,1B,...

    There are lots of hex to decimal converters available online that you can use. Wikipedia is also another excellent resource.

    So, $81 means (8*16) + (1*1) or 129.
  • JimCookJimCook Posts: 3
    edited 2009-06-03 23:02
    Thanks to everyone who replied I've got it figured out with your help. Just for the curios the first 4 hex digits (SerData) is wind speed .1 KPH, the next 4 digits (SerStr) is wind direction of 0 to 255 representing 0 to 360 degrees, and the next four digits represent temperature .1 degree F. ~8 MPH, ~353° and 70.2° F
Sign In or Register to comment.