Beckhoff#Let’s Use the TF6421 to write XML files!

In this article, we will use the Beckhoff TF6421 to write data to an XML File for PLC variables. Let’s get started!

Reference Link

Beckhoff#Let’s use TF6421 to read a XML File!

Reference Video

Beckhoff.Let’s read data from XML File via TF6421

Beckhoff.Let’s Write the data To XML File via TF6421!

Function Block

We will introduce the Function Block used in the article.

FB_XMLSRVWRITE

FB_XmlSrvWrite here can be used to write the values of PLC variables to an XML file. The input variable sXPath must point to a valid node in the XML file specified by sFilePath.

VAR_INPUT

VariableData TypeDescription
sNetIdT_AmsNet_IdNetwork address of the TwinCAT 3 XML server, empty string for Local PC.
ePathE_OpenPathSpecify the TwinCAT system path on the target device where the file will be opened
nModeWORDYou can control how XML files are evaluated, currently only XMLSRV_SKIPMISSING and XmlSrvRead are supported.
pSymAddrDWORDAddress Offset of PLC variables to be written to the XML file.
cbSymSizeUDINTSize of PLC variables to be written to the XML file.
sFilePathT_MaxStringSpecify the path and filename of the file that FB will open. Note that the path is only for the file system of the local computer, the path cannot be used on a network.
sXPathT_MaxStringThis will be the address of the tag in the XML document from which the data is to be accessed.
bExecuteBOOLExecute FB with start-up signal.
tTimeoutTIMEMaximum running time of FB

VAR_OUTPUT

VariableData TypeDescription
bBusyBOOLTrue=FB running.
bErrorBOOLTrue=FB error present.
nErrIdUDINTError code returned by TC3 XML Server.

FB_XmlSrvWriteByName

FB_XmlSrvWriteByName here can be used to write the value of a PLC variable to an XML file. The input variable sXPath must point to a valid node in the XML file specified by sFilePath.

VAR_INPUT

VariableData TypeDescription
sNetIdT_AmsNet_IdNetwork address of the TwinCAT 3 XML server, empty string for Local PC.
ePathE_OpenPathSpecify the TwinCAT system path on the target device where the file will be opened
nModeWORDYou can control how XML files are evaluated, currently only XMLSRV_SKIPMISSING and XmlSrvRead are supported.
sSymNameT_MaxStringXThis will be the name of the PLC symbol to be written to the ML file.
sFilePathT_MaxStringSpecify the path and filename of the file that FB will open. Note that the path is only for the file system of the local computer, the path cannot be used on a network.
sXPathT_MaxStringThis will be the address of the tag in the XML document from which the data is to be accessed.
bExecuteBOOLExecute FB with start-up signal.
tTimeoutTIMEMaximum running time of FB

VAR_OUTPUT

VariableData TypeDescription
bBusyBOOLTrue=FB running.
bErrorBOOLTrue=FB error present.
nErrIdUDINTError code returned by TC3 XML Server.

Implementation1

Implementation 1 uses the FB_XmlSrvWrite Function Block to write PLC variables to the XML File.

DUT_XMLWrite 

This is the structure of the variable to be written to the XML File in Implementation 1.

TYPE DUT_XMLWrite :
STRUCT
myBool:BOOL;
myReal:REAL;
myInt:INT;
myArray:ARRAY[0..41]OF DINT;
END_STRUCT
END_TYPE

MAIN

VAR

This is the variable used in Implemenation 1.

PROGRAM MAIN
VAR
//Example1
myValue1:DUT_XMLWrite;
fbxmlSrvWrite:FB_XmlSrvWrite;
bExecuteXMLWrite:BOOL;
sFilePathXMLWrite: T_MaxString:=’C:\Users\threespc02\OneDrive\Desktop\myXML1.xml’;
sXPathXMLWrite : T_MaxString:=’/dataentry/MAIN.value1′;
iCoutner:INT;
iCheck:INT;

END_VAR

Program

fbxmlSrvWrite(
nMode:=XMLSRV_ADDMISSING
,pSymAddr:=ADR(myValue1)
,cbSymSize:=SIZEOF(myValue1)
,sFilePath:=sFilePathXMLWrite
,sXPath:=sXPathXMLWrite
,bExecute:=bExecuteXMLWrite
);

IF bExecuteXMLWrite AND (fbxmlSrvWrite.bBusy OR fbxmlSrvWrite.bError) THEN
iCheck:=iCheck+1;
bExecuteXMLWrite:=False;
END_IF;

Result

Set bExecuteXMLWrite to True and execute Function Block.

myXML1.xml has been generated on the TwinCAT3 IPC.

Data was also written to the XML File!

Implementation2

Implemenation2FB_XmlSrvWriteByName Write a PLC variable to an XML File using the Function Block.

DUT_XMLWriteByName 

This is the structure of the variable to be written to the XML File in Implementation2.

TYPE DUT_XMLWriteByName :
STRUCT
BoolData:BOOL;
RealData:REAL;
ArrayInt:ARRAY[0..3]OF INT;
stData :DUT_XMLWrite;
END_STRUCT
END_TYPE

MAIN

VAR

This is the variable used in Implementation 2.

PROGRAM MAINVAR //Example2
myValue2_XMLWriteByName:DUT_XMLWriteByName;
bExecuteXMLWritebyName:BOOL;
fbxmlSrvWriteByName:FB_XmlSrvWriteByName;
sFilePathWriteByName: T_MaxString:=’C:\Users\threespc02\OneDrive\Desktop\myXML2.xml’;
sXPathWriteByName : T_MaxString:=’/dataentry/value2′;
sSymName: T_MaxString:=’MAIN.myValue2_XMLWriteByName’;END_VAR

