News:

  • October 13, 2025, 01:37:40 PM

Login with username, password and session length

Author Topic: FileRead  (Read 5928 times)

RBPLC

  • Hero Member
  • *****
  • Posts: 585
FileRead
« on: November 18, 2019, 10:44:51 PM »
Let's say I had a .txt file that I wanted to import using the FILEREAD command. The text file was structured like: 1,0,1,1\r and I wanted to import 1,0,1,1 into locations C1, C2, C3 AND C4. Is there any way to do this directly or would I have to import the file as ASCII text and convert this string using a string command to get the bits into the C locations?

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3761
    • Host Engineering
Re: FileRead
« Reply #1 on: November 19, 2019, 09:06:38 AM »
See the attached export project file.  It runs stand alone in the Simulator.

It takes a string with CSV data and parses it for real numbers and sticks the values into a block of real values.  It has 2 parts, the first program code block Tokenize tokenizes the values - separates the CSV text data into an array of individual strings.  The ParseToken program code block goes through that array of string tokens and calls STRTOREAL go generate the REAL values and sticks those in a REAL data block.

You would still need to do the FILEREAD to get the line of data out of the file, but then launch those 2 code blocks.

RBPLC

  • Hero Member
  • *****
  • Posts: 585
Re: FileRead
« Reply #2 on: November 19, 2019, 01:48:14 PM »
Thanks franji. I'll try this out and see what I can do with it. Do you see this as being something that could be natively (could be directly accomplished) supported at some point? I was looking at this as a potential to "batch load" a profile to control some lighting relays that would automatically be adjusted at pre-defined intervals. This approach would be similar to loading a recipe from an HMI at predefined intervals. Is there some other built-in function that I'm missing that could be used to accomplish the same thing?   

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3761
    • Host Engineering
Re: FileRead
« Reply #3 on: November 19, 2019, 01:58:45 PM »
It's already there via JSON.  CSV is OK, but you must know the schema of each column/token.  Is it a bit?  Integer?  Real?  Text?  To come up with some kind of generic CSV parser would be very difficult.

I recommend you read up on JSON.  JSON handles it much better than CSV, and Do-more already supports it!

JSON supports arrays and/or fields and you can nest them.  For flat CSV, you can easily implement it as an array of 0/1 bits:

Code: [Select]
[0, 1, 1, 0]

or even as Boolean (Do-more supports BOTH)
Code: [Select]
[false, true, true, false]

And even better, if it's a profile/recipe, you can store it as "field"/colon/value list:

Code: [Select]
{
"CookTemp" : 42,
"CookTime": 60,
"UseIngredientA": true,
"UseIncredientB": false
}

RBPLC

  • Hero Member
  • *****
  • Posts: 585
Re: FileRead
« Reply #4 on: November 19, 2019, 03:22:31 PM »
Thanks for the info, I'll look into JSON more. I'm assuming you would then utilize the JSONPARSE command?

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3761
    • Host Engineering
Re: FileRead
« Reply #5 on: November 19, 2019, 03:33:02 PM »
Thanks for the info, I'll look into JSON more. I'm assuming you would then utilize the JSONPARSE command?

Exactly.  We added ability to 2.6 in  JSONPARSE to parse the value straight to a bit, integer, real, or string element.

2.5 only parses it to a string, then you have to do the STR2INT instruction on the parsed string value).  2.6 eliminates that 2nd step.

Not sure how you are generating your text file, but if you could somehow put brackets around it - anywhere, it can work.

This can work:

Code: [Select]
[
1, 1, 0
]
white space means nothing (except inside the field name double quotes)

RBPLC

  • Hero Member
  • *****
  • Posts: 585
Re: FileRead
« Reply #6 on: November 23, 2019, 08:28:44 PM »
Franji,
I was reading up on JSON and think I understand the basics. I was trying to use the JSONPARSE in conjunction with a for loop to extract bits and assign them to C registers. Here's what I've got (see image attached for reference):

