Shop OBEX P1 Docs P2 Docs Learn Events
Parsing sub-strings from serial input of fixed length — Parallax Forums

Parsing sub-strings from serial input of fixed length

BenjBenj Posts: 66
edited 2013-12-05 13:07 in Propeller 1
I am fairly new to Propeller and I am starting on a new project. I will be taking serial input from an old piece of lab equipment and will need to parse out certain sub-strings and then append them to what will eventually be a URL GET request by a Wifly. The length of the string is fixed (24 characters), and the position of the pertinent pieces of data does not change from string to string. For instance, one of the pieces of data is the motor RPM. It always is in characters 7, 8, 9, and 10. I need to be able to parse those characters to send a serial out string to the Wifly, that would be something like this:

http://www.myserver.com/datainput.php?rpm=char7char8char9char10

Also, the lab equipment doesn't have any way for me to change the string it sends out, i.e. to add comma separation or the like.

Can someone point me in the right direction for the commands I would use?

Benj

Comments

  • T ChapT Chap Posts: 4,223
    edited 2013-12-05 12:40
    Benj, check this recent thread that sounds very similar to what you are wanting to do.


    http://forums.parallax.com/showthread.php/151780-Split-string-after-first-character?highlight=chap

    It is not really clear if how you are connecting to the lab equipment? It seems you need get some data from the equipment and then send a GET via the Wifly? When you say serial, have you already been successful getting the serial data into the Prop?
  • MagIO2MagIO2 Posts: 2,243
    edited 2013-12-05 12:45
    PUB
       ...
      bytemove( @url+42, @buffer_for_lab_device + 7, 4 )
       ...
    DAT
    buffer_for_lab_device  byte 0[100]
    url                               byte "http://www.myserver.com/datainput.php?bla=7890"
    

    Parsing means understanding, what you need is a simple copy. You did not tell anything about the size of the string that you receive by the lab device, so I used 100 for demonstration.
  • BenjBenj Posts: 66
    edited 2013-12-05 12:50
    OK, thanks both of you. I think between the two I am on the right path. I will post questions if I run into any walls.

    I guess I always thought of parsing as extracting out or replacing a certain portion of a string. Either way, I see what I need is a simple copy.
  • T ChapT Chap Posts: 4,223
    edited 2013-12-05 13:07
    Parsing can be whatever you want it to be. It sounds like to me that you want to read in ALL data from the lab device, then take SOME info out of it and do a GET to transfer the info to a server?

    So, first have a serial object read the device. I use the serial 4 port object so that you get 4 instances of a serial object. One instance for the lab device, one for the wifly uart, one for a serial terminal for debug or feedback if you need it. After you read the data in from the device, since you already know the positions(elements) you can then send the GET with whatever content you want, versus just a relay of the devices output.

    In the example below, I have already created a php webpage on a server that will take the GET and variables, and forward that to a mysql database and store the info. This is probably not your plan but you can see how I communicated with the wifly to the server successfully. I like using the server to deal direct with the database, much easier in my opinion.

    You will need to determine if the lab device is sending ascii or hex so that you can be sure of how and what you are sending with the GET. In the other thread linked, there were examples of converting strings to decimals.
    PUB Rec:LabData   | ii, val   'copy content of incoming data into array
         ii := 1
         response[0] := ser.rx(2)  'waits here for first byte response  from lab device
         Repeat 48                    'look for more data  set to what you need
            Val :=  ser.rxtime(2, 10)   ' wait only 2 ms, if no byte found  then keep repeating until then it goes back to the top and waits,  avoids hang up on incomplete data
            response[ii] := Val
            ii++
         'Now figure out what you want to do with the data in the array
         ser.tx(1, CR)      'send the data to the Wifly       adjust this to what the wifly wants to see
    
    
    PUB WriteDBase  'write the standard values to the DB
        ser.str(1, string("**Write DBase**"))      'debug your stages to terminal if needed on port 1
        ser.tx(1, CR)
        ex   'send an exit to wifly in case it is stuck in cmd mode from previous instruction
        ex
        ser.str(2, string("$$$"))   '  command mode on wifly
        Viewreply     'verify the reply from command mode on wifly
        ser.str(2, string("open yourserver.com 80"))     'your server name here
        ser.tx(2, CR) 
        Viewreply   'get any resplies from wifly to clear the buffer
        Viewreply   'get any resplies from wifly to clear the buffer
        ser.str(2, string("GET /yourpage.php?"))        'your page on a site that will parse the data and submit to mysqul db
        ser.str(2, string("TABLE=table1"))   'DB table name
        ser.str(2, string("&ID=1"))          'system ID
        ser.str(2, string("&SERVER=xxxxxx.db.xxxxxxx.godaddy.com"))    '' DB server
        ser.str(2, string("&DBU=xxxxxxxx"))      ''DB username
        ser.str(2, string("&DBP=xxxxxxxxx"))   ''DB password
        ''/// send all status parameters
        ser.str(2, string("&REMIP="))   '///TEST only >>>192.168.1.13"))    ''router IP (dynamic)
        ser.str(2, @IP)   'send the complete IP in one string from array
        ser.str(2, string("&TIMER="))    '///  test only>>1005"))    ''router IP (dynamic)
        ser.str(2, timer.showTimer)   '' insert the running timer value here
        ser.str(2, string("&C1="))
        ser.hex(2, C1S,1)
        ser.str(2, string("&C2="))
        ser.hex(2, C2S,1)
        ser.str(2, string("&C3="))
        ser.hex(2, C3S,1)
        ser.str(2, string("&C4="))
        ser.hex(2, C4S,1)
    
        ser.str(2, string("&CM1="))
        ser.hex(2, CM1S,1)
        ser.str(2, string("&CM2="))
        ser.hex(2, CM2S,1)
        ser.str(2, string("&CM3="))
        ser.hex(2, CM3S,1)
        ser.str(2, string("&CM4="))
        ser.hex(2, CM4S,1)
    
        ser.str(2, string("&DVS="))
        ser.hex(2, DVS,1)
    
        ser.str(2, string("&MIFI=")) 
        ser.hex(2, MIFIselect, 1)  
    
        ser.str(2, string(" HTTP/1.1"))   'http GET terminator
        ser.tx(2, CR)
        ser.tx(2, LF)
        ser.str(2, string("Host: yourservername.com"))
        ser.tx(2, LF)   ' two LF required
        ser.tx(2, LF)   '  
        Viewreply    'clear any reply fro wifly
        Viewreply 
        ser.str(1, string("**Write DB DONE**"))     'debug the finish
        ser.tx(1, CR)
        SetIdle2    'set wifly to idle
    
    PUB Ex
         'ser.str(2, string("$$$"))     
         ser.str(2, string("exit"))
         ser.tx(2, CR) 
         ViewReply
    
    PUB ViewReply  
       repeat 512   'get the response here from the load command
          buffer := ser.rxtime(2,1)
          if buffer < $FF
            ser.tx(1, buffer)
    
    PUB SetIdle2
        ser.str(2, string("$$$"))
        Viewreply
        ser.str(2, string("set com idle 2"))    
        ser.tx(2, CR)   
        Viewreply
        ser.str(2, string("exit"))    
        ser.tx(2, CR)   
        Viewreply
    
    
Sign In or Register to comment.