How to Append onto Variables?
eagletalontim
Posts: 1,399
I am needing to figure out how to "append" one variable on another variable then send this information out via Serial to a VB program. I have the communication between the computer and the prop working perfectly, but can't figure out how to append variables successfully.
I am using a function that I put together based on this post : http://forums.parallax.com/showthread.php?124308-Newb-question-Append-to-a-variable&highlight=append+variable
Here is a stripped version of my code.
So lets say Time_Count_Hours = 22 and pulseCount = 1234.... All that I need to have happen is where it has :
myArray[countpointer] := AppendVar(Time_Count_Hours, pulseCount)
Should turn out to be something like this :
myArray[countpointer] := 22:1234
So when I send it to the computer through the serial communication using this code, the VB program can sort it all out properly. :
I am using a function that I put together based on this post : http://forums.parallax.com/showthread.php?124308-Newb-question-Append-to-a-variable&highlight=append+variable
Here is a stripped version of my code.
VAR BYTE Time_Count_Hours BYTE countpointer LONG pulseData[255] LONG pulseCount long var1 long var2 long var3 byte varall[255] PUB Main ......... Other code myArray[countpointer] := AppendVar(Time_Count_Hours, pulseCount) PUB AppendVar(variable1, variable2) var1 := variable1 var2 := string(":") var3 := variable2 bytemove(@varall, var1, strsize(var1) + 1) bytemove(@varall + strsize(@varall), var2, strsize(var2) + 1) bytemove(@varall + strsize(@varall), var3, strsize(var3) + 1) return (@varall)
So lets say Time_Count_Hours = 22 and pulseCount = 1234.... All that I need to have happen is where it has :
myArray[countpointer] := AppendVar(Time_Count_Hours, pulseCount)
Should turn out to be something like this :
myArray[countpointer] := 22:1234
So when I send it to the computer through the serial communication using this code, the VB program can sort it all out properly. :
if command == 3 ' Send pulseData Array Ser.tx("D") i := 0 repeat i from 0 to countpointer Ser.str(pulseData[i]) ' not sure if this is the correct way or if I need to add an @ to pulseData Ser.tx(",") ' comma delimited to separate array values in VB program. i++
Comments
This above assumes you setup port #0 to communicate with the PC.
The above will output two zero padded digits with the value "Time_Count_Hours", followed by a ":" and then four digits (again zero padded) of the variable "pulseCount".
Your PC likely wants ASCII characters. The code you posted doesn't convert the variables to ASCII. Most serial objects have a "dec" method that does this converstion for you. In your case, I think you want the numbers zero padded so I chose a serial object that has a method to do this ("decx").
You could adapt "decx" to use with a different serial object. I'm sure I have a "zero pad" method somewhere in case you don't want to use the four port object (though it's a very good object to learn to use).
Using the above methods, you could load your numbers to a buffer "varall" this way.
Edit: I posted this before seeing your last post. Let me know if you need more clarification.
Edit again: I just realized there's a better way to use the above code. I'll rewrite the example in a couple of minutes.
There are of course other ways to do this. You could alternatively save your data in arrays of longs and then output them with serial "dec" or "decx" methods when the computer requests the data.
Edit: I just noticed I don't have any type of error recovery if the buffer overflows. You'd probably want it to display an error message and wait for the PC to request the data it does have.
What does your PC expect? The method I posted zero pads the data so the characters used will always be the same. If your program doesn't need zero padded data you could use the method below.
The "decl" method above will also output non-padded decimal characters. Just change all the "Decx" calls to "Decl" with the "flag" parameter set to zero and the "digits" parameter set to 10 (or the largest number of digits expected). The "globalPtr" should be adjusted automatically for you.
The methods "decx" and "decl" are methods I modified to send their outputs to a buffer instead of a serial terminal. I didn't write most of it and I still don't understand how it all works (particularly the flags part).
BTW, you may notice the "decx" method just sets the "flag" parameter (of "decl") to two, to produce zero padded output. If the "flag" parameter of "decl" is set to one, the output will be space padded. (Somewhere, I have a version that takes a character as a parameter and uses that character to pad the output.)
Edit: I think there are objects in the Propeller Tool library that will do a lot of what I just posted. I think the object "Numbers" (or something like that) have these type of methods. I have a bad habit of duplicating methods that have been done previously (and better) by someone else.
And here is my VB program code :
When you are outputting the code to the PC you can convert the Time_Count_Hours to ascii digits, insert the :, and convert the pulse_Count to ascii digits.