<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to SysMemCmp SysMemCpy</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/b7b8b270b4/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/Engineering/thread/b7b8b270b4/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/Engineering/thread/b7b8b270b4/</id><updated>2025-10-24T05:09:02.383000Z</updated><subtitle>Recent posts to SysMemCmp SysMemCpy</subtitle><entry><title>SysMemCmp SysMemCpy</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/b7b8b270b4/?limit=25#ac25" rel="alternate"/><published>2025-10-24T05:09:02.383000Z</published><updated>2025-10-24T05:09:02.383000Z</updated><author><name>alessandro</name><uri>https://forge.codesys.com/u/alessandro/</uri></author><id>https://forge.codesys.com28f28ac2d25f94601bb83a044d9c0dc7ca9e4420</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;VAR&lt;br/&gt;
    Data1:ARRAY&lt;span&gt;[1..10]&lt;/span&gt; OF SINT ;&lt;br/&gt;
    Data2:ARRAY&lt;span&gt;[1..10]&lt;/span&gt; OF SINT ;&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="n"&gt;Data3&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;ARRAY&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;OF&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;SINT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Data4&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;ARRAY&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;OF&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;SINT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;ex_1&lt;/span&gt;&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;BOOL&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;ex_2&lt;/span&gt;&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;BOOL&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;enable&lt;/span&gt;&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;BOOL&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;END_VAR&lt;/p&gt;
&lt;p&gt;// PROGRAM&lt;br/&gt;
// The scope of this example is compare and copy the values of two ARRAY only if some value is different using SysMemCmp and SysMemCpy.&lt;br/&gt;
// In this 2 example we don't use a FOR cicle for do this, and pBuffer1 and pBuffer2 is just a pointer to ARRAY. See details in Library util.SysMem of Codesys&lt;br/&gt;
// The compare funcion util.SysMem.SysMemCmp is bidirectional if Data1 chanege respect Data2 ex_1 go to 1 and if Data2 change respect Data1 also&lt;br/&gt;
// but the copy function util.SysMem.SysMemCpy work only from source ARRAY pSrc:=ADR(Data1) to destination ARRAY pDest:=ADR(Data2)&lt;/p&gt;
&lt;p&gt;// Example 1 Full ARRAY compare and copy&lt;br/&gt;
// This compare 2 different equal ARRAY and if there is some difference ex_1 go to 1 and if you give enable he copy Data1 in Data2&lt;/p&gt;
&lt;p&gt;ex_1 := TO_BOOL(util.SysMem.SysMemCmp(pBuffer1:=ADR(Data1), pBuffer2:=ADR(Data2), udiCount:=SIZEOF(Data1)));&lt;br/&gt;
IF ex_1 AND enable THEN&lt;br/&gt;
    util.SysMem.SysMemCpy(pDest:=ADR(Data2), pSrc:=ADR(Data1), udiCount:=SIZEOF(Data1));&lt;br/&gt;
END_IF&lt;/p&gt;
&lt;p&gt;// Example 2 Only selected area of the ARRAY compare and copy&lt;br/&gt;
// This compare 2 different equal ARRAY starting from position number &lt;a class="alink" href="/forge/talk-ru/3/"&gt;[3]&lt;/a&gt; for 4 byte in this case start at position &lt;a class="alink" href="/forge/talk-ru/3/"&gt;[3]&lt;/a&gt; and finish at position number &lt;span&gt;[6]&lt;/span&gt;&lt;br/&gt;
// and if there is some difference only in area &lt;span&gt;[3..6]&lt;/span&gt; of the ARRAY ex_2 go to 1 if you give enalbe he copy Data3&lt;span&gt;[3..6]&lt;/span&gt; in Data4&lt;span&gt;[3..6]&lt;/span&gt;&lt;br/&gt;
// if something change in other array position&lt;span&gt;[0..2]&lt;/span&gt; or &lt;span&gt;[7..10]&lt;/span&gt; are ingnored &lt;/p&gt;
&lt;p&gt;ex_2 := TO_BOOL(util.SysMem.SysMemCmp(pBuffer1:=ADR(Data3&lt;a class="alink" href="/forge/talk-ru/3/"&gt;[3]&lt;/a&gt;), pBuffer2:=ADR(Data4&lt;a class="alink" href="/forge/talk-ru/3/"&gt;[3]&lt;/a&gt;), udiCount:=4));&lt;br/&gt;
IF ex_2 AND enable THEN&lt;br/&gt;
    util.SysMem.SysMemCpy(pDest:=ADR(Data4&lt;a class="alink" href="/forge/talk-ru/3/"&gt;[3]&lt;/a&gt;), pSrc:=ADR(Data3&lt;a class="alink" href="/forge/talk-ru/3/"&gt;[3]&lt;/a&gt;), udiCount:=4);&lt;br/&gt;
END_IF&lt;/p&gt;
&lt;p&gt;// Attention udiCount input is intended in &amp;lt;byte&amp;gt; in the example 1 I pass to udiCount:=SIZEOF(Data1) for compare and copy the intere ARRAY and&lt;br/&gt;
// SIZEOF pass the size of intere ARRAY in byte to the function input, in this 2 examples I used variable type SINT each one occupie 1 byte and one position in the ARRAY&lt;br/&gt;
// and in the example 2 I pass udiCount:=4 for compare and copy only 4 position of Data3/4&lt;span&gt;[3..6]&lt;/span&gt; if you want to extend this example and use it for an ARRAY OF WORD&lt;br/&gt;
// remember that each WORD will occupe 2 byte (16 bit) and you will have to pass udiCount:=SIZEOF(Data1) if you need to compare intere ARRAY example 1&lt;br/&gt;
// but need udiCount:=(4&lt;em&gt;2) for the example 2 because need to compare and copy 4 word each occupie 2 byte (16 bit).&lt;br/&gt;
// For REAL (32 bit) need udiCount:=SIZEOF(4&lt;/em&gt;4) for LREAL or (64 bit) need udiCount:=SIZEOF(4&lt;em&gt;16)&lt;br/&gt;
// a good rule is calculate the dimension of the ARRAY in byte without empty space at the end, multiple of data type number for variable bigger then 8 bit&lt;br/&gt;
// Example : If you want to create an ARRAY for 5 REAL (32 bit) each occupie 4 byte the correct size is ARRAY&lt;span&gt;&lt;span&gt;[0..19]&lt;/span&gt;&lt;/span&gt; OF REAL 5&lt;/em&gt;4=20 position. &lt;br/&gt;
// NOTE : In the example the position of compare function util.SysMem.SysMemCmp is first and the copy function util.SysMem.SysMemCpy inside the IF is after&lt;br/&gt;
// in one cycle of the program the two array is compared and if there is some difference and enable are copied.&lt;br/&gt;
// If you move util.SysMem.SysMemCmp after the IF cycle he will copare the ARRAY in the current cycle but the copy of the value will do in the next cycle.&amp;lt;/byte&amp;gt;&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>