eWON Flexy Open File Invalid Parameter

I am following the link for “HOW TO: Create a custom historical log file in BASIC” that is in the forums. My code is currently similar to that. In bold below is where I come across my problem. The error received is “invalid parameter (5)”. fileName$ = “eWonTest.csv” in my case.
SETSYS PRG, “RESUMENEXT”, 1
CLOSE 1 REM - Close the file just in case.
OPEN fileName$ FOR BINARY INPUT AS 1
ErrorReturned = GETSYS PRG, “LSTERR”
IF(ErrorReturned = 33) THEN
CLOSE 1
OPEN fileName$ FOR BINARY OUTPUT AS 1
PUT 1, “Time,TagName,Value”
CLOSE 1
ELSE
OPEN fileName$ FOR BINARY APPEND AS 1
PUT 1, Time$ + “,TankLevel,” + STR$ TankLevel@ + CHR$(13)+ CHR$(10)
PUT 1, Time$ + “,Valve Status,” + valveStatus$ + CHR$(13)+ CHR$(10)
CLOSE 1

@nickTriPhase1

Can you confirm what firmware version your eWON is using? Try replacing fileName$ with f$.

Firmware 11.0s0, should I upgrade to 12.0?
I tried f$, but that does not work

@nickTriPhase1

Can you please upload a copy of your program.bas file here? You should be able to directly drag and drop the file into the text editor. If you script contains more private information please feel free to send it to me via a private message.

@nickTriPhase1

Thank you for the backup. I see a few things in this script that need fixed, however additionally it was only one line that really “crashed” out your script.

Issue One:

First, you had quite a bit of scripting in the CYCLIC section. We **do not** want anything in the cyclic section (especially file write actions). The cyclic section executes with each processor cycle resulting in extremely fast execution. Having a file write execute in the cyclic section can cause a new execution to occur far before the main execution is done.

Issue One Resolution

Luckily issue one has a very easy resolution. Simply create a new script section called cycle and copy-paste what you had in your cyclic section there. This will allow it to actually occur on the 1 second timer that you have defined.

Issue Two:

The error you were getting: Invalid Parameter (5) xxxx was being caused by the lack of closing the file after the first execution. You were doing it correctly initially, creatintg the file and closing it, however if you look at this specific code block:

IF(ErrorReturned = 33) THEN
    CLOSE 1
    OPEN f$ FOR BINARY OUTPUT AS 1
    //LINES OMITTED FOR PRIVACY
    CLOSE 1
ELSE
    //<!!!! RIGHT HERE IS WHERE YOU ARE MISSING CLOSE 1 !!!!>
    OPEN f$ FOR BINARY APPEND AS 1
    //PUT 1, Time$ + ",TankLevel," + STR$ TankLevel@ + CHR$(13)+ CHR$(10)
    //PUT 1, Time$ + ",Valve Status," +  valveStatus$ + CHR$(13)+ CHR$(10)
    PUT 1, "testValue"// + CHR$(13) + CHR$(10)
    CLOSE 1
ENDIF

Right below your else block, you are missing a CLOSE 1 statement. So when we actually get to this part of the script, the file is still opened. We can’t open a file for appending that is already open.

Issue Two Resolution

Ensure you are closing the file before executing subsequent requests. I typically issue a CLOSE File# prior to executing any file IO operations to be certain the file is closed. Note however that you will need to do this in your second else clause as well. I slightly altered your script for privacy but you can see the result of making these minimal changes below.

When in doubt, close it out.

Results

Moving Cycle




Good Run:
Note: The first IOError is expected as the file had not yet been created in the USR directory.

Please let me know if this helps.

1 Like

This works great! Thanks for your help.

It looks like the code that mine is based off of must need the CLOSE 1 statement as well:

@nickTriPhase1

Great catch! I’ll update that asap.