Shop OBEX P1 Docs P2 Docs Learn Events
Sending integer variables as a serial string? — Parallax Forums

Sending integer variables as a serial string?

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.

Comments

  • You're missing a right paren here:

    Io.str(string(“,”)

    Are you sure it compiled?

    -Phil
  • I just typed the line wrong on here! It does compile and runs, it just doesn’t work as I need it to.
    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

  • Spin does not support using "+" as a string concatenation operator, so
    io.str(string("$,") + x + string(".") + ...)
    
    is definitely not going to work.

    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)?
  • ersmith wrote: »
    Spin does not support using "+" as a string concatenation operator, so
    io.str(string("$,") + x + string(".") + ...)
    
    is definitely not going to work.

    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.
  • Here is the basic code I'm working with tonight. Based on what works (commented code at the bottom) so long as I transmit a long string everything works great. The CR at the end signifies that the string is complete. It looks like mixing the .str and .dec commands is terminating the read process with the transmitted data due to either a terminated string or CR. Based on that I was trying to if I could use the variables to create a long string from the integer variables with little success so far.
    I did some more searches for mixing strings and variables together but get the same results. Any ideas on how to get this going?
    '' =================================================================================================
    ''   File....... Master Controller v7a.spin
    ''   Purpose.... Create routines for leg control
    ''   Author..... Bob Sweeney
    ''   Started.... 1/15/19
    ''--------------------------------------------------------------------------------------------------
    ''   1/15/19 Setup serial communications  
    '' =================================================================================================
    
    con
    
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000
    
      CLK_FREQ = ((_clkmode - xtal1) >> 6) * _xinfreq
      MS_001   = CLK_FREQ / 1_000
      
    con
    
      #1, HOME, #8, BKSP, TAB, CLREOL, CLRDN, #16, CLS      ' io formmatting control
      
    con
      RX1           = 31
      TX1           = 30
      SDA           = 29
      SCL           = 28
    
    obj
      io     : "pcFullDuplexSerial4FC" '1 COG for 4 serial ports
      numb    : "simple_numbers" 
    
    var
      long comStack[100], outputstr
      long femurAngle, tibiaAngle, coxaAngle, LegNum
      byte buffer[20]
    
    pub Start | femur, tibia, coxa, leg
      io.Init
      '  AddPort(vm_port,vm_rx, vm_tx, vm_cts,        vm_rts,        UARTS#DEFAULTTHRESHOLD, UARTS#NOMODE,UARTS#BAUD9600)
      'port 0 for comms to serial terminal
      io.AddPort(0,      31,    30,    io#PINNOTUSED, io#PINNOTUSED, io#DEFAULTTHRESHOLD, io#NOMODE, io#BAUD115200) 'Serial terminal output
      'port 1 for comms to master from legs
      io.AddPort(1,      1,     0,     io#PINNOTUSED, io#PINNOTUSED, io#DEFAULTTHRESHOLD, io#NOMODE, io#BAUD115200)                                                      'IO port to master
      'port 2 - output to leg only
      io.AddPort(2,      10,     11,     io#PINNOTUSED, io#PINNOTUSED, io#DEFAULTTHRESHOLD, io#NOMODE, io#BAUD115200)                                                      'IO port to master
      io.Start
    
      pauseMSec(500)                                                                     
    
      io.tx(0,16)
    
      'various experiments to try different variations
      leg := 3
      LegNum := numb.dec(leg)
      io.str(0,legnum)
      io.str(0,string(13))
      femur := 900
      femurAngle := numb.Dec(femur)
      io.str(0,femurAngle)
      io.str(0,string(13))
      tibia := 20
      tibiaAngle := numb.Dec(tibia)
      io.str(0,tibiaAngle)
      io.str(0,string(13))
      coxa := 900
      coxaAngle := numb.Dec(coxa)
      io.str(0,coxaAngle)
      io.str(0,string(13))
    
     'following traditional output code does not work - only identifies the value for leg, ignores rest 
          io.str(2,string("$,"))
          io.dec(2,leg)
          io.str(2,string(","))
          io.dec(2,femur)
          io.str(2,string(","))
          io.dec(2,tibia)
          io.str(2,string(","))
          io.dec(2,coxa)
          io.str(2,string(13)) 
    
    ' following hard code works
    {    io.str(2,string("$,1,900,0,900",13))
        io.str(2,string("$,2,900,0,900",13))
        io.str(2,string("$,3,900,0,900",13))
        io.str(2,string("$,4,900,0,900",13))
        io.str(2,string("$,5,900,0,900",13))
        io.str(2,string("$,6,900,0,900",13))}
    
  • @DiverBob

    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):
    var
      byte outbuf[128]
      
    pub strappend(buf, s) | n1, n2
        n1 := strsize(buf)
        n2 := strsize(s) + 1 ' include trailing 0
        buf += n1
        bytemove(buf, s, n2)
    
    pub buildstring
      ' code to output
      outbuf[0] := 0 ' start with empty string
      strappend(@outbuf, string("$,"))
      strappend(@outbuf, numb.dec(leg))
      strappend(@outbuf, string(","))
      strappend(@outbuf, numb.dec(femur))
      strappend(@outbuf, string(","))
      strappend(@outbuf, numb.dec(tibia))
      ... ' and so on
    
    pub main
      buildstring
      io.str(2, @outbuf)
    
  • ersmith wrote: »
    @DiverBob
    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):
    Your quick solution worked the first time I tried it! Thanks for the assistance. I had tried something along these lines earlier but my code failed and I didn’t pursue it further. After a nights rest and a fresh brain, this was the path I wanted to explore today so you have saved me a lot of time, so I can get on with some new coding!
Sign In or Register to comment.