20798 FileOpen Error

Hello everyone,

When writing to the file whose code and configuration settings I have shared below, I encounter error code 20798 during the FileOpen function. Despite all my attempts, I have not been able to resolve this error.

My goal in using this file structure is to permanently store the values I need to write in my system. I would greatly appreciate any assistance you can provide on this matter.

Thank you.

#define DEVICE_NAME "HARDDISK"
#define FILE_NAME   "SM1800"

// Dosya yazma ve okuma islemlerinin calismasini saglar.
void FileOperationControl(file_controller_t* pFileOp)
{
	switch (pFileOp->state)
	{
		case FILE_INIT:

		
			break;
		
		case FILE_OPEN:
			/* Initialize file open structure */
			pFileOp->fileOpen.enable    = 1;
			pFileOp->fileOpen.pDevice   = (UDINT) DEVICE_NAME;
			pFileOp->fileOpen.pFile     = (UDINT) FILE_NAME;
			pFileOp->fileOpen.mode      = fiREAD_WRITE;
			FileOpen(&(pFileOp->fileOpen));

			/* Get FBK output information */
			pFileOp->ident.fileOpen  = pFileOp->fileOpen.ident;
			pFileOp->status.fileOpen = pFileOp->fileOpen.status;
		
			if (pFileOp->status.fileOpen == fiERR_FILE_NOT_FOUND)
				pFileOp->state = FILE_CREATE;
			
			else if (pFileOp->status.fileOpen == 0)
			{
				if (pFileOp->workingMode == WRITE)
					pFileOp->state = FILE_WRITE;
				else if(pFileOp->workingMode == READ)
					pFileOp->state = FILE_READ;
			}

			else if (pFileOp->status.fileOpen != FILE_STATUS_BUSY)
			{
				pFileOp->state = FILE_INIT;
				if (pFileOp->status.fileOpen == fiERR_SYSTEM)
					pFileOp->error.fileOpen = FileIoGetSysError();
			}
        
			break;
		
		case FILE_CREATE:
			 /*Initialize file create structure*/ 
			pFileOp->fileCreate.enable  = 1;
			pFileOp->fileCreate.pDevice = (UDINT) DEVICE_NAME;
			pFileOp->fileCreate.pFile   = (UDINT) FILE_NAME;
			FileCreate(&(pFileOp->fileCreate));
		
			/* Get FBK output information */
			pFileOp->ident.fileCreate  = pFileOp->fileCreate.ident;
			pFileOp->status.fileCreate = pFileOp->fileCreate.status;
		
			/* Verify status */
			if (pFileOp->status.fileCreate == 0)
				pFileOp->state = FILE_WRITE;
			else if (pFileOp->status.fileCreate != FILE_STATUS_BUSY)
			{
				pFileOp->state = FILE_INIT;
                               
				if (pFileOp->status.fileCreate == fiERR_SYSTEM)
					pFileOp->error.fileCreate = FileIoGetSysError();
			}
      
			break;
		
		case FILE_WRITE:
			WriteFileData(&(motion), &(pFileOp->writeData));

			/* Initialize file create structure */
			pFileOp->fileWrite.enable   = 1;
			pFileOp->fileWrite.ident    = pFileOp->ident.fileOpen;
			pFileOp->fileWrite.offset   = 0;
			pFileOp->fileWrite.pSrc     = (UDINT) &(pFileOp->writeData);
			pFileOp->fileWrite.len      = sizeof (pFileOp->writeData);
			FileWrite(&(pFileOp->fileWrite));
		
			/* Get status */
			pFileOp->status.fileWrite = pFileOp->fileWrite.status;
		
			/* Verify status */
			if (pFileOp->status.fileWrite == 0)
				pFileOp->state = FILE_CLOSE;
			else if (pFileOp->status.fileWrite != FILE_STATUS_BUSY)
			{
				pFileOp->state = FILE_INIT;

				if (pFileOp->status.fileWrite == fiERR_SYSTEM)
					pFileOp->error.fileWrite = FileIoGetSysError();
			}
      
			break;
		
		case FILE_READ:
			/*Initialize file create structure*/ 
			pFileOp->fileRead.enable    = 1;
			pFileOp->fileRead.ident     = pFileOp->ident.fileOpen;
			pFileOp->fileRead.offset    = 0;
			pFileOp->fileRead.pDest     = (UDINT) &(pFileOp->readData);
			pFileOp->fileRead.len       = sizeof (pFileOp->readData);
			FileRead(&(pFileOp->fileRead));
		
			/* Get status */
			pFileOp->status.fileRead = pFileOp->fileRead.status;
		
			/* Verify status */
			if (pFileOp->status.fileRead == 0)
			{
				pFileOp->state = FILE_CLOSE;
				ReadFileData(&motion,&(pFileOp->readData));
			}
			else if (pFileOp->status.fileRead != FILE_STATUS_BUSY)
			{
				pFileOp->state = FILE_INIT;
				if (pFileOp->status.fileRead == fiERR_SYSTEM)
					pFileOp->error.fileRead = FileIoGetSysError();
			}
       
			break;
		
		case FILE_CLOSE:
	
			pFileOp->fileClose.enable   = 1;
			pFileOp->fileClose.ident    = pFileOp->ident.fileOpen;
			FileClose(&(pFileOp->fileClose));
	
			/* Get status */
			pFileOp->status.fileClose = pFileOp->fileClose.status;
	
			/* Verify status */
			if (pFileOp->status.fileClose == 0)
				pFileOp->state = FILE_INIT;
			else if (pFileOp->status.fileClose != FILE_STATUS_BUSY)
			{
				pFileOp->state = FILE_INIT;
				if (pFileOp->status.fileClose == fiERR_SYSTEM)
					pFileOp->error.fileClose = FileIoGetSysError();
			}
	      
			break;
		
		case FILE_DELETE:
	
      
			break;
		
		default:

		break;
	}
}

Your File Device Configuration and DEVICE_NAME in the code do not correspond. Change either so that they match each other and you should be good to go.

File Device configuration says “HD” and DEVICE_NAME in code is “HARDDISK”.

Do you mean I should change both of them to make them different? Or should I change both of them to the same name?

I changed both of the following names, but unfortunately the resuldt did not change and I am still getting an error.

image

The name should be the same in both places, so the configuration and code.

Dear Tommi Piiparinen,

Thank you very much for your support. After about a day of trying to resolve this issue, I was able to solve it successfully. :slight_smile:

However, I have a question. In versions 4.12 and earlier, such as 4.9, I had projects where these two names were different, and they worked without any issues. As far as I remember, this wasn’t a problem in previous versions — is that correct?

Best regards,

The behaviour should be the same in the earlier versions too. So you have to match your code and file devices in order to use them. (Would not make sense if they didn’t match).

1 Like

Hi @omer.alumur ,
despite this being solved I can just recommend using already written code:

Should do what you want :slight_smile:

Best regards
Michael

2 Likes

Thank you for your interest. I will look into it.