I'm attemting to parse JSON string SS1. For this example this is a 10 length array of boolean values. I'm trying to run through string SS1 using a for loop with index stored in V10 going from 0 to 9. I've selected "0-based Array/Field Index" and set this value to V10. I would like to store the bits in registers C100-C109. The problem that I'm having is that I can't specify C[100+V10] to correspond with the loop index. What am I missing in trying to write the bits into C100-C109 using the syntax C[100+V10]?

Bolt

  • Hero Member
  • *****
  • Posts: 591
Re: FileRead
« Reply #7 on: November 23, 2019, 08:56:03 PM »
What am I missing in trying to write the bits into C100-C109 using the syntax C[100+V10]?
That type of indirect reference is limited to C[V10+63] if I'm not mistaken. Otherwise it maybe limited to 31.  I wish it was more flexible than that, but such is life.

If necessary, sometimes I do a MATH instruction in the loop to do V11 = V10+100, then you can reference C[V11].

Hope that helps!

RBPLC

  • Hero Member
  • *****
  • Posts: 585
Re: FileRead
« Reply #8 on: November 23, 2019, 09:20:14 PM »
Thanks Bolt. It didn't occur to me to use a MATH function to get the index I wanted.

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3761
    • Host Engineering
Re: FileRead
« Reply #9 on: November 23, 2019, 09:30:31 PM »
MATH Result array index can be full MATH expressions themselves.

So, stick the result of the JSONPARSE into a temp C bit, say C42, then do a MATH with C[V10 + 100] in the Result field, and just "C42" in the expression.

MATH C[V10 + 100] "C42"

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6126
  • Yes Pinky, Do-more will control the world!
Re: FileRead
« Reply #10 on: November 24, 2019, 12:24:15 AM »
What am I missing in trying to write the bits into C100-C109 using the syntax C[100+V10]?
That type of indirect reference is limited to C[V10+63] if I'm not mistaken. Otherwise it maybe limited to 31.  I wish it was more flexible than that, but such is life.

If necessary, sometimes I do a MATH instruction in the loop to do V11 = V10+100, then you can reference C[V11].

Hope that helps!

We?re already cramming 10 pounds into a 5 pound bag. Six bits was all that was available for the displacement. We are currently spec-ing the Son of Do-more engine that will be able to do simple expressions for virtually every input parameter, in addition to very deep UDTs and multidimensional arrays. Limits like this will be gone forever.
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

RBPLC

  • Hero Member
  • *****
  • Posts: 585
Re: FileRead
« Reply #11 on: November 24, 2019, 12:24:24 PM »
So I've got the JSON parsing working. Now I've got another issue. I've got to first get my JSON structured text into the controller. I'm doing this using the FILEREAD command to read in a .txt file. The problem is my JSON structured text will wind up being more than 1024 characters long for the full implementation. Would there be a straightforward way to iteratively parse sections of my JSON formatted text file to import sections of it at a time? I realize I could break my text file up into multiple files that don't exceed 1024 characters but I would like to keep it one file if possible. 

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3761
    • Host Engineering
Re: FileRead
« Reply #12 on: November 24, 2019, 03:36:45 PM »
Instead of using a STRING, us a BYTE Data Block.

Create a new BYTE block called MyData that's 10,000 in length (or whatever max length you think you need).

Use that data block in your FILEREAD and in your JSONPARSE instruction.

RBPLC

  • Hero Member
  • *****
  • Posts: 585
Re: FileRead
« Reply #13 on: November 24, 2019, 04:55:59 PM »
Franji,
See attached photo. I created the MyData unsigned byte block that is MyData0-9999. I still seem to be running into the same issue with import length. I set the "Buffer Size in Bytes" to the maximum of 1024. I can't seem to import anymore than 1024 characters out of my JSON text file.

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6126
  • Yes Pinky, Do-more will control the world!
Re: FileRead
« Reply #14 on: November 24, 2019, 04:58:46 PM »
FILEREAD can be called more than once to fill the buffer.
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO