Shop OBEX P1 Docs P2 Docs Learn Events
Need clarification on variable declaration — Parallax Forums

Need clarification on variable declaration

Vincenzo1309Vincenzo1309 Posts: 76
edited 2009-04-21 11:03 in Learn with BlocklyProp
Dear all,

I have written a program for my robot..... this robot is able to be controlled by a user from a computer via Bluetooth connections
The G.U.I in the computer is programed in VB6.

So when the user clicked on the forward button, the Comm Control in VB6 will output a "A" over to my Basic Stamp 2.
I understand that BS2 will convert A to 65 (ASCII) automatically upon receiving it, may I know the reason why?

I have declared cmd as the variable...

cmd VAR byte(5)

If cmd = 65 then
.......................
.......................

Is the byte size of 5 correct? since it only contains the number 65? How do I define the byte size in this case?

Kindly advise.
Thanks alot!
«1

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-04-11 03:55
    The BS2 does not automatically convert A to 65. The letter A is sent by VB6 as the 8 bit value of 65 because the value 65 is the representation of the letter A according to the ASCII standard. There are no letters or digits or other characters except that we create a standard to define them. All quantities in computing are numbers that in practice are represented in binary (as bits somewhere). We just add another layer of definitions on top of that for our convenience.

    The way you use the variable "cmd", it looks like it could be just a single byte as in "cmd VAR byte". Why would you want it to be a byte array of size 5?

    The term "byte" simply refers to an 8-bit quantity. With Stamps and Parallax Stamp Basic, you have several known storage sizes:
    Bit (a single bit), Nibble (4 bits representing values from 0-15), Byte (8 bits representing values from 0-255), and Word (16 bits representing values from 0-65535). Words can sometimes be used to represent signed numbers from -32768 to 32767.
  • Vincenzo1309Vincenzo1309 Posts: 76
    edited 2009-04-11 04:05
    Hi

    Thanks for your prompt reply.
    So if I put,
    If cmd = "A" Then
    .........................
    ...........................
    It might not work right? Because I have define cmd as a byte. But what if I define cmd as a string, will it work?

    Also I am quite confused about this code: cmd VAR byte(5)
    What does 5 means?? 5 characters?
    or one byte = 8 bit number, therefore byte(5) = 8*5 = 40 bits number?
    Kindly advise as I am a novice in programming.
    Thanks alot!!
  • SRLMSRLM Posts: 5,045
    edited 2009-04-11 04:25
    There is no string datatype in PBASIC
    cmd VAR byte(5) is an array of five bytes (thats what '(5)' means- five of the previous datatype)
    In pbasic, you can only have 1,4,8, or 16 bit numbers.
  • Vincenzo1309Vincenzo1309 Posts: 76
    edited 2009-04-11 04:34
    Hi, thanks for the reply

    So when VB6 now send the number 65 to the BS2, how do I read in this 65?

    I saw this command code somewhere and I am applying it here : SERIN 0, 84, [noparse][[/noparse]STR cmd\1\CR]
    May I know what's the meaning of that code?
    I think that I am converting the 65 to A since I am taking the String equivalent of 65. but why is there a 1 there??
    Why is the CR there?

    for me i think the code can be simplified as SERIN 0, 84, [noparse][[/noparse]STR cmd]
    Am I right in this case?

    Kindly advise.
  • Mike GreenMike Green Posts: 23,101
    edited 2009-04-11 04:43
    You can say

    if cmd = "A" then

    and it will work as you might expect. The "A" gets translated by the compiler into the value 65.
    As far as the Stamp compiler (Stamp Editor) is concerned, "A" is just another way of writing 65
    as is $41 or %01000001.

    As SRLM said, there is no string datatype. There are only numeric values in the range 0-65535.
    The compiler will accept negative numbers and these are represented as 16 bit 2's complement
    values, but all of the arithmetic is on 16 bit unsigned integers.

    Stamps do have some provisions for strings in I/O statements. You can write string constants and
    they will be handled as a sequence of individual byte values. There are also provisions for byte
    arrays to be used to hold strings. This can be done in some cases on input and output using the
    STR formatter (see the description and examples in the manual).
  • Vincenzo1309Vincenzo1309 Posts: 76
    edited 2009-04-11 05:04
    Thanks everyone for your prompt replies.

    I have a doubt which I hope to clear. Recently my program don't seems to work very consistently. Sometimes it does, sometimes it doesn't. After consulting my friend, he told me to clear the memory of BS2.

    Is this a effective solution? I mean, do people do that and can get their problems solved?

    Is there a command which can clear the memory of BS2 before I download the program into it again?

    Kindly advise.
  • SRLMSRLM Posts: 5,045
    edited 2009-04-11 05:18
    To clear the memory you can just load a simple program with the two headers and END. However, when you download any program it overwrites what was there before. Most likely your dilemma is low batteries.
  • Vincenzo1309Vincenzo1309 Posts: 76
    edited 2009-04-11 05:28
    That is what I think so, since loading a program means overwriting the previous one.

    Just for curiosity, you say 2 header and an END statement.
    what headers do you mean? is it something like this below? Kindly advise

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}


    END

    I have already tried replacing the low batteries.... Hmmm.... stills need more troubleshooting....
  • SRLMSRLM Posts: 5,045
    edited 2009-04-11 06:47
    Yes, those are the headers I mentioned.
  • Vincenzo1309Vincenzo1309 Posts: 76
    edited 2009-04-11 07:22
    Hi SRLM,

    Thanks a lot!!
  • Vincenzo1309Vincenzo1309 Posts: 76
    edited 2009-04-11 08:07
    Hi Mike,

    As you say in the previous post, I can still put :
    If cmd = "A" Then
    ..........................
    ...........................

    Then what do I declare cmd as? As a byte? Or as a string?

    Kindly advise
  • Mike GreenMike Green Posts: 23,101
    edited 2009-04-11 16:06
    You CANNOT declare cmd as a string. There is no way to do that. You can declare cmd as a byte or a word since both of those are large enough to hold the value of 65. Declaring cmd as a word is wasteful since you don't need to hold a value larger than a byte, but it would work otherwise.
  • Vincenzo1309Vincenzo1309 Posts: 76
    edited 2009-04-12 01:54
    Thanks Mike!! It works!!

    So can I conclude that when VB6 send the "A" over to my BS2, it is actually sending the 8 bit value of 65. Upon receiving it, the Basic Stamp Editor translate the 65 into the letter "A".

    Am I right for the above?

    Kindly advise!!

    Thanks alot!!
  • Mike GreenMike Green Posts: 23,101
    edited 2009-04-12 02:06
    The Basic Stamp editor does not translate a 65 to the letter "A". It does do the opposite (represents "A" as the value 65).

    VB6 does send the 8 bit value 65 to the BS2.

    Everything in the BS2 is just byte values. Even larger numbers that are held in 16 bit words are represented as pairs of bytes. They're sent as bytes during the download process and they're stored in the EEPROM as bytes.
  • Vincenzo1309Vincenzo1309 Posts: 76
    edited 2009-04-12 03:43
    So you are trying to say, when I input this code:

    If cmd = "A" Then
    ..........................
    ...........................

    BS2 receive 65 from the VB6 (8 bit value of 65 is stored in the variable cmd)..... what happens after that?

    since my code has no: If cmd = 65 Then
    ........................
    ........................

    So I guess is BS editor will represent this 8 bit value of 65 as a A, therefore my program can work?

    Kindly advise.....
  • Mike GreenMike Green Posts: 23,101
    edited 2009-04-12 04:18
    When you say

    IF cmd = "A" THEN

    the Stamp Editor effectively translates this into

    IF cmd = 65 THEN

    which is why your program works when VB6 sends a byte with the value of 65.

    You could write

    IF "A" = 65 THEN

    and this would get compiled into

    IF 65 = 65 THEN

    which would always be true.

    As I said before, 65 is the same as $41 which is the same as %01000001 which is the same as "A".
    These are all numbers with the same value.
  • Vincenzo1309Vincenzo1309 Posts: 76
    edited 2009-04-12 05:18
    Thanks for clearing my doubts, Mike.....
    Have a nice day!! [noparse]:)[/noparse]
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2009-04-14 00:14
    Quite an interesting topic and something I was recently thinking about,also some careful and great answers to the OP

    The Stamp and the PC send data back and forth a byte at a time , how these bytes are interpreted depends both on what format they are sent and in what format they are received.

    For instance the default method of writing a byte to the serial port in Visual Basic is to write it as an ASCII character ( or characters (an ASCII string) ) , actually it's not the ASCII character but the ASCII character code value that is sent.·

    So as you observed when you transmit an "A" VB sends the ASCII character code 65.·

    SERIN rx,baud,[noparse][[/noparse]x] ' x now contains the value 65.

    Also by default the Pbasic IDE DEBUG instruction prints ASCII characters to the screen even though your program sees it as a number, so now

    DEBUG x 'prints the letter "A"

    will print the ASCII character represented by the character code in x which is the letter "A"

    When dealing with numbers the DEC formatter ( also HEX , BIN etc. ) will format several ASCII bytes into one numeric byte value , for example taking the scenario above where x contains the character code for "A" in other words the value 65

    DEBUG DEC x 'prints the value 65

    will print to the screen the ASCII character code for "A" which is 65.

    So you can see that the ASCII characters are for our convenience and sometimes simpler to use, the microprocessor all it sees are numbers.

    Taking things a step further it is possible to change from the default format in which VB transmits , you can change from sending numbers as ASCII codes to transmitting numbers as a one byte value (this gets a little more complex when the values are greater than 255 but still doable).

    Still using the value of 65 if we were to transmit the value 65 using the ASCII format whether it be from the Stamp or the PC it would take two ASCII character codes ( 2 bytes ) "6" and "5", if there were ten such numbers it would take 20 ASCII formatted bytes with each pair requiring some kind of separation and formatting at the receiving end.

    Using single bytes to contain these values has the benefits of reducing the number of bytes transmitted therefore possibly triple the transmission speed with larger numbers and usually less code in handling the numbers. Using ASCII initially tends to be more straightforward and a little easier to understand

    Just wanted to highlight what took me time to see , when passing information around especially across some kind of external communications platform it's important to know exactly how the information is formatted at both ends and what benefits/drawbacks each has. If your ever unsure read the help files until you are sure.

    Jeff T.
  • Vincenzo1309Vincenzo1309 Posts: 76
    edited 2009-04-14 12:04
    Hi Jeff

    Thanks for sharing. Really appreciate that!

    Regards
    Vincent
  • Vincenzo1309Vincenzo1309 Posts: 76
    edited 2009-04-14 13:01
    Hi,

    There is one doubt that I need to clear. So does that mean that when the VB send a "A" over, the binary form of 65(%01000001) is sent over to the PC? Or is it in hex form?

    Kindly advise

    Thanks and Regards,
    Vincent Chung
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2009-04-14 13:50
    Hi Vincenzo, that is absolutely right ,·the PC and the Stamp ·store and communicate information as ones and zeros (binary) and it makes more sense to us to write it down as 65 as opposed to %01000001. DEC and HEX are formatters that are ways of writing those same ones and zeros in shorthand form·, again different formats have different benefits and this is something to take into consideration when designing an application , even working with·binary notation has benefits at times·. There is more to formatting than is immediately apparent.

    Jeff T.
  • Vincenzo1309Vincenzo1309 Posts: 76
    edited 2009-04-15 12:24
    Thanks for the enlightenment, Jeff.
    Have a nice day!
  • Vincenzo1309Vincenzo1309 Posts: 76
    edited 2009-04-19 11:52
    Hi everyone,

    Sorry, but I still have some doubts. We were discussing about how a "A" is sent from the VB6 to the BS2. I fully understood the theory, the ASCII code for A is 65, therefore a binary form of 65 is in fact sent to the BS2. Computers all deal with numbers.

    But what if I want to send "ACK" to BS2? In ASCII code, it is 65 67 75. So actually what get transmitted to the BS2?
    Is it the binary form of 65 67 75 which is %01000001 %01000011 %01001011?

    Kindly advise?

    Thanks and Regards,
    Vincent Chung
  • Mike GreenMike Green Posts: 23,101
    edited 2009-04-19 14:26
    What actually gets sent back and forth between the VB6 and the BS2 is 8 bit asynchronous serial data. Read this Wikipedia article: en.wikipedia.org/wiki/Asynchronous_serial_communication.

    The data is sent least significant bit first, so the "A" is sent with a start bit (0), then 1, 0, 0, 0, 0, 0, 1, 0, then a stop bit (1). Next is another start bit (0), then 1, 1, 0, 0, 0, 0, 1, 1, then a stop bit (1) for the "C". The "K" is then sent the same way.

    The SERIN statement processes the start and stop bits and assembles the 8 data bits into the proper order and stores the resulting numeric value into the variable you specify. If you're using one of the SERIN formatters, the 8 bit numeric values get passed to the formatter to process and they're treated as a sequence of character values. For example, "0", "0", "1", "5" would be recognized by the DEC formatter and translated into the single numeric value 15.
  • Martin HebelMartin Hebel Posts: 1,239
    edited 2009-04-19 17:11
    If you are sending an ASCII ACK, this is actually code 06. The ASCII codes cover numbers, letters, control characters and formatters (such as carriage return - 13) with a unique code associate with each character or code.

    -Martin
  • Vincenzo1309Vincenzo1309 Posts: 76
    edited 2009-04-20 11:49
    Hi Mike,

    Thanks alot for your reply
    Have a nice day!

    Regards,
    Vincent
  • Vincenzo1309Vincenzo1309 Posts: 76
    edited 2009-04-20 11:51
    Hi Martin,

    How do you get ACK = 06??
    kindly advise

    Regards,
    Vincent
  • Vincenzo1309Vincenzo1309 Posts: 76
    edited 2009-04-20 12:31
    Hi Mike,

    Thanks for the link to the Wikipedia article.

    I got some doubts. Is the start bit always 0? Is the stop bit always 1?

    "The data is sent least significant bit first, so the "A" is sent with a start bit (0), then 1, 0, 0, 0, 0, 0, 1, 0, then a stop bit (1). Next is another start bit (0), then 1, 1, 0, 0, 0, 0, 1, 1, then a stop bit (1) for the "C". The "K" is then sent the same way."

    I understand the part of A, but is there a error for C... u wrote (0) 11000011 (1)
    Should it be (0) 11000010 (1)? is it a typo error?


    Kindly advise

    Thanks and Regards,
    Vincent
  • Martin HebelMartin Hebel Posts: 1,239
    edited 2009-04-20 12:58
    Vincent,
    Is you look at any ASCII table that lists what each code means, you'll see ACK is represented by decimal 6.

    http://www.asciitable.com/

    This use of ACK assumes both sides understands through their programming what 6 represents and uses it accordingly.

    -Martin
  • Mike GreenMike Green Posts: 23,101
    edited 2009-04-20 13:11
    You are correct, that was a typo.

    By definition (and historical precedent) the resting state of an asynchronous serial line is a logic 1 (called a "mark"). The leading edge of the start bit (1 to 0 transition) defines the start of a bit cell. The start bit is always a logic 0 (called a "space"). The stop bit is just a guaranteed bit time at the resting state (which is logic 1) to allow the receiver to reset and recover from the process of receiving a character.

    I believe the terms "mark" and "space" come from telegraphy where a "ticker" (a solenoid with a pin attached) would mark a moving strip of paper when activated and leave a space when not activated. When the teletype was invented, the mark state was the resting state because you could detect when the line was broken. This is still used in that, if you leave an asynchronous serial line at logic 0 for at least a character time, it's detected as a "break" ... the absence of a stop bit when expected.
Sign In or Register to comment.