IF satement help
grasshopper
Posts: 438
'' Propeller "Hello, world input and output" CON _clkmode = xtal1 + pll16x ' use external crystal * 16 _xinfreq = 5_000_000 ' 5 MHz obj SER : "FullDuplexSerial" PUB COMPUB ser.start(31, 30, 0, 9600) if [color=#990000] ser.rx(string("[noparse][[/noparse]X1]")) then[/color] ser.str(string("Hello World ")) ser.tx(13) 'line feed ser.tx(10) 'carriage return else [color=#990000] goto COMPUB[/color]
Basically i want to pause the code until the serial string [noparse][[/noparse]X1] is recieved then say HELLO WORLD
Comments
This always goes back to waiting for the "[noparse][[/noparse]" if any of the characters are incorrect. There are other, fancier ways of doing this that can do a smarter job of matching strings, but this would do what you asked.
if ser.rx == byte[noparse][[/noparse] @DATAONE ]
Only some programming languages have the ability to work with strings of characters as basic values. Spin is not one of these. There is some minimal support in the compiler for string and floating point constants and some simple string operations (equality testing, length, and copying) implemented as efficient function/procedure calls, but that's it.
So you say: "---"it" would put the pointer at this (@DATAONE) address..."
A better way to say this would be: " ... look at the memory cells pointed to by the pointer..."
You say:"... and if is true "[noparse][[/noparse]X1]" then complete the if statement."
A better way to say this would be: ".. and if those memory cells are equal to the bytes read in by the serial routine...."
Is this what you think?
It "yes", I shall tell you in the next posting why it is not so with SPIN...
I really want to have a "serial data table" that I can look up when data is sent to the propeller via a computer. Once i find a specific match in the table i can execute code for that table.
Any help is appreciated even if I get embarrassed [noparse]:)[/noparse]
(a) You have called a routine here, "rx". First find out as precise as possible what this routine delivers as a result! Is it a "string"? A pointer to a string? A number? What is the eaning of that number?
This can be difficult sometimes! The best way is to look into the object where that routine is defined! There are generally coments that tell you, but sometimes you have to look through its very code....
(b) You use an "operator", "==". What does the manual tell you what it does, again: What it "precisely" does...
Can you now put those things together? If not - after having done your "homework" (a) and (b) - call here again
1. "name" and "t" are variables (of what data type i do not know) in a pub called matchit (i understand naming a pub)
2. name alwys = 0
3. repeat until "[noparse][[/noparse]" is captured from serial port. (i understand this)
4. load into memory "name" until "]" is received or name is larger than 8? (kind of foggy here)
5. result = 0 (i think it returns a 0 to the pub)
6. repeat ehile t := table ( looking up the table for matches?)
7. the rest i am lost on.
if i find a match what then??
i still need to send through the serial port "hello world for X1 and say bye world for X2 etc...??
Thanks in advance.
--- I thought it to be a string but maybe a number not sure
--- == is boolean: is equal
I still need to do my homework... i will call many many more times [noparse]:)[/noparse]
Read the documentation on PRI / PUB. You'll see that local variables are all longs.
2. name alwys = 0
No. Look at the repeat loop
3. repeat until "[noparse][[/noparse]" is captured from serial port. (i understand this)
4. load into memory "name" until "]" is received or name is larger than 8? (kind of foggy here)
Look up the "<<" operator. Work through an example on paper.
5. result = 0 (i think it returns a 0 to the pub)
Look up the unary "++" operator. Again, work through an example on paper.
6. repeat ehile t := table ( looking up the table for matches?)
You can put an assignment statement in parentheses and the value assigned is used. Here it's the result of ser.rx.
7. the rest i am lost on.
Again, you'll learn a lot working through an example on paper. I usually make columns for each variable and
step through each statement (or part of a statement) on each row, writing down what changes.
if i find a match what then??
This subroutine returns the number of the table entry where there was a match (0 to n) or -1 if there was no match.
In your main program, you'd have something like:
·
But the name needs to be global variable i think. Alos why do i have to input the commands twice before it will send it back through the port?
PassIt won't work the way you've done it (you're reinitializing the serial port rather than setting up a new one).
1) You need two separate serial ports, one for input and one for your output. You can declare multiple instances
of the serial object. Keep SER as one name and maybe use OUTPUT as the other. The OUTPUT.start call needs to
be in your START routine so that it's only called once during initialization.
2) Sure, make "name" a global variable (in a VAR section ... declare it as a long). To transmit it, use the following:
Post Edited (Mike Green) : 1/15/2008 7:31:19 PM GMT
Take a look at the attached *.spin code, probably not the best example, but works for what I need it to do. Demonstrates receiving a 'canned' string and then acting upon what was received.
Please remember I code for a 'relaxation hobby' like some that play Xbox or whatever as a relaxation!
Always learning!
Hope this helps,
Mike
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Mike