0

我正在使用 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,然后它可以保存数据。下一个它不能,依此类推。我在这个中的错误是什么?

谢谢!

4

1 回答 1

0

经过一番修改后,我自己找到了解决方案。在为 设置所有参数之前FB_FileOpen,我将执行设置为 false,如下所示FB_FileWriteFB_FileClose

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;

现在它正在工作。

于 2018-07-20T12:21:06.180 回答