修复 TwinCAT 3 问题:FB_FileOpen 忙,状态机问题

问题描述 投票:0回答:1

我是 Beckhoff/TwinCAT 新手,正在学习教程。在制作一个简单的事件记录器时,我遇到了一个障碍:使用 FB_FileOpen 后,它的 bBusy 保持 True。这会使我的状态机卡在 FILE_OPEN 中。对我搞砸的事情有什么想法吗?这是我的代码:

VAR 
    fbFileOpen : FB_FileOpen;
    fbFilePuts : FB_FilePuts;
    RisingEdge : R_TRIG;
    eWriteState : (If_Trigger_Detection,OpenFile_Trigger, OpenFile, WaitForTheEvent,     WriteFileTriggner, WriteFile, CloseFile, Error); 
    stEventWriteToFile: ST_Event;
    
    CsvString : T_MaxString;
    
END_VAR

RisingEdge(CLK:=TRUE);

CASE eWriteState OF
    If_Trigger_Detection:
        IF RisingEdge.Q THEN 
            eWriteState:=OpenFile_Trigger;
        END_IF 
 
    OpenFile_Trigger:
        fbFileOpen.bExecute := FALSE;
        fbFileOpen(sPathName:= 'C:\TwinCAT\Eventslog.txt',nMode:=FOPEN_MODEAPPEND OR FOPEN_MODETEXT,bExecute := TRUE);
        eWriteState:= OpenFile;
        
    OpenFile:
        fbFileOpen.bExecute := FALSE;
        IF fbFileOpen.bError THEN
            eWriteState := Error;   
                 
        ELSIF NOT fbFileOpen.bBusy AND fbFileOpen.hFile <>0 THEN 
            eWriteState:= WaitForTheEvent;
            
        END_IF
        
        
    WaitForTheEvent:
        //do nothing just wait
    
    WriteFileTriggner:
    CsvString := CreateCsvString (stEvent:= stEventWriteToFile);
    //fbFilePuts(bExecute:=FALSE,
    //          sLine:=CsvString);
    fbFilePuts.bExecute:=FALSE;
    fbFilePuts(sLine := CsvString,bExecute := TRUE); 
    eWriteState := WriteFile;
    WriteFile:
    fbFilePuts(bExecute := FALSE);
    IF NOT fbFilePuts.bBusy THEN 
        eWriteState := WaitForTheEvent;
    END_IF

    //CloseFile:
    
    Error:
    //Here we will write the code for error Handling
END_CASE

enter image description here

state-machine plc twincat structured-text st
1个回答
0
投票

您也必须在

fbFileOpen()
状态下继续拨打
OpenFile
。现在,您只需将
bExecute
标志设置为
FALSE
,但实际上并未调用功能块的主体,这意味着它永远不会执行,因此保持忙碌状态。 只需更换:

fbFileOpen.bExecute := FALSE;

fbFileOpen(bExecute := FALSE);
© www.soinside.com 2019 - 2024. All rights reserved.