signed

QiShunwang

“诚信为本、客户至上”

TwinCAT 3 报警程序

2021/6/3 17:12:02   来源:

TwinCAT 3 报警程序

//******报警码赋值******//
FOR i:=0 TO nAlarmMaxNum BY 1 DO
	alarm[i].nCode:=i;
END_FOR

//******故障报警******//
FOR i:=nAlarmFaultStartNum TO nAlarmFaultStartNum+nFaultMaxNum-1 BY 1 DO
	alarm[i].bState:=fault[i-nAlarmFaultStartNum+1].bState;
	alarm[i].sMessage:=fault[i-nAlarmFaultStartNum+1].sMessage;
END_FOR

//******安全门报警******//
FOR i:=nAlarmSafetyDoorStartNum TO nAlarmSafetyDoorStartNum+nSafetyDoorMaxNum-1 BY 1 DO
	alarm[i].sMessage:=WCONCAT("安全门:",INT_TO_WSTRING(i-nAlarmSafetyDoorStartNum+1));
	alarm[i].sMessage:=WCONCAT(alarm[i].sMessage,"  描述:");
	alarm[i].sMessage:=WCONCAT(alarm[i].sMessage,safetyDoor[i-nAlarmSafetyDoorStartNum+1].sMessage);

	IF safetyDoor[i-nAlarmSafetyDoorStartNum+1].status.bCloseError THEN
		alarm[i].bState:=TRUE;
		alarm[i].sMessage:=WCONCAT(alarm[i].sMessage,":报警");
	ELSE
		alarm[i].bState:=FALSE;
		alarm[i].sMessage:=WCONCAT(alarm[i].sMessage,":报警已消除");
	END_IF
END_FOR

(*
//******安全光栅报警******//
FOR i:=nAlarmSafetySensorStartNum TO nAlarmSafetySensorStartNum+nSafetySensorMaxNum-1 BY 1 DO
	alarm[i].sMessage:=WCONCAT("安全光栅:",INT_TO_WSTRING(i-nAlarmSafetySensorStartNum+1));
	alarm[i].sMessage:=WCONCAT(alarm[i].sMessage,"  描述:");
	alarm[i].sMessage:=WCONCAT(alarm[i].sMessage,safetySensorStatus[i-nAlarmSafetySensorStartNum+1].sMessage);

	IF safetySensorStatus[i-nAlarmSafetySensorStartNum+1].bError THEN
		alarm[i].bState:=TRUE;
		alarm[i].sMessage:=WCONCAT(alarm[i].sMessage,":报警");
	ELSE
		alarm[i].bState:=FALSE;
		alarm[i].sMessage:=WCONCAT(alarm[i].sMessage,":报警已消除");
	END_IF
END_FOR
*)

//******轴报警******//
FOR i:=nAlarmAxisStartNum TO nAlarmAxisStartNum+nAxisMaxNum-1 BY 1 DO
	alarm[i].sMessage:=WCONCAT("轴:",INT_TO_WSTRING(i-nAlarmAxisStartNum+1));
	alarm[i].sMessage:=WCONCAT(alarm[i].sMessage,"  描述:");
	alarm[i].sMessage:=WCONCAT(alarm[i].sMessage,axis[i-nAlarmAxisStartNum+1].sMessage);

	IF axis[i-nAlarmAxisStartNum+1].status.bHomeError THEN
		alarm[i].bState:=TRUE;
		alarm[i].sMessage:=WCONCAT(alarm[i].sMessage,":回原点报警");
	ELSIF axis[i-nAlarmAxisStartNum+1].status.bJogError THEN
		alarm[i].bState:=TRUE;
		alarm[i].sMessage:=WCONCAT(alarm[i].sMessage,":点动报警");
	ELSIF axis[i-nAlarmAxisStartNum+1].status.bMoveAbsoluteError THEN
		alarm[i].bState:=TRUE;
		alarm[i].sMessage:=WCONCAT(alarm[i].sMessage,":绝对运动报警");
	ELSIF axis[i-nAlarmAxisStartNum+1].status.bMoveRelativeError THEN
		alarm[i].bState:=TRUE;
		alarm[i].sMessage:=WCONCAT(alarm[i].sMessage,":相对运动报警");
	ELSE
		alarm[i].bState:=FALSE;
		alarm[i].sMessage:=WCONCAT(alarm[i].sMessage,":报警已消除");
	END_IF
END_FOR

//******马达报警******//
FOR i:=nAlarmMotorStartNum TO nAlarmMotorStartNum+nMotorMaxNum-1 BY 1 DO
	alarm[i].sMessage:=WCONCAT("马达:",INT_TO_WSTRING(i-nAlarmMotorStartNum+1));
	alarm[i].sMessage:=WCONCAT(alarm[i].sMessage,"  描述:");
	alarm[i].sMessage:=WCONCAT(alarm[i].sMessage,motor[i-nAlarmMotorStartNum+1].sMessage);

	IF motor[i-nAlarmMotorStartNum+1].status.bRunForwardError THEN
		alarm[i].bState:=TRUE;
		alarm[i].sMessage:=WCONCAT(alarm[i].sMessage,":正转报警");
	ELSIF motor[i-nAlarmMotorStartNum+1].status.bRunBackwardError THEN
		alarm[i].bState:=TRUE;
		alarm[i].sMessage:=WCONCAT(alarm[i].sMessage,":反转报警");
	ELSE
		alarm[i].bState:=FALSE;
		alarm[i].sMessage:=WCONCAT(alarm[i].sMessage,":报警已消除");
	END_IF
