FB_FileClose只是第一次忙

时间:2018-07-20 07:57:23

标签: plc st twincat

我正在使用TwinCAT 3和ST来保存套接字连接中的数据。套接字也可以工作,并且可以节省一部分,但不是全部。我尝试保存的第一个数组工作正常。但是,如果我要保存另一个,它将失败。 FB_FileClose不会忙。

IF reset THEN // I reset the FB after it saved one array.
    bSuccess := FALSE;
    iError := 0;
    step := 1;
    reset := FALSE;
    MEMSET(ADR(saveArray), 0, SIZEOF(saveArray));
    RETURN;
END_IF
CASE step OF
1:
    IF path = '' THEN
        bSuccess := FALSE;
        iError := 12;
        step := 1;
    END_IF
    fbFileOpen.sPathName := path;
    fbFileOpen.nMode := FOPEN_MODEAPPEND OR FOPEN_MODEPLUS;
    fbFileOpen.bExecute := TRUE;
    fbFileOpen.tTimeout := T#2S;
    fbFileOpen();
    step := 2;

2:
    fbFileOpen(bExecute := FALSE);
    IF NOT fbFileOpen.bBusy AND NOT fbFileOpen.bError THEN 
        step := 3;
    ELSE 
        iError := fbFileOpen.nErrId;
    END_IF

3:
    fbWriteFile.hFile := fbFileOpen.hFile;
    fbWriteFile.bExecute := TRUE;
    fbWriteFile.pWriteBuff := ADR(saveArray);
    fbWriteFile.cbWriteLen := SIZEOF(saveArray);
    fbWriteFile.tTimeout := T#2S;
    fbWriteFile();
    step := 4;

4:
    fbWriteFile(bExecute := FALSE);
    IF NOT fbWriteFile.bBusy AND NOT fbWriteFile.bError THEN
        step := 5;
    END_IF

5:
    fbCloseFile.hFile := fbFileOpen.hFile;
    fbCloseFile.bExecute := TRUE;
    fbCloseFile.tTimeout := T#3S;
    fbCloseFile();
    IF fbCloseFile.bBusy THEN //Gets suck here at the second run. And if I remove it, the FB doesn't get busy and doesn't close my hFile.
        step := 6;
    END_IF


6:
    fbCloseFile(bExecute := FALSE);
    IF NOT fbCloseFile.bBusy AND NOT fbCloseFile.bError THEN
        bSuccess := TRUE;
    ELSE
        iError := fbCloseFile.nErrId;
    END_IF
END_CASE

我还注意到 FB_FileOpen 连续两次打开相同的 hFile 。它的第二个不能从FB_FileClose关闭。下次运行时,它将获得一个新的hFile,然后可以保存数据。下一个它不能,等等。我这是什么错误?

谢谢!

1 个答案:

答案 0 :(得分:0)

经过一番破解之后,我自己找到了解决方案。在为FB_FileOpenFB_FileWriteFB_FileClose设置所有参数之前,我将execute设置为false,如下所示:

1:
    IF path = '' THEN
        bSuccess := FALSE;
        iError := 12;
        step := 1;
    END_IF
    fbFileOpen(bExecute := FALSE); // Set this to false before.
    fbFileOpen.sPathName := path;
    fbFileOpen.nMode := FOPEN_MODEAPPEND OR FOPEN_MODEPLUS;
    fbFileOpen.bExecute := TRUE;
    fbFileOpen.tTimeout := T#2S;
    fbFileOpen();
    step := 2;

现在可以使用了。