Program

myValue2_XMLWriteByName.BoolData:=TRUE;
myValue2_XMLWriteByName.RealData:=3.14;
myValue2_XMLWriteByName.stData.myBool:=TRUE;
myValue2_XMLWriteByName.stData.myReal:=10.1234;
myValue2_XMLWriteByName.stData.myInt:=5002;

FOR iCoutner:=0 TO 41 DO
myValue2_XMLWriteByName.stData.myArray[iCoutner]:=iCoutner+1;
END_FOR;
FOR iCoutner:=0 TO 3 DO
myValue2_XMLWriteByName.ArrayInt[iCoutner]:=(iCoutner+1)*20;
END_FOR;

fbxmlSrvWriteByName(
nMode:=XMLSRV_ADDMISSING
,sSymName:=sSymName
,sFilePath:=sFilePathWriteByName
,sXPath:=sXPathWriteByName
,bExecute:=bExecuteXMLWritebyName
);


IF bExecuteXMLWritebyName AND (fbxmlSrvWriteByName.bBusy OR fbxmlSrvWriteByName.bError) THEN
iCheck:=iCheck+1;
bExecuteXMLWritebyName:=FALSE;
END_IF;

Result

Set bExecuteXMLWritebyName to True and execute Function Block.

The myXML2.xml was generated on the TwinCAT3 IPC.

Structures and arrays of data were also written to the XML File!

Implementation3

Implementation generates a random number in TwinCAT3, if the random number is smaller than 0.2, it retrieves the TwinCAT3 Runtime time and writes the random number value and the time at that time in an XML File.

DUT_XMLLogging 

This is the structure of the variable to be written to the XML File in Implemenation 3.

TYPE DUT_XMLLogging :
STRUCT
logTime:STRING(50);
Value:REAL;
END_STRUCT
END_TYPE

MAIN

VAR

こちらはImplemenation3で使用したした変数になります。

PROGRAM MAIN
//Example3
MyTimeStruct :TIMESTRUCT;
ntGetTime :NT_GetTime;
iState :INT;
mytimer :TON;
sMyLoggingFilePath :T_MaxString:=’C:\Users\threespc02\OneDrive\Desktop\myXMLLogging.xml’;
sMyLogginXPath :T_MaxString:=’/dataentry/Logging’;
sXPath4 :T_MaxString;
myLoggintTime :T_MaxString;
rand :DRAND;
myLogValue :REAL;
myXMLLoggingStructure :DUT_XMLLogging;
fbxmlSrvWriteLogging :FB_XmlSrvWrite;
bExecute2Log :BOOL;
iDataCounter :INT:=0;
END_VAR

Program

CASE iState OF

0:
ntGetTime(START:=FALSE);
mytimer(IN:=FALSE,PT:=T#1S);

rand();
myLogValue:=rand.Num;
IF myLogValue<0.2 THEN
iState:=10;
iCheck:=iCheck+1;
ELSE
iState:=5;
END_IF
5:
mytimer(IN:=TRUE);
IF mytimer.Q THEN
mytimer(IN:=FALSE);
iState:=0;
END_IF
10:
mytimer(IN:=TRUE);
IF mytimer.Q THEN
mytimer(IN:=FALSE);
iState:=20;
END_IF
20:
ntGetTime(START:=TRUE,TIMESTR=>MyTimeStruct);
IF NOT ntGetTime.ERR AND NOT ntGetTime.BUSY THEN
iState:=30;
END_IF
30:
myLoggintTime:=SYSTEMTIME_TO_STRING(MyTimeStruct);
iState:=40;
40:
sXPath4:=CONCAT(sMyLogginXPath,INT_TO_STRING(iDataCounter));
iState:=50;
iDataCounter:=iDataCounter+1;
50:
myXMLLoggingStructure.logTime:=myLoggintTime;
myXMLLoggingStructure.Value:=myLogValue;
bExecute2Log:=TRUE;
IF fbxmlSrvWriteLogging.bBusy THEN
iState:=60;
END_IF
IF fbxmlSrvWriteLogging.bError THEN
iState:=990;
END_IF
60:
bExecute2Log:=FALSE;
IF NOT fbxmlSrvWriteLogging.bBusy THEN
iState:=100;
bExecuteXMLWrite:=FALSE;
END_IF
IF fbxmlSrvWriteLogging.bError THEN
iState:=990;
END_IF
100:
iState:=0;
990:
;
END_CASE

fbxmlSrvWriteLogging(
nMode:=XMLSRV_ADDMISSING
,bExecute:=bExecute2Log
,pSymAddr:=ADR(myXMLLoggingStructure)
,cbSymSize:=SIZEOF(myXMLLoggingStructure)
,sFilePath:=sMyLoggingFilePath
,sXPath:=sXPath4
);

Result

The current value of iState is now a random number, which allows new data to be written to the XML File.

myXMLLogging.xml has been generated on the TwinCAT3 IPC.

The logging time and the random value at that time were also written to the XML File as structure data!

Footer_Basic

Please Support some devices for my blog

Amazon Gift List

Find ME

Twitter:@3threes2
Email:soup01threes*gmail.com (* to @)
YoutubeChannel:https://www.youtube.com/channel/UCQ3CHGAIXZAbeOC_9mjQiWQ

シェアする

  • このエントリーをはてなブックマークに追加

フォローする