END_FOR

//******气缸报警******//
FOR i:=nAlarmCylinderStartNum TO nAlarmCylinderStartNum+nCylinderMaxNum-1 BY 1 DO
	alarm[i].sMessage:=WCONCAT("气缸:",INT_TO_WSTRING(i-nAlarmCylinderStartNum+1));
	alarm[i].sMessage:=WCONCAT(alarm[i].sMessage,"  描述:");
	alarm[i].sMessage:=WCONCAT(alarm[i].sMessage,cylinder[i-nAlarmCylinderStartNum+1].sMessage);

	IF cylinder[i-nAlarmCylinderStartNum+1].status.bOpenError THEN
		alarm[i].bState:=TRUE;
		alarm[i].sMessage:=WCONCAT(alarm[i].sMessage,":打开报警");
	ELSIF cylinder[i-nAlarmCylinderStartNum+1].status.bCloseError THEN
		alarm[i].bState:=TRUE;
		alarm[i].sMessage:=WCONCAT(alarm[i].sMessage,":关闭报警");
	ELSE
		alarm[i].bState:=FALSE;
		alarm[i].sMessage:=WCONCAT(alarm[i].sMessage,":报警已消除");
	END_IF
END_FOR

//******报警记录******//
FOR i:=1 TO nAlarmMaxNum BY 1 DO 
	//复位写log
	IF alarmLog[i].control.bWrite THEN
		alarmLog[i].control.bWrite:=FALSE;
	END_IF

	//报警上升沿、下降沿触发
	alarm[i].rTRIG(CLK:=alarm[i].bState , Q=> );
	alarm[i].fTRIG(CLK:=alarm[i].bState , Q=> );

	//报警触发信息
	IF alarm[i].rTRIG.Q
	OR alarm[i].fTRIG.Q THEN
		alarm[i].sTime:=STRING_TO_WSTRING(systemTime.sTime);
		alarmLog[i].control.bWrite:=TRUE;
		IF i<10 THEN
			alarmLog[i].parameter.sDescription:=WCONCAT("报警码:000",INT_TO_WSTRING(alarm[i].nCode));
		ELSIF i<100 THEN
			alarmLog[i].parameter.sDescription:=WCONCAT("报警码:00",INT_TO_WSTRING(alarm[i].nCode));
		ELSIF i<1000 THEN
			alarmLog[i].parameter.sDescription:=WCONCAT("报警码:0",INT_TO_WSTRING(alarm[i].nCode));
		ELSE
			alarmLog[i].parameter.sDescription:=WCONCAT("报警码:",INT_TO_WSTRING(alarm[i].nCode));
		END_IF
		alarmLog[i].parameter.sDescription:=WCONCAT(alarmLog[i].parameter.sDescription,"  报警信息:");
		alarmLog[i].parameter.sDescription:=WCONCAT(alarmLog[i].parameter.sDescription,alarm[i].sMessage);
	END_IF

	//报警LOG功能块
	alarmLog[i].FB(
		LogControl:=alarmLog[i].control , 
		LogParameter:=alarmLog[i].parameter , 
		LogTime:=systemTime , 
		LogStatus=>alarmLog[i].status );
END_FOR

//******当前报警列表******//
//排列当前报警列表
j:=0;
FOR i:=0 TO nAlarmMaxNum BY 1 DO
	IF alarm[i].bState THEN
		alarmHMI[j].sTime:=alarm[i].sTime;
		alarmHMI[j].sCode:=INT_TO_STRING(alarm[i].nCode);
		alarmHMI[j].sMessage:=alarm[i].sMessage;
		j:=j+1;
	END_IF
END_FOR
FOR j:=j TO nAlarmMaxNum BY 1 DO
	alarmHMI[j].sTime:="";
	alarmHMI[j].sCode:='';
	alarmHMI[j].sMessage:="";
END_FOR

//******历史报警列表******//
//排列历史报警列表
FOR i:=0 TO nAlarmMaxNum BY 1 DO
	IF alarm[i].rTRIG.Q THEN
		FOR j:=nAlarmHistoryMaxNum TO 1 BY -1 DO
			alarmHistoryHMI[j].sTime:=alarmHistoryHMI[j-1].sTime;
			alarmHistoryHMI[j].sCode:=alarmHistoryHMI[j-1].sCode;
			alarmHistoryHMI[j].sMessage:=alarmHistoryHMI[j-1].sMessage;
		END_FOR
		alarmHistoryHMI[0].sTime:=alarm[i].sTime;
		alarmHistoryHMI[0].sCode:=INT_TO_STRING(alarm[i].nCode);
		alarmHistoryHMI[0].sMessage:=alarm[i].sMessage;
	END_IF
END_FOR

//******系统报警中******//
FOR i:=0 TO nAlarmMaxNum BY 1 DO
	IF alarm[i].bState THEN
		system.status.bInAlarm:=TRUE;
	END_IF
END_FOR