2

I have this program which creates file on USB stick. The problem is it is only saving one line (rewriting same line). I would need after each cycle it would write the data to new line . I think it has to do something with offset I need to calculate offset so it doesn't start from 0 every time. Here is the part of the code

10: DevLink_0.enable := TRUE;
    DevLink_0.pDevice := ADR('Disk');
    DevLink_0.pParam := ADR(cesta_k_USB);
    DevLink_0();

    IF DevLink_0.status =0 THEN
        step :=20;
    END_IF

20: FileCreate_0.enable := TRUE;
    FileCreate_0.pDevice := ADR('Disk');
    FileCreate_0.pFile := ADR('results.csv');
    FileCreate_0();

    IF FileCreate_0.status = 0 THEN
        identification_file := FileCreate_0.ident;
        offset :=0;
        step :=30;
    END_IF

    IF FileCreate_0.status = fiERR_EXIST THEN 
        step :=25;
    END_IF

25: FileOpen_0.enable := TRUE;
    FileOpen_0.pDevice :=  ADR('Disk');
    FileOpen_0.pFile := ADR('results.csv');
    FileOpen_0.mode := FILE_W;
    FileOpen_0();

    IF FileOpen_0.status = 0 THEN
        identification_file := FileOpen_0.ident;
        offset := FileOpen_0.filelen;
        step := 30;
    END_IF

30: data:=INT_TO_STRING(y);
    data:=INSERT(data,'$r$n',LEN(data));
    FileWrite_0.enable := TRUE;
    FileWrite_0.ident := identification_file;
    FileWrite_0.pSrc := ADR(data); 
    FileWrite_0.len := LEN(data); 
    FileWrite_0.offset := offset;
    FileWrite_0();
    

    IF FileWrite_0.status = 0 THEN
        
            step :=40;
       
        END_IF



40: FileClose_0.enable := TRUE;
    FileClose_0.ident := identification_file;
    FileClose_0();

    IF FileClose_0.status =0 THEN
        IF save = FALSE THEN
            step :=50;
        ELSE
            step := 25;
        END_IF
    END_IF

50: DevUnlink_0.enable := TRUE;
    DevUnlink_0.handle := DevLink_0.handle;
    DevUnlink_0();

    IF DevUnlink_0.status =0 THEN
        stav:= 0;
    END_IF
eglease
  • 2,445
  • 11
  • 18
  • 28
Lukas
  • 21
  • 1

1 Answers1

2

As already mentioned in a comment, you will need to set the offset in the FileWrite FUB correspondingly.

I do this usually with the FileInfo FUB Guid 6eaf42f0-4ce5-44b7-95cb-275ae1c2fac5 in the AS help. It will tell you if the file exists already and also which size it has.

If it exists, the next step would be FileOpen, otherwise FileCreate.

Recently I have created a small project on GitLab which is, among other things, also appending a line to a file: https://gitlab.com/kirni/bur_robotic_sample/blob/master/bur_robotic_sample/Logical/Libraries/TeachLib/Teach.c

The code is in C, but I am sure you get the idea.

case stTEACH_INFO:

            /*setup fub*/
            this->fbInfo.enable = 1;
            this->fbInfo.pDevice = (UDINT)inst->szDevice;
            this->fbInfo.pName = (UDINT)inst->szFile;
            this->fbInfo.pInfo = &this->Info;

            //call fub
            FileInfo(&this->fbInfo);

            //fub is done..
            if(this->fbInfo.status != ERR_FUB_BUSY)
            {
                //file exists -> open it and append code
                if(this->fbInfo.status == ERR_OK)
                {
                    //start writing to the end of the file
                    this->Offset = this->Info.size;

                    //open existing file
                    this->Step = stTEACH_OPEN;
                }
                //file does not exist -> create it and insert code
                else if(this->fbInfo.status == fiERR_FILE_NOT_FOUND)
                {
                    //start writing at the beginning of the file
                    this->Offset = this->Info.size;

                    //crete new file
                    this->Step = stTEACH_CREATE;
                }
                //error
                else
                {
                    //set error status and goto error state
                    inst->Status = this->fbInfo.status;
                    this->Step = stTEACH_ERROR;
                }

                //disable fub
                this->fbInfo.enable = 0;            
                FileInfo(&this->fbInfo);
            }

            break;

        case stTEACH_CREATE:

            /*setup fub*/
            this->fbCreate.enable = 1;
            this->fbCreate.pDevice = (UDINT)inst->szDevice;
            this->fbCreate.pFile = (UDINT)inst->szFile;

            //call fub
            FileCreate(&this->fbCreate);

            //fub is done..
            if(this->fbCreate.status != ERR_FUB_BUSY)
            {
                //success
                if(this->fbCreate.status == ERR_OK)
                {
                    this->Ident = this->fbCreate.ident;

                    //open existing file
                    this->Step = stTEACH_WRITE;
                }           
                //error
                else
                {
                    //set error status and goto error state
                    inst->Status = this->fbCreate.status;
                    this->Step = stTEACH_ERROR;
                }

                //disable fub
                this->fbCreate.enable = 0;          
                FileCreate(&this->fbCreate);
            }

            break;

        case stTEACH_OPEN:

            /*setup fub*/
            this->fbOpen.enable = 1;
            this->fbOpen.pDevice = (UDINT)inst->szDevice;
            this->fbOpen.pFile = (UDINT)inst->szFile;
            this->fbOpen.mode = fiREAD_WRITE;

            //call fub
            FileOpen(&this->fbOpen);

            //fub is done..
            if(this->fbOpen.status != ERR_FUB_BUSY)
            {
                //success
                if(this->fbOpen.status == ERR_OK)
                {
                    this->Ident = this->fbOpen.ident;

                    //open existing file
                    this->Step = stTEACH_WRITE;
                }           
                //error
                else
                {
                    //set error status and goto error state
                    inst->Status = this->fbOpen.status;
                    this->Step = stTEACH_ERROR;
                }

                //disable fub
                this->fbOpen.enable = 0;            
                FileOpen(&this->fbOpen);
            }
            break;

        case stTEACH_WRITE:

            /*setup fub*/
            this->fbWrite.enable = 1;
            this->fbWrite.ident = this->Ident;
            this->fbWrite.offset = this->Offset;
            this->fbWrite.pSrc = this->szLine;
            this->fbWrite.len = this->Lenght;

            //call fub
            FileWrite(&this->fbWrite);

            //fub is done..
            if(this->fbWrite.status != ERR_FUB_BUSY)
            {
                //success
                if(this->fbWrite.status == ERR_OK)
                {
                    this->Ident = this->fbWrite.ident;

                    //Write existing file
                    this->Step = stTEACH_CLOSE;
                }           
                    //error
                else
                {
                    //set error status and goto error state
                    inst->Status = this->fbWrite.status;
                    this->Step = stTEACH_ERROR;
                }

                //disable fub
                this->fbWrite.enable = 0;           
                FileWrite(&this->fbWrite);
            }
            break;

However this should also work with the filelen output of the FileOpen FUB - just the way you did it. I would suggest, that you set a brakepoint just before writing and check if the offset is set on the FUB properly.

I would also recommend, that you call each FUB after it was done, again with enable:=0 (like I did in my sample) i.e. disabling it. Some FUBs only update their inputs parameters on a positive edge on the enable, execute, start, etc. input command.

I hope this helps!

kirni
  • 121
  • 6