Sending integer variables as a serial string?
DiverBob
Posts: 1,108
in Propeller 1
Did some searches but didn’t find what I was looking for( this reminded me how much I don’t like the search function on the website!).
I have a string that consists of characters and integer variables to be send serially using the 4port serial object. The format of the output is: $,integer, integer, integer, integer followed by a CR. An example output would be $,1,900,20,900 followed by CR. I tried using this code:
Io.str(string(“$,”))
Io.dec(1)
Io.str(string(“,”)
Io.dec(900) ‘etc....
The output device recognizes the ‘$,1,” and then nothing after that.
However if I hard code the output as io.str(string(“$,1,900,20,900”,13)) it works as expected. However I need to be able to change the integer values on the fly. I thought I’d done this before so I checked my old code and several objects looking for options but didn’t find anything that would help. Looking for some other options that I have forgotten.
I have a string that consists of characters and integer variables to be send serially using the 4port serial object. The format of the output is: $,integer, integer, integer, integer followed by a CR. An example output would be $,1,900,20,900 followed by CR. I tried using this code:
Io.str(string(“$,”))
Io.dec(1)
Io.str(string(“,”)
Io.dec(900) ‘etc....
The output device recognizes the ‘$,1,” and then nothing after that.
However if I hard code the output as io.str(string(“$,1,900,20,900”,13)) it works as expected. However I need to be able to change the integer values on the fly. I thought I’d done this before so I checked my old code and several objects looking for options but didn’t find anything that would help. Looking for some other options that I have forgotten.
Comments
Io.str(string(“,”)
Are you sure it compiled?
-Phil
I saw an example I will try after dinner using Simple_Numbers object:
X := nums.Dec(1)
Io.str(string(“$,”) + x + string(“,”) + ....) ‘ this is just a part of the code to try out
Your original approach, using io.str() and io.dec(), should work. I suspect you probably just have a typo in it somewhere. Could you post all of your code, preferably between [ code ] and [ /code ] blocks (no spaces in those)?
I hadn’t tried that approach out yet, just thinking out loud! Thanks for the input, have to figure out another angle.
The problem with the first example is that although it does compile and run, the output works up to the first instance of io.dec(1). Unfortunately the next portion does not transfer correctly. I think it has something to do with the first io.dec command. As long a I transmit everything as a string it works right. As far as I know I need a means to convert the variable integers into a string.
I did some more searches for mixing strings and variables together but get the same results. Any ideas on how to get this going?
Your code looks OK. That makes me think that probably your device has a timing dependency: the .dec method is written in Spin, so it takes a while to create its string. My guess is that probably the device is timing out while its waiting for that.
Probably the simplest solution is to construct the whole string in a buffer first, and then transmit it (since you've already established that works). Something like the code below (I have *not* tested this, so it probably needs some tweaking, I'm just trying to give the general flavor):