CoDeSys data type conversion; intermediate tags ?

Ken Roach

Lifetime Supporting Member + Moderator
Join Date
Apr 2002
Location
Seattle, WA
Posts
17,443
This is a sort of beginner question, but I'd appreciate some input.

I'm a longtime Rockwell user, and one of the convenient things about the A-B instruction set is that many of the instructions automatically convert datatypes.

You have to be careful about rounding, and wary of precision, but for simple math it's convenient.

I'm working in CoDeSys v3.5 (Bosch-Rexroth IndraWorks, specifically) and am wondering if there's a simpler way to do what I want.

All I'm doing is multiplying a REAL value between 0.0 and 10.0 so that the output is an integer between 0 and 30,000 (the command for an analog output module).

I think I have to perform a REAL_to_INT, and then use an intermediate INT type tag to get the data into my MUL instruction.

Is there a way I'm not aware of to tie the output of the REAL_to_INT to the argument of the MUL instruction without the intermediate tag ?

Codesys_realtoint.jpg
 
It won't let you just drag the output from the REAL TO INT block to the input on the MUL?

I just tried it on my PacDrive3 project and I can do it there.. Though i'm not sure on the CodeSys version differences.
 
Once looking at it and opening my eyes, I realize that converting before multiplying would make rounding a problem. I've had that happen a bunch of times, when writing out my problem for posting made a solution clear.

But the question remains: is there a way of tying together the output argument and the input argument to two CoDeSys instructions in Relay Ladder Logic without using an intermediate variable ?

Real_to_Int2_PLCTalk.jpg
 
There doesn't seem to be any sort of handle or function for graphically linking or "dragging together" those two elements in IndraWorks.

Thanks for the input on PacDrive3; I have an upcoming project that I'm taking over where I'll have to learn that.
 
No, still seems to work, must be a difference in the editors.

If you try it again, can you right click on the output pin and create your empty box for the MUL instruction from there?
 
Use a MUL function without the EN input (insert empty box rather than drag one in from the toolbox)
Why? No idea, but you can hang a REAL_TO_INT on the input pins if you do.
Pp
 
Thank you ! That's an interesting difference between the two methods for inserting that function into an RLL rung.

Unfortunately the REAL_TO_INT then seems to lack a direct reference to a tag on its output. If I add a MOVE, I can get the result into the output tag.

So the follow-on question is "when a pin in CoDeSys RLL has no tag output, can you add one or do you always need a MOVE ?

Something new every day. Thanks !
 
Last edited:
I can relate to this too Ken. Having programmed AB my whole career and then swap to B&R controls was like the training wheels broke off my bike. Every compile attempt was said with “No Whammy’s!!”, only to end in red text that said “Nuh uh uh, you didn’t say the magic word.”.

One thing I would advise when working with software that requires you to say “please” when going from one data type to the other, is to never change data types unless necessary. Have a look at your HMI and I would wager that it has the ability to scale a value to another for readability on the HMI or at least allow implicit decimal places.

This allows everything in the plc to be one data type (to some extent).

Edit: In B&R world, units of measurement can be created to convert or scale the PLC variable to whatever. Even adding decimal places. The unit of measurement is then selected for the variable in the data source. Just saying, maybe Rexroth has the same l.
 
Last edited:
Hi Ken. As you know I also grew up in AB land with auto casting of data types. The rest of the world has been doing the X_to_Y conversion forever and it's going to stay that way. If you do a lot of this then just build a function to do what you need to do. You can easily nest functions like this:

MyInt:INT;
MyReal:REAL;

MyReal := WORD_TO_REAL(INT_TO_WORD(MyInt));

Now of course there was no reason to go through the WORD type in my example but it's just to make the point.

If you want to connect inputs to outputs then use CFC instead of Ladder You can also use embedded ST in your ladder using the EXECUTE block.

Good luck!

Yosi
 
One method to implement this is to using functions:

1:Create new pou and when "add pou" dialog opens choose type: Function, Return type : INT and implementation language: ST. Don't forget to give name to your function. In this example i used name f_Example.

Dialog.PNG

2:Create input tag to your function as follows

VAR_INPUT
IN :REAL; (*Input value in real type*)
END_VAR


3:Make code to your function as follows

f_Example:= REAL_TO_INT (IN *3000);
Function.PNG

4:Now, if you still want to use ladder logic to make your program then insert "empty box" to your ladder logic and name your box as "f_Example".
Ladder logic.jpg

This example makes the same thing you descripted above but it looks little simpler in your code
 
Last edited:
Hi,

In FBD language you could do it as shown in the picture. Also, to get a more precise output multiply first with 3000 and then convert REAL to INT. As depicted in the picture.

ST will be w1 := REAL_TO_INT(3000 * r1);

Hope this helps.

Thanks.

FBD.jpg
 
Aha ! ParrafinPower's comment is what I need to connect these blocks to an output tag; the "Assignment" object from the Toolbox.

In Indraworks, right-clicking on the output of an instruction box brings up a menu that includes most of the same objects (box, branch, contact, coil) that are in the General category of the Toolbox.

But it doesn't include the Tag Assignment operator. To get that you actually have to open the Toolbox and drag it over.

So there were two RLL editor things I needed to know: insert an instruction Box without EN/ENO pins to allow the conversion instruction and the math instruction to connect directly to one another, and then insert an Assignment object at the end of the rung.

Thank you to the PLCTalk community for helping me understand these simple RLL editor techniques !
 
Also, with regard to Yosi's mention of the EXECUTE block: that's my new favorite feature.

In IndraWorks there's an issue where the EXECUTE dialog box won't show onscreen if you're not at the appropriate screen resolution, so I have to manually reset my screen to a different ratio, so that made me grumpy.

But the ability to throw a bit of ST code into an RLL diagram is very useful.

The Rockwell world has its "Compute" instruction, but EXECUTE is a great way to do something (my favorite is the CASE_OF instruction) that would take a screen worth of RLL networks and condense them into a readable bit of ST.
 

Similar Topics

I'm using IndraWorks LOGIC 11V04.0022, trying to write a multiple move FB in ST. The editor is CoDeSys based, 3.x if I'm not mistaken...
Replies
3
Views
3,389
Hello. I have been using CODESYS for years and had taken for granted that the PLC part was very similar to TwinCAT. but just found one issue that...
Replies
1
Views
1,950
Does anyone in the community have some experience to help a novice understand how to use the Configuration Assembly data for an Adapter device...
Replies
16
Views
7,758
Hi all, I need help on how to create a data logger using Codesys. I am using Overdigit PLC and I need to create a data log and save it to csv...
Replies
15
Views
11,420
Basically looking for a scatter plot, looking at the trace feature seems all the x-axis data is time-series only. I'd to display speed vs flow, so...
Replies
2
Views
1,884
Back
Top Bottom