Hello and In this tutorial I will explain how to Search files from specific keywords in a Directory. Different things but just some know-how.I will share how to do it.
Function Block
This Function Block can return all the files with specific Keywords in a directory.And it only can start a new search only if the previous search has been fully completed.
The search is completed only if bEOE is True or eCmd is eEnumCmd_Abort.
For the TwinCAT system, the search may not yet be completed if the PLC application has already found the file or directory that was sought.And If bEOE=TRUE was reached or if an error occurs, eEnumCmd_Abort is automatically executed internally.
VAR_INPUT
sNetID | T_AmsNetID | The NetworkId of the target TwinCAT system. The String can be empty for the local System. |
sPathName | T_MaxString | The Directory or Directory with specific name.But Also with special wildcards(* and?) |
eCmd | E_EnumCmdType := eEnumCmd_First | The Command of the FB |
pFindList | DWORD | Array variable address (pointer variable) of type: ST_FindFileEntry. |
cbFindList | UDINT | Array variable byte size of type: ST_FindFileEntry. |
bExecute | BOOL | The command is executed with a positive edge at the input |
tTimeout | TIME := DEFAULT_ADS_TIMEOUT | Maximum time allowed for the execution of the ADS command. |
VAR_OUTPUT
bBusy | BOOL | True= the function block is activated |
bError | BOOL | True=ADS Error |
nErrId | UDINT | The Error information |
bEOE | BOOL | End of enumeration was reached |
nFindFiles | UDINT | Number of valid files in the buffer. |
TYPE E_EnumCmdType
Enumeration command parameter used by enumeration function blocks. Not all parameters are supported by every function block!
TYPE E_EnumCmdType : ( eEnumCmd_First := 0, eEnumCmd_Next, eEnumCmd_Abort ); END_TYPE |
eEnumCmd_First | Enumerate first element. |
eEnumCmd_Next | Enumerate next element. |
eEnumCmd_Abort | Command to abort enumeration (closes opened handles). |
TYPE ST_FindFileEntry
Here is the DUT to store the result of FB_EnumFindFileList.
sFileName | T_MaxString | A null-terminated string that is the name of the file. |
sAlternateFileName | STRING(13) | A null-terminated string that is an alternative name for the file. |
fileAttributes | ST_FileAttributes | file attributes of the file or directory |
fileSize | T_ULARGE_INTEGER | 64 bit unsigned integer. The size of the file is equal to (dwHighPart * (0xFFFFFFFF+1)) + dwLowPart. |
creationTime | T_FILETIME | specifies when the file or directory was created. |
lastAccessTime | T_FILETIME | specifies when the file was last read from or written to.For a directory |
lastWriteTime | T_FILETIME | specifies when the file was last written to |
TYPE T_FILETIME
The T_FILETIME structure is a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).
dwLowDateTime | DWORD |
dwHighDateTime | DWORD |
FUNCTION FILETIME_TO_DT
This Function converts time from FILETIME format into DATE_AND_TIME format (DT),allowed minimum conforms to the value DT#1970-01-01-00:00:00 and the maximum to DT#2106-02-06-06:28:15.
VAR_INPUT
fileTime | T_FILETIME |
Implementation
I will use this Function Block to implement this image browsing operation and show the properties of that image.
VAR
VAR EnumFindFileList:FB_EnumFindFileList; FindList:ARRAY[0..49]OF ST_FindFileEntry; bExecute:BOOL; commnd:E_EnumCmdType; bBusy:BOOL; hmiIndex:INT:=0; bError: BOOL; nErrID: UDINT; bEOE: BOOL; nFindFiles: UDINT; hmiDisplayFileEntry:DUT_HMIFileEntry_Display; FileTime:DT; sBasicPath:STRING:=’file:\\\C:\FTPFiles\’; bDisplay:BOOL; hmiIndexDisplayBackup:INT; END_VAR |
Code
EnumFindFileList( sNetId:=’192.168.217.146.1.1′ ,sPathName:=’C:\FTPFiles\*.jpg’ ,eCmd:=commnd ,pFindList:=ADR(FindList) ,cbFindList:=SIZEOF(FindList) ,bExecute:=bExecute ,bBusy=>bBusy ,bError=>bError ,nErrID=>nErrID ,bEOE=>bEOE ,nFindFiles=>nFindFiles ); IF bExecute AND (EnumFindFileList.bEOE OR EnumFindFileList.bError)THEN bExecute:=False; END_IF IF hmiIndex >=0 AND hmiIndex <=15 THEN hmiDisplayFileEntry.FileEntry:=FindList[hmiIndex]; END_IF IF hmiIndex <0 THEN hmiIndex:=15; ELSIF hmiIndex >15 OR hmiIndex > EnumFindFileList.nFindFiles-1 THEN hmiIndex:=0; END_IF IF hmiIndex <>hmiIndexDisplayBackup THEN hmiIndexDisplayBackup:=hmiIndex; bDisplay:=TRUE; END_IF hmiDisplayFileEntry.CreationTime:= FILETIME_TO_DT( fileTime:= hmiDisplayFileEntry.FileEntry.creationTime); hmiDisplayFileEntry.LastAccesTime:= FILETIME_TO_DT( fileTime:= hmiDisplayFileEntry.FileEntry.lastAccessTime); hmiDisplayFileEntry.LastModificationTime:= FILETIME_TO_DT( fileTime:= hmiDisplayFileEntry.FileEntry.lastWriteTime); hmiDisplayFileEntry.ImagePath:=CONCAT(STR1:=sBasicPath,STR2:=hmiDisplayFileEntry.FileEntry.sFileName); GVL.currentPath:=hmiDisplayFileEntry.FileEntry.sFileName; |
Source Code
Please download the project from follow link:
https://github.com/soup01Threes/TwinCAT3/blob/main/Example_FB_EnumFindFileList.tnzip