[Snippet of code in first response which throws the error]
Hi,
I'm creating a program for a machine that should test our products. The testing sequence is complete but I am unable to log the data.
I'm able to create a folder and a file to write to. But I'm unable to write to the file.
The filwr FB gives me an error on eError of value 1. At the moment I'm at a loss, any help would be appreciated.
The code below ends up at ManagerState 90 with POU_ERROR = TRUE and ErrorAtState = 42 with State_eError = 1.
1 is not present in the Error ENUM of the CAA_File library hence the reason I'm posting this.
VARPOU_ERROR:BOOL;ManagerState:INT:=0;ErrorAtState:INT:=0;State_eError:FILE.ERROR;hFile:FILE.CAA.HANDLE;filop:FILE.Open;filcl:FILE.Close;filwr:FILE.Write;hDir:FILE.CAA.HANDLE;fildop:FILE.DirOpen;fildcl:FILE.DirClose;fildcr:FILE.DirCreate;LOG30:TBLOG;LOG76:TBLOG;LWD:ARRAY[0..MaxID-1]OFTBTEST;sTarget:STRING;sFileName:STRING;TestID:UINT;xFileExists:BOOL;udiFileSize:UDINT;szFileSize1:FILE.CAA.SIZE:=0;udiCmpError:UDINT;j:UINT;sData:STRING(255);END_VARVARCONSTANTMaxID:UINT:=3;HeaderNames:ARRAY[0..8]OFSTRING:=['DateTime', 'TestState', 'TestPaused', 'TestCounterA', 'RunTimeStrokeA', 'RunTimeTotalA', 'TestCounterB', 'RunTimeStrokeB', 'RunTimeTotalB'];sTestString:STRING:='Hello world!';END_VAR//SavelogsectionLOG30.LTT(PT:=gvl_LogUpdateFreq);LOG76.LTT(PT:=gvl_LogUpdateFreq);CASE(ManagerState)OF0://InitializingLogManagerPOU_ERROR:=FALSE;fildop(xExecute:=FALSE);IFPLC_R.i_wSdCardStatus<>SEC.PLC_R_SDCARD_STATUS.SDCARD_READWRITETHEN////SDCARDerrorErrorAtState:=ManagerState;State_eError:=FILE.ERROR.NOT_EXIST;ManagerState:=90;ELSEManagerState:=10;END_IF10://SD-cardincorrectstatecheckiffolderexistsfildop.sDirName:=gvl_sLogDir;fildop(xExecute:=TRUE);IFfildop.xDoneTHENhDir:=fildop.hDir;IFNOTfildop.xErrorTHENManagerState:=11;END_IFEND_IFIFfildop.xErrorTHENManagerState:=15;END_IFIFfildop.xDoneTHENfildop(xExecute:=FALSE);END_IF11://Closedirectoryfildcl.hDir:=hDir;fildcl(xExecute:=TRUE);IFfildcl.xDone=TRUETHENfildcl(xExecute:=FALSE);ManagerState:=20;END_IFIFfildcl.xError=TRUETHENErrorAtState:=ManagerState;State_eError:=fildcl.eError;ManagerState:=90;END_IF15://Createdirectoryfildcr.sDirName:=gvl_sLogDir;fildcr(xExecute:=TRUE);IFfildcr.xDoneTHENfildcr.xExecute:=FALSE;ManagerState:=0;//ReturntocheckiffilehasbeenmadeEND_IFIFfildcr.xError=TRUETHENErrorAtState:=ManagerState;State_eError:=fildcr.eError;ManagerState:=90;END_IF20://Waitingforevent//ResettingallFB's filop (xExecute := FALSE); filcl (xExecute := FALSE); filwr (xExecute := FALSE); fildop(xExecute := FALSE); fildcr(xExecute := FALSE); //Setting the timer to be linked to wether the test is active or not IF TB30.State = AUTO THEN LOG30.LTT(IN := TRUE); ELSE LOG30.LTT(IN := FALSE); END_IF IF TB76.State = AUTO THEN LOG76.LTT(IN := TRUE); ELSE LOG76.LTT(IN := FALSE); END_IF //Checking triggers IF LOG30.State <> TB30.State OR LOG30.Paused <> TB30.Paused OR LOG30.LTT.Q THEN LWD := TEST_30; ManagerState := 30; LOG30.State := TB30.State; LOG30.Paused := TB30.Paused; IF LOG30.LTT.Q THEN LOG30.LTT( IN := FALSE); END_IF ELSIF LOG76.State <> TB76.State OR LOG76.Paused <> TB76.Paused OR LOG76.LTT.Q THEN LWD := TEST_76; ManagerState := 30; LOG76.State := TB76.State; LOG76.Paused := TB76.Paused; IF LOG76.LTT.Q THEN LOG76.LTT( IN := FALSE); END_IF END_IF 30: // Write start TestID := 0; ManagerState := 34; 34: // Check if enabled and generate name IF LWD[TestID].Enabled THEN //Generate file name IF LWD[TestID].Product = '' THEN LWD[TestID].Product := 'UNKNOWNPRODUCT'; END_IF IF LWD[TestID].ProductID = '' THEN LWD[TestID].ProductID := CONCAT('UNKNOWNPRODUCTID', UINT_TO_STRING(TestID)); END_IF sFileName := CONCAT(LWD[TestID].Product, '-'); sFileName := CONCAT(sFileName, LWD[TestID].ProductID); sFileName := CONCAT(sFileName, '.csv'); sTarget := CONCAT(gvl_sLogDir, sFileName); ManagerState := 36; ELSE ManagerState := 70; END_IF 36: // Check if file exists udiFileSize := FILE.SysFileGetSize(sTarget, ADR(udiCmpError)); IF udiFileSize = 0 THEN //File doesnt exist or is empty //Header should be created ManagerState := 40; ELSIF udiCmpError = ERR_OK THEN //File exists //File should be directly opened in Append mode ManagerState := 45; ELSE State_eError := FILE.ERROR.ERROR_UNKNOWN; ErrorAtState := ManagerState; ManagerState := 90; END_IF 40: // File doenst exist - Create file filop.sFileName := sTarget; filop.eFileMode := FILE.MODE.MWRITE; filop.xExclusive:= TRUE; filop (xExecute := TRUE ); IF filop.xDone THEN hFile := filop.hFile; ManagerState := 42; END_IF IF filop.xError THEN State_eError := filop.eError; ErrorAtState := ManagerState; ManagerState := 90; END_IF 42: // New file created - Write header //Creation of table header string sData := ''; FOR j := 0 TO SIZEOF(HeaderNames) DO sData := CONCAT(sData, HeaderNames[j]); sData := CONCAT(sData, ';');END_FORfilwr.hFile:=hFile;filwr.pBuffer:=ADR(sTestString);szFileSize1:=SIZEOF(sTestString);filwr.szSize:=szFileSize1;filwr.udiTimeOut:=100000;//100msTimeOutfilwr(xExecute:=TRUE);IFfilwr.xDoneTHENManagerState:=42;filwr(xExecute:=FALSE);END_IFIFfilwr.xErrorTHENState_eError:=filwr.eError;ErrorAtState:=ManagerState;ManagerState:=90;END_IF45://FileexistsopeninMAPPDfilop.sFileName:=sTarget;filop.eFileMode:=FILE.MODE.MAPPD;filop(xExecute:=TRUE);IFfilop.xDoneTHENhFile:=filop.hFile;ManagerState:=60;//-------------------------- TEMPEND_IFIFfilop.xErrorTHENState_eError:=filop.eError;ErrorAtState:=ManagerState;ManagerState:=90;END_IF60://Writingdone,closefilefilcl.hFile:=hFile;filcl(xExecute:=TRUE);IFfilcl.xDoneTHENManagerState:=70;END_IFIFfilcl.xErrorTHENState_eError:=filcl.eError;ErrorAtState:=ManagerState;ManagerState:=90;END_IF70://NextitemstateorendcycleTestID:=TestID+1;IFTestID<MaxIDTHENManagerState:=34;ELSEManagerState:=20;END_IF90://ErrorstatePOU_ERROR:=TRUE;END_CASE
I'm running this on a M241 Schneider Electric PLC in SoMachine V4.3. I need to revert the CAA library to the v3.5.3.0 instead of v3.5.3.132 because otherwise the PLC throws 2 unresolved references: FILE_CLOSE and FILE_DIRCLOSE.
Last edit: lmdejong 2020-05-25
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
[Snippet of code in first response which throws the error]
Hi,
I'm creating a program for a machine that should test our products. The testing sequence is complete but I am unable to log the data.
I'm able to create a folder and a file to write to. But I'm unable to write to the file.
The filwr FB gives me an error on eError of value 1. At the moment I'm at a loss, any help would be appreciated.
The code below ends up at ManagerState 90 with POU_ERROR = TRUE and ErrorAtState = 42 with State_eError = 1.
1 is not present in the Error ENUM of the CAA_File library hence the reason I'm posting this.
I'm running this on a M241 Schneider Electric PLC in SoMachine V4.3. I need to revert the CAA library to the v3.5.3.0 instead of v3.5.3.132 because otherwise the PLC throws 2 unresolved references: FILE_CLOSE and FILE_DIRCLOSE.
Last edit: lmdejong 2020-05-25
more posts ...
The actual problem could be in here:
As far as I know this is exactly as in the example from the CAA_File documentation.
SOLVED: Been working on this too long, didn't see a typo. The errorcode remains unfound though!
Would you mind correcting this typo so we can c/p your solution?
That would be awesome!
The error is in step 42, writing step. I circle it back to step 42 and try to write again without resetting the write function.
There is the error.
Below is the final code.
I had to add some extra steps to reset the write function because when u start a file it writes two lines, one to create the table header.
Thanks for posting this! Would you mind if I submit this into the code snippet corner? As this seems a good snippet for it
Sure, no problem