<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to Convert REAL to 2 WORDS, modbus</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/029f5a8611/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/Engineering/thread/029f5a8611/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/Engineering/thread/029f5a8611/</id><updated>2020-02-27T13:57:15.538000Z</updated><subtitle>Recent posts to Convert REAL to 2 WORDS, modbus</subtitle><entry><title>Convert REAL to 2 WORDS, modbus</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/029f5a8611/?limit=25#a0c4" rel="alternate"/><published>2020-02-27T13:57:15.538000Z</published><updated>2020-02-27T13:57:15.538000Z</updated><author><name>Vuli</name><uri>https://forge.codesys.com/u/vuli/</uri></author><id>https://forge.codesys.com65356215870b9cae9a0e88a4535a181df6a9cef9</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hey &lt;br/&gt;
I was also dealing with Modbus and words only.&lt;/p&gt;
&lt;p&gt;Overlapping addresses is a general approach ... but you should let the system take care of addresses for you.&lt;/p&gt;
&lt;p&gt;Think in bytes :&lt;/p&gt;
&lt;p&gt;WORD   2 bytes&lt;br/&gt;
DWORD 4 bytes&lt;br/&gt;
REAL     4 bytes&lt;br/&gt;
LREAL   8 bytes&lt;/p&gt;
&lt;p&gt;REAL to 2 WORD or 4 BYTES:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nv"&gt;RealValue&lt;/span&gt; : &lt;span class="nv"&gt;REAL&lt;/span&gt;&lt;span class="c1"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;WordArray&lt;/span&gt; : &lt;span class="nv"&gt;ARRAY&lt;/span&gt;[&lt;span class="mi"&gt;0&lt;/span&gt;..&lt;span class="mi"&gt;1&lt;/span&gt;] &lt;span class="nv"&gt;OF&lt;/span&gt; &lt;span class="nv"&gt;WORD&lt;/span&gt;&lt;span class="c1"&gt;; (* array of 2 words *)&lt;/span&gt;
&lt;span class="nv"&gt;MEMCPY&lt;/span&gt;&lt;span class="ss"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;ADR&lt;/span&gt;&lt;span class="ss"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;WordArray&lt;/span&gt;[&lt;span class="mi"&gt;0&lt;/span&gt;]&lt;span class="ss"&gt;)&lt;/span&gt;, &lt;span class="nv"&gt;ADR&lt;/span&gt;&lt;span class="ss"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;RealValue&lt;/span&gt;&lt;span class="ss"&gt;)&lt;/span&gt;, &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="ss"&gt;)&lt;/span&gt;&lt;span class="c1"&gt;; (* where 4 is number of bytes for REAL *)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;LREAL to 4 WORD or 8 BYTES:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nv"&gt;RealValue&lt;/span&gt; : &lt;span class="nv"&gt;LREAL&lt;/span&gt;&lt;span class="c1"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;WordArray&lt;/span&gt; : &lt;span class="nv"&gt;ARRAY&lt;/span&gt;[&lt;span class="mi"&gt;0&lt;/span&gt;..&lt;span class="mi"&gt;3&lt;/span&gt;] &lt;span class="nv"&gt;OF&lt;/span&gt; &lt;span class="nv"&gt;WORD&lt;/span&gt;&lt;span class="c1"&gt;; (* array of 4 words *)&lt;/span&gt;
&lt;span class="nv"&gt;MEMCPY&lt;/span&gt;&lt;span class="ss"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;ADR&lt;/span&gt;&lt;span class="ss"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;WordArray&lt;/span&gt;[&lt;span class="mi"&gt;0&lt;/span&gt;]&lt;span class="ss"&gt;)&lt;/span&gt;, &lt;span class="nv"&gt;ADR&lt;/span&gt;&lt;span class="ss"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;RealValue&lt;/span&gt;&lt;span class="ss"&gt;)&lt;/span&gt;, &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="ss"&gt;)&lt;/span&gt;&lt;span class="c1"&gt;; (* where 8 is number of bytes for LREAL *)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;</summary></entry><entry><title>Convert REAL to 2 WORDS, modbus</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/029f5a8611/?limit=25#ff90" rel="alternate"/><published>2020-02-27T13:57:15.315000Z</published><updated>2020-02-27T13:57:15.315000Z</updated><author><name>t.lundahl</name><uri>https://forge.codesys.com/u/t-lundahl/</uri></author><id>https://forge.codesys.com3cb23440b0f3c4b8b74f36dcd0199f642d0fdf87</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Check in the manual for: UNION&lt;/p&gt;
&lt;p&gt;I think this was made for two variable types to share same memory space.&lt;/p&gt;
&lt;p&gt;/Torbjörn Lundahl&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Convert REAL to 2 WORDS, modbus</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/029f5a8611/?limit=25#91a8" rel="alternate"/><published>2020-02-27T13:57:14.615000Z</published><updated>2020-02-27T13:57:14.615000Z</updated><author><name>ndzied1</name><uri>https://forge.codesys.com/u/ndzied1/</uri></author><id>https://forge.codesys.com83cd0bd9d367772b6cf4aaba54a232e60d671b23</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;There is an open source library by Oscat that has the &lt;br/&gt;
REAL_TO_DW function which is written the same way as TimvH's example.&lt;/p&gt;
&lt;p&gt;There is a lot of good stuff in the library&lt;br/&gt;
&lt;a class="" href="http://www.oscat.de/" rel="nofollow"&gt;http://www.oscat.de/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Another way I have done this is with overlapping marker addresses.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nt"&gt;VAR&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nt"&gt;MyReal&lt;/span&gt; &lt;span class="nt"&gt;AT&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="nt"&gt;MW0&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;REAL&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nt"&gt;MyBytes&lt;/span&gt; &lt;span class="nt"&gt;AT&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="nt"&gt;MW0&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;ARRAY&lt;/span&gt; &lt;span class="cp"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="nx"&gt;..3&lt;/span&gt;&lt;span class="cp"&gt;]&lt;/span&gt; &lt;span class="nt"&gt;OF&lt;/span&gt; &lt;span class="nt"&gt;BYTE&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;END_VAR&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Because both variables above are declared at the same marker address, you can write to MyReal with a real value and then send the array MyBytes to the modbus function as an array of byte values.&lt;/p&gt;
&lt;p&gt;You must make sure that your slave will understand that the real value is packed into 4 bytes.  Also, I have had to use trial and error to determine which Word or Byte swapping the Modbus devices use.  You may need to change the order of the byte elements so that the slave will understand them.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Convert REAL to 2 WORDS, modbus</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/029f5a8611/?limit=25#47ca" rel="alternate"/><published>2020-02-27T13:57:14.387000Z</published><updated>2020-02-27T13:57:14.387000Z</updated><author><name>timvh</name><uri>https://forge.codesys.com/u/timvh/</uri></author><id>https://forge.codesys.com9dc7183fbd9e4e8809ddfcdc2fa7a291b0d56937</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;You could do it by using pointers. See below for an example to put the real value in a DWORD.&lt;br/&gt;
Then you can split your dword into two single words and put them on the bus.&lt;br/&gt;
I believe there is a library with a function which can do this more userfriendly, but I cannot find it that quickly. Maybe somebody else will respond with a better suggestion.&lt;/p&gt;
&lt;p&gt;VAR&lt;br/&gt;
    dwDword: DWORD;&lt;br/&gt;
    pDword: POINTER TO DWORD;&lt;br/&gt;
    rReal1: REAL;&lt;br/&gt;
    pReal: POINTER TO REAL;&lt;br/&gt;
    rReal2: REAL;&lt;br/&gt;
END_VAR&lt;/p&gt;
&lt;p&gt;pDword := ADR(rReal1);&lt;br/&gt;
dwDword := pDword^;&lt;/p&gt;
&lt;p&gt;(&lt;em&gt;&lt;br/&gt;
conversion back, just for testing&lt;br/&gt;
pReal := ADR(dwDword);&lt;br/&gt;
rReal2 := pReal^;&lt;br/&gt;
&lt;/em&gt;)&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Convert REAL to 2 WORDS, modbus</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/029f5a8611/?limit=25#82aa" rel="alternate"/><published>2020-02-27T13:57:14.084000Z</published><updated>2020-02-27T13:57:14.084000Z</updated><author><name>urbanlarsson</name><uri>https://forge.codesys.com/u/urbanlarsson/</uri></author><id>https://forge.codesys.comeafbe333d44cd0c60d07568f65ac47cbe84cebc5</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hi!&lt;/p&gt;
&lt;p&gt;I need tips on how to convert a REAL to 2 WORDS.&lt;/p&gt;
&lt;p&gt;I have some real variables I want to put into my modbus-slave-config, but it only supports Words, are there any built functions in codesys to do the convertion?&lt;/p&gt;
&lt;p&gt;/U&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>