TwinCAT 3 读写xml程序

 GVL_config_xml

VAR_GLOBAL
	configXml:ST_config_xml;
END_VAR

 ST

TYPE ST_config_xml :
STRUCT
	control:ST_config_xml_control;
	parameter:ST_config_xml_parameter;
	status:ST_config_xml_status;
END_STRUCT
END_TYPE

TYPE ST_config_xml_control :
STRUCT
	bWrite:BOOL;
	bRead:BOOL:=TRUE;
END_STRUCT
END_TYPE

TYPE ST_config_xml_parameter :
STRUCT
	sSymName:T_MaxString:='GVL_parameter.configData';
 	sFilePath:T_MaxString:='C:\PlcData\Config\Parameter.XML';
	sXPath:T_MaxString:='/configData';
END_STRUCT
END_TYPE

TYPE ST_config_xml_status :
STRUCT
	bWriteDone:BOOL;
	bReadDone:BOOL;

	bError:BOOL;
	sComment:WSTRING:="无信息";
END_STRUCT
END_TYPE

P_config_xml

PROGRAM P_config_xml
VAR
	nWriteStep:INT;
	nReadStep:INT;

	nReadFail:INT;

	XmlSrvReadByNameFb:FB_XmlSrvReadByName;
	XmlSrvWriteByNameFb:FB_XmlSrvWriteByName;
END_VAR
//写入配置信息步
CASE nWriteStep OF
	ENUM_config_xml.nInitial://复位动作
		XmlSrvWriteByNameFb.bExecute:=FALSE;

		IF configXml.control.bWrite THEN
			configXml.status.bError:=FALSE;
			configXml.status.sComment:="";
			configXml.status.bWriteDone:=FALSE;
			configXml.control.bWrite:=FALSE;
			nWriteStep:=ENUM_config_xml.nWrite;
		END_IF

	ENUM_config_xml.nWrite://写入数据
		XmlSrvWriteByNameFb.bExecute:=TRUE;
		IF XmlSrvWriteByNameFb.bBusy THEN
			nWriteStep:=ENUM_config_xml.nWhetherWrite;
		END_IF

	ENUM_config_xml.nWhetherWrite://判断数据是否写入
		IF XmlSrvWriteByNameFb.bError THEN
			configXml.status.bError:=TRUE;
			configXml.status.sComment:="写配置失败";
			nWriteStep:=ENUM_config_xml.nInitial;
		END_IF
		IF NOT XmlSrvWriteByNameFb.bBusy AND NOT XmlSrvWriteByNameFb.bError THEN
			configXml.status.bWriteDone:=TRUE;
			configXml.status.sComment:="写配置写入完成";
			nWriteStep:=ENUM_config_xml.nInitial;
		END_IF
END_CASE

//读取配置信息步
CASE nReadStep OF
	ENUM_config_xml.nInitial://复位动作
		XmlSrvReadByNameFb.bExecute:=FALSE;

		IF configXml.control.bRead THEN
			configXml.status.bError:=FALSE;
			configXml.status.sComment:="";
			configXml.status.bReadDone:=FALSE;
			configXml.control.bRead:=FALSE;
			nReadStep:=ENUM_config_xml.nRead;
		END_IF

	ENUM_config_xml.nRead://读取数据
		XmlSrvReadByNameFb.bExecute:=TRUE;
		IF XmlSrvReadByNameFb.bBusy THEN
			nReadStep:=ENUM_config_xml.nWhetherRead;
		END_IF

	ENUM_config_xml.nWhetherRead://判断数据是否读取
		IF XmlSrvReadByNameFb.bError THEN
			configXml.status.bError:=TRUE;
			configXml.status.sComment:="读配置失败";
			nReadFail:=nReadFail+1;
			IF nReadFail<5 THEN
				configXml.control.bRead:=TRUE;
			END_IF
			nReadStep:=ENUM_config_xml.nInitial;
		END_IF
		IF NOT XmlSrvReadByNameFb.bBusy AND NOT XmlSrvReadByNameFb.bError THEN
			configXml.status.bReadDone:=TRUE;
			configXml.status.sComment:="读配置读取完成";
			nReadStep:=ENUM_config_xml.nInitial;
		END_IF
END_CASE

//功能块
XmlSrvReadByNameFb(
	sNetId:='' , 
	ePath:= , 
	nMode:= , 
	sSymName:=configXml.parameter.sSymName , 
	sFilePath:=configXml.parameter.sFilePath , 
	sXPath:=configXml.parameter.sXPath , 
	bExecute:= , 
	tTimeout:=T#10S , 
	bBusy=> , 
	bError=> , 
	nErrId=> );

XmlSrvWriteByNameFb(
	sNetId:='' , 
	ePath:= , 
	nMode:=XMLSRV_ADDMISSING , 
	sSymName:=configXml.parameter.sSymName , 
	sFilePath:=configXml.parameter.sFilePath , 
	sXPath:=configXml.parameter.sXPath , 
	bExecute:= , 
	tTimeout:=T#10S , 
	bBusy=> , 
	bError=> , 
	nErrId=> );

你可能感兴趣的:(TwinCAT,3,TwinCAT,3)