Get a decimal value and set the value into a variable?
kevinspace
Posts: 56
Hello everyone~
I tried to get a decimal value and set the value into a variable.
This is my code:
Above the code, which always waitting for the input ! I have to input a value, the repeat loop will execute once only!
I hope the repeat loop which can always executed ! And until when I input the value, the variable will change to a new value!!
Are there any suggesstion? or any example?
I tried to get a decimal value and set the value into a variable.
This is my code:
VAR long Stack[100] long x OBJ Debug: "FullDuplexSerialPlus" PUB Main Debug.start(31, 30, 0, 9600) Debug.tx(16) repeat x := Debug.GetDec Debug.Dec(x) Debug.str(string(13))But the result didn't correspond with my idea!
Above the code, which always waitting for the input ! I have to input a value, the repeat loop will execute once only!
I hope the repeat loop which can always executed ! And until when I input the value, the variable will change to a new value!!
Are there any suggesstion? or any example?
Comments
variables in SPIN are
bytes 8 bit integer
words 16 bit integer
long 32 bit integer
if you take a look into the method GetDec
it calls getstr
So what you have to send is a bytesequence terminating with a carriage return (the "13")
It's not important to understand all the cryptic details in the square-brackets important is
repeat until ..... == 13
which means repeat until value 13 (a carriage return) is received.
I had no problems to receive and echo back the sended values on my PPDB from PST.EXE
The decimal point is elimated by the StrToDec method.
This makes sense as SPIN-variables are only integer and not floating-point-values
How does the sender send his decimal values?
repeating it all the time at high speed or only once you click a button? (FDX+ has a receivebuffer of 16 bytes which is filled up quickly)
What is the range of the numbers you are sending?
what do you get back from the "Debug.Dec"?
can you provide answers to these questions to make it easier to analyse the problem?
best regards
Stefan
The range of the sending numbers were approximately from 0 to 6000.
By the way, This makes sense as SPIN-variables are only integer and not floating-point-values ?
How to solve the problem?
And I got back from the "Debug.Dec"?, because I have to check the value which I have been sent.
Eventually, how to edit the progrom to receive string or dec datas and display the value at Parallax Serial Terminal simultaneously?
And I hope the value will change when I input a new value, furthermore,
if I didn't input any value, the value will display the last one value always.
.
I'm sorry I did not understand what you are trying to say with your last posting.
Except that you want to receive a string and a decimal value and both should be echoed back to PST.EXE.
If I use exactly your posted code above and let my propeller communicate with PST.EXE
I start your program: the PST.EXE receive-window stays empty as expected
I type in f.e. the number "123" and then press enter
PST.EXE echoes back 123
I enter "-789" + Enterkey
PST.EXE echoes back -789
I enter "123.567" + Enterkey
PST.EXE echoes back 123567 WITOUT the decimal point
after each enter PST.EXE waits for the next number to be entered and echoes nothing
just as expected.
Now YOU have to be much more specific what you are typing in and what the propeller echoes back
Without this information it is impossible for me to analyse and help any more
best regards
Stefan
It looks like you want to be able to enter floating point values and later display them. Have a look at the FloatString version 1.2 object in the Propeller Object Exchange. You will also need one of the floating point objects. I suggest Floating Point version 1.6 or F32 version 1.3.
This is another code which I have corrected, in this code, I have to use another cog to receive data always.
Therefore, the Parallax Serial Terminal which can display the last value always.
The question of me is that how to achieve the same result without another cog ?
[code]
VAR long Stack[150]
long x
OBJ
Debug: "FullDuplexSerialPlus"
PUB Main
coginit(3, Get, @stack[0])
Debug.start(31, 30, 0, 9600)
Debug.tx(16)
repeat
Debug.Bin(x,8) 'convenient to observe
Debug.str(string(13))
PUB Get
repeat
x := Debug.GetDec
[/code
FullDuplexSerialPlus (FDS+) as its name says is a fullduplex serial driver it can send and receive data at the same time.
FDS+ has a send and a receive FIFO-buffer of 16 bytes. This means while you are sending for example 10 bytes FDS+ is able to receive up to 16 bytes
at really the same time and will store them in the receivebuffer. If you call debug.rx or debug.getstr or any other receive-method the bytes are taken out of the receivebuffer.
Another issue is if you have to send more than 16 bytes within a short time the receivebuffer-size must be increased.
STOP trying around with having not really understand how FDS+ works.
To give you the most effective advice please answer the following questions:
Hhow fast are you sending your values? 100 times per second, every 5 seconds?
How fast will your code process the received values?
1000 times per second? every minute?
Do you send integer-values or some kind of floating-point or fix-point values?
If your PC-software is something special - please attach the PC-software you were using for sending the values towards the propeller-chip
Please post a logfile of what you send and what you receive back from the propeller
Of course you are free to tinker around what ever you want but then help won't be effective.
From my 25-year experience of coding you can believe me: if you hurry up to make things quick running the truth is you are slowing things down.
Now the time elapsed is two days and 7 postings but nothing substantial has changed.
best regards
Stefan
I think that I have to describe my project.
I used 12 servos to control my Robot, and I hope that I can change the Robot's walking speed anytime,
so I have to input the speed variable by FullDuplexSerialPlus.
In fact, I don't care the received data speed, I just want to observe the variable value.
In the example code, I can input the variable anytime and change the x variable, but the way which have to
use another cog, are you any math or any suggesstion to help me that how to write the code more easier?
In this way, I have to input a decimal value into the x variable. Another question, assume in other project,
how to input a floating-point values ?
[code]
VAR long Stack[150]
long x
OBJ
Debug: "FullDuplexSerialPlus"
PUB Main
coginit(3, Get, @stack[0])
Debug.start(31, 30, 0, 9600)
Debug.tx(16)
repeat
Debug.Bin(x,8) 'convenient to observe
Debug.str(string(13))
PUB Get
repeat
x := Debug.GetDec
[/code
I'm really sorry I don't understand what you are asking. Please take time to write an easy to understand question.
After writing please re-read what you have written if it really makes sense and is easy to understand THEN submit your posting.
I could answer on a question like this but it is not effective. Why?
A short answer might not hit what you want to know.
A long answer has to be an almost complete programming tutorial 5 to 30 pages big to make sure that the detais you want to know are included.
Sorry that's too much effort.
To get this together
It is the same thing as with your last posting. It is written very fast and "dirty" and exactly this fast stile slooooooooooows things down.
I have to ask again.
The only thing I can say from your code is:
NEVER use coginit. ALWAYS use cognew. if you use cognew the propeller-chip manages automatically which cog does what.
If you use FDX+ which starts a cog too and this cog is cog 3 and then you do a coginit(3,...) you are forcing the propeller-chip
to stop the FDX+-driver and start cog 3 with method get.
About floating-point values: My answer "floating-point object"
Everything clear??? I guess not. Why is not everything clear?? Because a lot of details are left out. Do you understand?
It is the same qith your question a lot of details left out.
To be honest most of the time I'm a patient guy. But this half-sentences cut and paste code writing-stile makes me upset.
You want to have help and all the forum members here are willing to help but you have to provide detailed information
I will only answer again if:
your complete sourcecode is attached as a zip-archive that is created with the archive-function of the propellertool
A description of minimum 20 sentences describing in detail what your PC is doing and what the propellercode should do.
regards
Stefan