Need clarification on variable declaration
Vincenzo1309
Posts: 76
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!
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!
Comments
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.
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!!
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.
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.
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).
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.
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....
Thanks a lot!!
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
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!!
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.
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.....
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.
Have a nice day!! [noparse]:)[/noparse]
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.
Thanks for sharing. Really appreciate that!
Regards
Vincent
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
Jeff T.
Have a nice day!
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
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
Thanks alot for your reply
Have a nice day!
Regards,
Vincent
How do you get ACK = 06??
kindly advise
Regards,
Vincent
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
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
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.