File_over_EtherCATΒΆ
Function blocks for FoE (File over EtherCAT)
Example:
File over EtherCAT
This example shows how to download a firmware file with File over EtherCAT. It is also demonstrated how to get information about the master and slaves, like the number of active slaves or the state of the slave. The File over EtherCAT functions are implemented theoretically, without hardware.
PROGRAM FoE
VAR
FoEDownload : ETC_FoE_Download;
FoEMailBox : ETC_FoE_Mailbox;
FoEUpload : ETC_FoE_Upload;
abyDataDownload : ARRAY[1..16] OF BYTE;
dwPassword : DWORD := 16#00000000;
sFileName : STRING :='firmwarefile';(* File on the device*)
sErrorText : STRING;
abyDataUpload : ARRAY[1..16] OF BYTE;
udiBytesRead : UDINT; (* Bytes read from the Upload *)
uiReceived : UINT; (* bytes received from the Mailbox *)
abyMailInput : ARRAY[1..16] OF BYTE;
abyMailOutput : ARRAY[1..16] OF BYTE;
hFileHandle : RTS_IEC_HANDLE;
Result : RTS_IEC_RESULT;
xwFileSize : __XWORD;
pbyFirmware : POINTER TO BYTE;
pSlave : POINTER TO ETCSlave; (* Pointer to slave in in the device Tree *)
iState : INT;
END_VAR
(* This File over EtherCAT demonstrates a firmware download to the EtherCAT Slave. The specific firmware file is opened. Afterwards
the memory is allocated dynamically. The slave is set to Boot Mode (this is only necessary for firmware download)
and then the data is downloaded and the memory is released.*)
EtherCAT_Master();
CASE iState OF
0:
(* Wait for the EtherCAT to be ready. *)
IF Ethercat_Master.xConfigFinished THEN
iState := iState + 1;
END_IF
1: (* Get the size of the file for the memory allocation *)
xwFileSize := SysFileGetSize(szFileName := sFileName, pResult := ADR(Result));
(* Allocate the memory dynamically.*)
pbyFirmware := SysMemAllocData(szComponent := 'FoE', udiSize := xwFileSize, pResult := ADR(Result));
IF pbyFirmware = 0 THEN
iState := 32767;
END_IF
(* Open the file, which includes the firmware. *)
hFileHandle := SysFileOpen(szFile := sFileName, am := ACCESS_MODE.AM_READ, pResult := ADR(Result));
IF Result <> 0 THEN
iState := 32767;
END_IF
(* Read the firmware file. *)
SysFileRead(hFile := hFileHandle, pbyBuffer := pbyFirmware, ulSize := xwFileSize, pResult := ADR(Result));
IF Result <> 0 THEN
iState := 32767;
END_IF
(* Close the file, because it is not needed anymore *)
Result := SysFileClose(hFile := hFileHandle);
IF Result <> 0 THEN
iState := 32767;
END_IF
(* Search for the correct slave. The address is found unter the device in the "Slave" tab. Hence this is just a
demonstration, the address could be another one.*)
WHILE pSlave <> 0 DO
(* get the address of the Slave fith FoE Function. This can also be another number.*)
IF pSlave^.SlaveAddr = 1004 THEN
(* Set the slave in boot mode. This is neccessary for the firmware download. *)
IF NOT pSlave^.SetOpMode(wOpMode := ETC_SLAVE_STATE.ETC_SLAVE_BOOT) THEN
iState := 32767;
END_IF
EXIT;
END_IF
pSlave := pSlave^.NextInstance;
END_WHILE
2: (* Download the firmware, to the specified slave. The filename is the file in the device
to where the data is downloaded.*)
FoEDownload(xExecute := TRUE, usiCom := 1, uiDevice:= 1004, pbyData :=pbyFirmware,
udiDataSize := __XWORD_TO_UDINT(xwFileSize), dwPassword := dwPassword, strFilename := sFileName);
IF FoEDownload.xDone THEN
sErrorText := FoEDownload.strErrorText; (* Save the error text *)
iState := iState + 1;
FoEDownload(xExecute := FALSE);
ELSIF FoEDownload.xError THEN
iState := 32767;
END_IF
3: (* Release the used memory *)
SysMemFreeData(szComponent := 'FoE', pMemory := pbyFirmware);
4: (* An example, how to do an upload from the device.*)
FoEUpload(xExecute := TRUE, usiCom := 1, uiDevice := 1004, pbyData := ADR(abyDataUpload),
udiDataSize := SIZEOF(abyDataUpload), dwPassword := dwPassword, strFilename := sFileName);
IF FoEUpload.xDone THEN
udiBytesRead := FoEUpload.udiDataRead;
iState := iState + 1;
FoEUpload(xExecute := FALSE);
ELSIF FoEUpload.xError THEN
iState := 32767;
END_IF
32767: (* error *)
END_CASE