I have solved it. I used fbIL_ECATRemoteSlaveGetDevState1.ActDevState = ECAT_STATE_OP instead of eActBusState1. Declaration: PROGRAM EtherCAT_Auto_Switching_OP_INIT
VAR
fbIL_ECATRemoteSlaveGetDevState1: IL_ECATRemoteSlaveGetDevState;
strMasterName : STRING := 'ethercatmaster';
eActBusState1 : IL_ECAT_STATE;
eActDevState1: CXA_EthercatMaster.IL_ECAT_STATE;
fbIL_ECATRemoteSlaveGetDevState2: IL_ECATRemoteSlaveGetDevState;
eActBusState2 : IL_ECAT_STATE;
eActDevState2: CXA_EthercatMaster.IL_ECAT_STATE;
fbIL_ECATRemoteSlaveGetDevState3: IL_ECATRemoteSlaveGetDevState;
eActBusState3 : IL_ECAT_STATE;
eActDevState3: CXA_EthercatMaster.IL_ECAT_STATE;
fbIL_ECATRemoteSlaveGetDevState4: IL_ECATRemoteSlaveGetDevState;
eActBusState4 : IL_ECAT_STATE;
eActDevState4: CXA_EthercatMaster.IL_ECAT_STATE;
fbBLINK: BLINK;
bBlinkOut: BOOL;
//AUTO Switching Condition
fbIL_ECATRemoteSlaveSetDevState1: IL_ECATRemoteSlaveSetDevState;
fbIL_ECATRemoteSlaveSetDevState2: IL_ECATRemoteSlaveSetDevState;
fbIL_ECATRemoteSlaveSetDevState3: IL_ECATRemoteSlaveSetDevState;
fbIL_ECATRemoteSlaveSetDevState4: IL_ECATRemoteSlaveSetDevState;
iSetMaster: INT;
bExecute: BOOL;
fbECATMasterSetBusState: IL_ECATMasterSetBusState;
eActBusState: IL_ECAT_STATE;
fbECATMasterGetBusState: IL_ECATMasterGetBusState;
END_VAR Implementation: //Read EtherCAT State
//1001
fbBLINK(ENABLE:= TRUE, TIMELOW:= T#1S, TIMEHIGH:= T#1S, OUT=> bBlinkOut);
fbIL_ECATRemoteSlaveGetDevState1.Execute := bBlinkOut;
fbIL_ECATRemoteSlaveGetDevState1.MasterName := ADR(strMasterName);
fbIL_ECATRemoteSlaveGetDevState1.SlaveAddress := 1001;
fbIL_ECATRemoteSlaveGetDevState1();
IF TRUE = fbIL_ECATRemoteSlaveGetDevState1.Done THEN
eActDevState1 := fbIL_ECATRemoteSlaveGetDevState1.ActDevState;
END_IF
IF TRUE = fbIL_ECATRemoteSlaveGetDevState1.Error THEN
; // error handling
END_IF
//1002
fbIL_ECATRemoteSlaveGetDevState2.Execute := bBlinkOut;
fbIL_ECATRemoteSlaveGetDevState2.MasterName := ADR(strMasterName);
fbIL_ECATRemoteSlaveGetDevState2.SlaveAddress := 1002;
fbIL_ECATRemoteSlaveGetDevState2();
IF TRUE = fbIL_ECATRemoteSlaveGetDevState2.Done THEN
eActDevState2 := fbIL_ECATRemoteSlaveGetDevState2.ActDevState;
END_IF
IF TRUE = fbIL_ECATRemoteSlaveGetDevState2.Error THEN
; // error handling
END_IF
//1003
fbIL_ECATRemoteSlaveGetDevState3.Execute := bBlinkOut;
fbIL_ECATRemoteSlaveGetDevState3.MasterName := ADR(strMasterName);
fbIL_ECATRemoteSlaveGetDevState3.SlaveAddress := 1003;
fbIL_ECATRemoteSlaveGetDevState3();
IF TRUE = fbIL_ECATRemoteSlaveGetDevState3.Done THEN
eActDevState3 := fbIL_ECATRemoteSlaveGetDevState3.ActDevState;
END_IF
IF TRUE = fbIL_ECATRemoteSlaveGetDevState3.Error THEN
; // error handling
END_IF
//1004
fbIL_ECATRemoteSlaveGetDevState4.Execute := bBlinkOut;
fbIL_ECATRemoteSlaveGetDevState4.MasterName := ADR(strMasterName);
fbIL_ECATRemoteSlaveGetDevState4.SlaveAddress := 1004;
fbIL_ECATRemoteSlaveGetDevState4();
IF TRUE = fbIL_ECATRemoteSlaveGetDevState4.Done THEN
eActDevState4 := fbIL_ECATRemoteSlaveGetDevState4.ActDevState;
END_IF
IF TRUE = fbIL_ECATRemoteSlaveGetDevState4.Error THEN
; // error handling
END_IF
//AUTO Switching Condition
fbECATMasterGetBusState.Execute := bBlinkOut;
fbECATMasterGetBusState.MasterName := ADR(strMasterName);
fbECATMasterGetBusState();
fbECATMasterSetBusState.Execute := bExecute;
fbECATMasterSetBusState.MasterName := ADR(strMasterName);
CASE iSetMaster OF
0: //Automatically Start every slave in OP mode
IF NOT (fbIL_ECATRemoteSlaveGetDevState1.ActDevState = ECAT_STATE_OP) AND (fbIL_ECATRemoteSlaveGetDevState2.ActDevState = ECAT_STATE_OP) AND (fbIL_ECATRemoteSlaveGetDevState3.ActDevState = ECAT_STATE_OP) AND (fbIL_ECATRemoteSlaveGetDevState4.ActDevState = ECAT_STATE_OP) THEN
fbECATMasterSetBusState.NewBusState := IL_ECAT_STATE.ECAT_STATE_INIT;
ELSIF fbECATMasterGetBusState.ActBusState = ECAT_STATE_INIT THEN
fbECATMasterSetBusState.NewBusState := IL_ECAT_STATE.ECAT_STATE_OP;
END_IF
1:
fbECATMasterSetBusState.NewBusState := IL_ECAT_STATE.ECAT_STATE_INIT;
2:
fbECATMasterSetBusState.NewBusState := IL_ECAT_STATE.ECAT_STATE_PREOP;
3:
fbECATMasterSetBusState.NewBusState := IL_ECAT_STATE.ECAT_STATE_SAFEOP;
4:
fbECATMasterSetBusState.NewBusState := IL_ECAT_STATE.ECAT_STATE_OP;
END_CASE
fbECATMasterSetBusState();
IF fbECATMasterSetBusState.Done THEN
; // Master reached operational state
END_IF
IF TRUE = fbECATMasterSetBusState.Error THEN
; // Error handling
END_IF
... View more