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