News:

  • July 01, 2026, 12:17:54 AM

Login with username, password and session length

Author Topic: Stripping numbers from ASCII Stream  (Read 14709 times)

roger3208

  • Newbie
  • *
  • Posts: 4
Stripping numbers from ASCII Stream
« on: April 01, 2015, 12:15:26 PM »
I am using a Do-More H2-DM1E PLC and this is the ASCII string that I am getting from a particle counter.
Is there and easy way to get out just the numbers I need to store in DataWorx, or use just about every ASCII block in program?
And if the string length is changing will make it even more fun.
This is the ASCII data string:
Sample of the string I am receiving;
12-MAR-2012 14:04:57, 0.3, 136960, 0.5, 25440, 0.7, 9580, 1.0, 6220, 2.0, 2620,
5.0, 960, 24, 41, 1, 60,0.3,0.5,0
The 6 numbers I need to get from the above string are:
0.3  0.5  0.7  1.0  2.0  5.0    (none of the time and date in front of the 0.3 and none of the info after the 5.0) (and remove 136960  25440  9580  6220  2620)
This group of 6 numbers will change with each sample.
Thank’s
Roger

plcnut

  • Hero Member
  • *****
  • Posts: 814
    • premiersi.com
Re: Stripping numbers from ASCII Stream
« Reply #1 on: April 01, 2015, 12:47:43 PM »
Use STRFIND to search for the comma. Then use that index for a STR2REAL (I believe that is instruction name) to get your data. Now continue with the same instructions through the remainder of the sting. I can post a sample later if you need it.
Circumstances don't determine who we are, they only reveal it.

~Jason Wolthuis
Premier Systems Integration, LLC
http://premiersi.com

roger3208

  • Newbie
  • *
  • Posts: 4
Re: Stripping numbers from ASCII Stream
« Reply #2 on: April 01, 2015, 03:56:34 PM »
Using the STRFIND and looking for the comma it finds the 19 comma's in the string, yes I would like to see a sample instructions how you are using the STR2REAL to step through to pick off the numbers. Thank you, Roger

plcnut

  • Hero Member
  • *****
  • Posts: 814
    • premiersi.com
Re: Stripping numbers from ASCII Stream
« Reply #3 on: April 01, 2015, 06:13:02 PM »
I did the first 2, you can figure out the rest :)
Circumstances don't determine who we are, they only reveal it.

~Jason Wolthuis
Premier Systems Integration, LLC
http://premiersi.com

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3833
    • Host Engineering
Re: Stripping numbers from ASCII Stream
« Reply #4 on: April 01, 2015, 06:17:38 PM »
I implemented two task code-blocks, TOKENIZE and PARSETOKENS.

Tokenize code-block takes the line of CSV text (comma separated variables) and sticks them into a block of STRINGs called Tokens (it supports up to 100 tokens in a single line), and sets D101 (NumTokens) equal to the number of tokens it found.

ParseTokens then takes the Tokens data-block and looks at them as REALs, and sticks the corresponding REAL values in a REAL data-block called ParsedData

So you end up with
Tokens0 ... ParsedData0
Tokens1 ... ParsedData1
Tokens2 ... ParsedData2
...
up to ID (NumTokens-1); so if NumTokens was 20, the last entry in both data-blocks would be
Tokens19 ... ParsedData19
and Tokens20..Tokens99 would be empty strings and the REALs in ParsedData20..ParsedData99 would be 0.0

I've attached 2 screen shots and the project export file (EDIT: SEE THE UPDATED PROJECT EXPORT TEXT FILE IN MY MORE RECENT POST) that you can import into Designer (File->Import->Project).

The 1st screen shot shows the input text (your example) in SL0, along with the resulting NumTokens (20 tokens are in that line)

The 2nd screen shot shows the two result data-blocks, the Tokens and the ParsedData.  Sure, the "date" token makes no sense, but you would not look at ParsedData0 as a REAL cuz you know it was a date.  But you should know that you want to look at (0-based index) Tokens1, Tokens3, Tokens5, Tokens7, Tokens9, and Tokens11.

