Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bur/plctypes.h>
- #include <fileio.h>
- /***** Variable declaration *****/
- _LOCAL BOOL bOK;
- _LOCAL USINT byStep, byErrorLevel;
- _LOCAL USINT byReadData[300], byWriteData[100];
- _LOCAL UINT wStatus, wError;
- _LOCAL UDINT dwIdent;
- _LOCAL FileOpen_typ FOpen;
- _LOCAL FileClose_typ FClose;
- _LOCAL FileCreate_typ FCreate;
- _LOCAL FileRead_typ FRead;
- _LOCAL FileWrite_typ FWrite;
- _LOCAL FileDelete_typ FDelete;
- _LOCAL STRING device[25][30];
- /***** Init part *****/
- _INIT void Init(void)
- {
- int i;
- /* Initialize variables */
- bOK = 0;
- byStep = 1;
- byErrorLevel = 0;
- /* Initialize read and write data */
- for (i = 0; i < 300; i ++)
- {
- byReadData[i] = 0;
- }
- }
- /***** Cyclic part *****/
- _CYCLIC void Cyclic(void)
- {
- int s,i,j=0;
- for(i=0;i<=100;i++)
- {
- if(byReadData[i]==10){
- j=j+1;
- s=0;
- }
- else
- {
- device[j][s]=(STRING)byReadData[i];
- s++;
- }
- }
- switch (byStep)
- {
- case 0: /**** Error step ****/
- bOK = 0;
- break;
- case 1: /**** Try to open existing file ****/
- /* Initialize file open structrue */
- FOpen.enable = 1;
- FOpen.pDevice = (UDINT) "LOGS";
- FOpen.pFile = (UDINT) "config.txt";
- FOpen.mode = FILE_RW; /* Read and write access */
- /* Call FUB */
- FileOpen(&FOpen);
- /* Get FUB output information */
- dwIdent = FOpen.ident;
- wStatus = FOpen.status;
- /* Verify status (20708 -> File doesn't exist) */
- if (wStatus == 20708)
- {
- byStep = 3;
- }
- else if (wStatus == 0)
- {
- byStep = 4;
- }
- else if (wStatus != 65535)
- {
- byErrorLevel = 1;
- byStep = 0;
- if (wStatus == 20799)
- {
- wError = FileIoGetSysError();
- }
- }
- break;
- case 2: /**** Create file ****/
- /* Initialize file create structure */
- FCreate.enable = 1;
- FCreate.pDevice = (UDINT) "HARDDISK";
- FCreate.pFile = (UDINT) "TestFile";
- /* Call FUB */
- FileCreate(&FCreate);
- /* Get output information of FUB */
- dwIdent = FCreate.ident;
- wStatus = FCreate.status;
- /* Verify status */
- if (wStatus == 0)
- {
- byStep = 3;
- }
- else if (wStatus != 65535)
- {
- byErrorLevel = 2;
- byStep = 0;
- if (wStatus == 20799)
- {
- wError = FileIoGetSysError();
- }
- }
- break;
- case 3: /**** Write data to file ****/
- /* Initialize file write structure */
- FWrite.enable = 1;
- FWrite.ident = dwIdent;
- FWrite.offset = 0;
- FWrite.pSrc = (UDINT) &byWriteData[0];
- FWrite.len = sizeof (byWriteData);
- /* Call FUB */
- FileWrite(&FWrite);
- /* Get status */
- wStatus = FWrite.status;
- /* Verify status */
- if (wStatus == 0)
- {
- byStep = 4;
- }
- else if (wStatus != 65535)
- {
- byErrorLevel = 3;
- byStep = 0;
- if (wStatus == 20799)
- {
- wError = FileIoGetSysError();
- }
- }
- break;
- case 4: /**** Read data from file ****/
- /* Initialize file read structure */
- FRead.enable = 1;
- FRead.ident = dwIdent;
- FRead.offset = 0;
- FRead.pDest = (UDINT) &byReadData[0];
- FRead.len = sizeof (byReadData);
- /* Call FUB */
- FileRead(&FRead);
- /* Get status */
- wStatus = FRead.status;
- /* Verify status */
- if (wStatus == 0)
- {
- byStep = 5;
- }
- else if (wStatus != 65535)
- {
- byErrorLevel = 4;
- byStep = 0;
- if (wStatus == 20799)
- {
- wError = FileIoGetSysError();
- }
- }
- break;
- case 5: /**** Close file ****/
- /* Initialize file close structure */
- FClose.enable = 1;
- FClose.ident = dwIdent;
- /* Call FUB */
- FileClose(&FClose);
- /* Get status */
- wStatus = FClose.status;
- /* Verify status */
- if (wStatus == 0)
- {
- byStep = 0;
- }
- else if (wStatus != 65535)
- {
- byErrorLevel = 5;
- byStep = 0;
- if (wStatus == 20799)
- {
- wError = FileIoGetSysError();
- }
- }
- break;
- case 6: /**** Delete file ****/
- /* Initialize file delete structure */
- FDelete.enable = 1;
- FDelete.pDevice = (UDINT) "HARDDISK";
- FDelete.pName = (UDINT) "TestFile";
- /* Call FUB */
- FileDelete(&FDelete);
- /* Get status */
- wStatus = FDelete.status;
- /* Verify status */
- if (wStatus == 0)
- {
- bOK = 1;
- byStep = 7;
- }
- else if (wStatus != 65535)
- {
- byErrorLevel = 6;
- byStep = 0;
- if (wStatus == 20799)
- {
- wError = FileIoGetSysError();
- }
- }
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement