CAA File Handling: "Read only" error

otbeka
2025-03-19
2025-03-20
  • otbeka - 2025-03-19

    Hi,

    On CODESYS V3.5.19.50 I am using the latest 3S File Access library (CAA File & CAA Types) to save and retrieve configuration data from the User Data section of an SD card, via file called config.txt.

    The application reads the file to memory once upon initialisation, and then the only other operation is overwriting the file with the updated configuration whenever the user changes the settings.

    An issue is ocurring, seemingly randomly, where the config.txt file, and all surrounding directories, cannot be written to by the unit, returning a READ_ONLY_CAA error upon file open. This sometimes does not clear even after power-cycling the PLC several times, yet checking the file permissions with ls -l shows normal read, write, and execute access.

    I am wondering if this is caused by the PLC itself or by an issue in my application. I have included my CAA File handler function block (SD_Card_Bin(FB).txt) and any associated types/calls below:

    TYPE ConfigData :
    STRUCT
        Config : ARRAY[0..256] OF STRING(64);
        NumberOfStrings : UDINT;
    END_STRUCT
    END_TYPE
    
    TYPE Buffer :
        STRUCT
            Data       : ARRAY[0..SIZEOF(ConfigData)] OF BYTE;
            DataLength : UDINT;
        END_STRUCT
    END_TYPE
    

    A case within my MAIN PRG:

    mode.WriteConfigDataToFile:
        VisuElems.CURRENTVISU := 'Screen_Write';
        IF xModeHasChanged THEN
            iErrorCount := 0;
            iRoute := 0;
        END_IF
    
        CASE iRoute OF
        0:
            inBuffer := ConfigData_To_Buffer(GVL.UnitData);
            xConfigError := FALSE;
            xDataConfigured := FALSE;
            iRoute := 1;
        1 : //Read Data from Card
            SD_Card_Bin.strFileName :=FileName;
            SD_Card_Bin.iFileOperation := 2;
            SD_Card_Bin(xExecute :=TRUE, WriteBuffer := inBuffer, xDone =>xDataConfigured, xError=>xConfigError, strErrorCodes=>strError);
            IF xDataConfigured THEN
                SD_Card_Bin(xExecute :=FALSE);
                IF xConfigError THEN
                    iRoute := 32767;
                ELSE
                    iRoute :=100;
                END_IF
            END_IF
        100: // The end
            bCurMode := mode.DisplayProcessVariables;
        32767: // Config error
            iErrorCount := iErrorCount + 1;
            IF iErrorCount > 2 THEN // Repeats 3 times, if no progress abandons and returns error
                bCurMode := mode.WriteError;
            ELSE
                iRoute := 0; // Try again
            END_IF
        END_CASE
    

    I'd like to rule out my application as the cause of this bug - help via comments or criticism would be greatly appreciated.

     
  • atone - 2025-03-20

    In case of error while opening the file to read you assume that the file doesn't exist and try to create one. But it could be that the SD card is not ready yet or simply not there. I would try to check first if the SD card is there and accessible. Otherwise with FILE.Create you could end up creating a new directory that is not mapped to the SD card. This would explain the errors even after a reboot, though it doesn't explain the READ_ONLY_CAA error I guess? I would use FILE.DirOpen and FILE.DirList to check if the SD card is there and ready.

     

Log in to post a comment.