News:

  • October 13, 2025, 11:33:40 AM

Login with username, password and session length

Author Topic: FileRead  (Read 5919 times)

RBPLC

  • Hero Member
  • *****
  • Posts: 585
Re: FileRead
« Reply #15 on: November 24, 2019, 06:14:10 PM »
I guess the best way to do this is using the FILESEEK command to pick up reading the file where it left off on the last FILEREAD?

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6126
  • Yes Pinky, Do-more will control the world!
Re: FileRead
« Reply #16 on: November 24, 2019, 06:22:52 PM »
I guess the best way to do this is using the FILESEEK command to pick up reading the file where it left off on the last FILEREAD?

No need. Take a look at the file pointer in the file structure. It shows you where you are currently reading from.
"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 #17 on: November 27, 2019, 09:30:22 AM »
I see that the FILEREAD command can be called multiple times in sequence. The attached shows the FILEREAD command being used multiple times reading up to 1000 Bytes per use of FILEREAD. This is in a stage and I've gotten it to work. What I would like to do is loop over the FILREAD command multiple times without having individual FILEREAD commands. This would allow a file length of  any size (within memory limitations of the BRX) to be read in without knowing it's size before hand. I've tried running this in a for loop but I have been unsuccessful. Can this command be looped over or is there another way to accomplish this?   

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6126
  • Yes Pinky, Do-more will control the world!
Re: FileRead
« Reply #18 on: November 27, 2019, 09:53:04 AM »
Build the loop in stages. I do it all the time for bulk file or comm transfers.
"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 #19 on: November 27, 2019, 11:19:38 AM »
Bob,
Could you give me an example of how you normally implement this. Everything I'm trying is overrunning the memory that I've allocated to read the data into. I'd greatly appreciate it.

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6126
  • Yes Pinky, Do-more will control the world!
Re: FileRead
« Reply #20 on: November 27, 2019, 12:53:47 PM »
The attached project worked for me.
"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 #21 on: December 02, 2019, 02:55:21 PM »
Thanks Bob. I made a few tweaks to what you provided and it seems to work for my application without issue. Now, another question. Is there a built in command to determine the length of a JSON array? I didn't see one. What I was trying to do is use the JSONPARSE to loop through all elements of a JSON Array without knowing the array's length (using the 0 based array/field option). If there was a command to determine the JSON array length I could loop that many times through the array and extract the elements of the array and write them out to individual memory locations. It looks like I could also use the JSONPARSE command in a while loop and use the JSONPARSE result code to loop until the result code returned a 0 to indicate an element not found. Would this be the easiest approach to doing this, or am I missing an easier way to do this?

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3761
    • Host Engineering
Re: FileRead
« Reply #22 on: December 02, 2019, 03:25:13 PM »
Use a REPEAT/UNTIL or WHILE loop to iterate the JSONPARSE with an incrementing Array Index, utilizeing the Return Results in a D location (say D99).

In the Help topic for JSONPARSE, it talks about this Return Results D value.  The High word will be 0 when the JSON text is good, but your Array Index was not found, e.g. there are only 3 elements in the array, but you passed in a 0-based index of 3.  0, 1, and 2 are valid array indexes in the situation where the record has 3 elements, so when you try to read the 4th (at 0-based index 3), then that tells you you're done.

Code: [Select]
MOVE 0 V42  // JSON array index, start at 0
REPEAT
  JSONPARSE (Lookup by Array Index V42) (Return Result in D99)

  STRLE D99:SW1 <= 0  // D99:SW1 is a "cast" which means look at the HIWORD of D99 as a Signed Word, if it's 0 it's done, or < 0 it's in error
  OUT C99  // turn on C99 when DONE

  STRN C99  // not done?
  INC V42  // increment V42 to the NEXT index

UNTIL C99

What's good is that V42 will equal the number of array elements after the UNTIL.  So if there are 3 elements in the array, the JSONPARSE will use 0, 1, 2, successfully.  When V42 increments to 3 and goes back to the top of the REPEAT, the JSONPARSE will fail when the 0-based index is 3 (i.e. 4th time through the loop), it will set C99, and NOT increment V42 at the bottom of the loop (STRN is a NC contact), hence it will remain equal to 3, the UNTIL will exit the loop since C99 is now ON.  And V42 will equal 3 (one beyond the last 0-based index, which is, by definition, the number of items in the JSON array record!)