But if you wanted to look at ANY of the other REAL values, you could (you just gotta know the right 0-based token index you are getting from whatever is sending you the data)

This project runs in the Simulator, so anybody can try it.
1. Import the project then download it to the Simulator. SEE MY MORE RECENT POST FOR AN UPDATED .TXT FILE
2. Bring up 1 Data View with Tokens0..Tokens20 (or however many you want), another one with ParsedData0..ParsedData20, another Data View with D101, C102, and SL0
3. Set SL0 to whatever text you want.
4. Make sure X0 is OFF, then put the Sim into RUN mode.
5. Transition X0 from OFF to ON then back to OFF.
6. The tokens should show up in Tokens data-block and the corresponding values should show up in ParsedData data-block
« Last Edit: April 02, 2015, 09:58:39 AM by franji1 »

plcnut

  • Hero Member
  • *****
  • Posts: 814
    • premiersi.com
Re: Stripping numbers from ASCII Stream
« Reply #5 on: April 02, 2015, 08:40:08 AM »
Franji1, I cannot import your program. I get 2 errors:
GeneralTokenize.txt(70) Unknown Data Block
GeneralTokenize.txt(207) Unknown Data Block

I had the same problem with my own program when I tried exporting and then importing it back.
Am I doing something wrong?
Circumstances don't determine who we are, they only reveal it.

~Jason Wolthuis
Premier Systems Integration, LLC
http://premiersi.com

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3833
    • Host Engineering
Re: Stripping numbers from ASCII Stream
« Reply #6 on: April 02, 2015, 09:09:48 AM »
Franji1, I cannot import your program. I get 2 errors:
Doh!  :(  See the attached updated file.

I did not include the System Configuration, and the Data Block #'s are necessary for DATAINFO instruction.  Doh!

I just ended up making sure the block #'s were correct.  I guess I could have just included the large SYSCONFIG section, but I like a more stream-lined text file.

Regardless, you did nothing wrong, and I have attached a "better" version with no errors, and the mnemonics now also include the Nicknames, so it is more human-readable.

plcnut

  • Hero Member
  • *****
  • Posts: 814
    • premiersi.com
Re: Stripping numbers from ASCII Stream
« Reply #7 on: April 02, 2015, 09:28:29 AM »
Nice work!
I always learn from your examples :)
Thanks!
Circumstances don't determine who we are, they only reveal it.

~Jason Wolthuis
Premier Systems Integration, LLC
http://premiersi.com

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3833
    • Host Engineering
Re: Stripping numbers from ASCII Stream
« Reply #8 on: April 02, 2015, 10:11:35 AM »
Nice work!
Thank YOU!

I like to solve the problem generically (Tokenize), then customize it (ParseTokens).  Even Tokenize could be made more generic by letting the delimiter be a STRING element (e.g. SS4) instead of a string literal "," comma.  Then, you could re-use Tokenize multiple ways for different delimiters (e.g. spaces, tabs) by stuffing that specific delimiter into a specific predefined SS element (e.g. SS4), then tweak the STRFIND in Tokenize to use SS4 element "delimiter" instead of the hard-coded "," comma.

You could write a ParseTokens that knows that Token0 is always a date-time stamp, Token1 is an integer that is a data-type value, and based off Token1's value, look at Token2 this way or that way (e.g. data-type-value of 0 means Token2 is a percentage, data-type-value 1 means its a URL, data-type-value of 2 means it's a temperature, etc.)  But it started with a generic "Tokenize" code-block, and then utilized a smarter "ParseTokens" code-block.

If you try to do that all in "one pass", it gets complicated pretty quickly, is a little buggy under certain input strings, and it's not re-usable.

roger3208

  • Newbie
  • *
  • Posts: 4
Re: Stripping numbers from ASCII Stream
« Reply #9 on: April 03, 2015, 09:14:10 PM »
plcnut
Thank you, for that program layout that work and a lot cleaner than what had layout.
roger3208  :)