PDA

View Full Version : type conversion



b.koerts
03-26-2016, 05:48 AM
Hi all,

I am struggling with this. I want to multiply a INT (signed 16 bit) with an UINT (unsigned 16 bit), divide this by a CONSTANT and the result needs to go in a REAL.

This is what I tried amongst many others but can't get it to work:

r_nwv_weegplaat_1_kg := INT_TO_REAL (s16_weegplaat_1_netto * UINT_TO_INT(u16_weegplaat_1_omrekenfactor)) / 10000000;

I am using a cr0401 and a cr0451. The above line is in the cr0401 and the REAL r_nwv_weegplaat_1_kg is sent to the display. If I set this variable to 12.34 with a constant I see that the display shows 12.34 just as expected. If I let the calculation set the value in the REAL variable than the result is jumpy by large numbers and also the sign changes from positive to negative and back. This value comes from an analogue input connected to a potmeter for now. I guess it is a problem with overload at some point because the type conversion is wrong.

What am I doing wrong?

Bert

Jeremy Kopicz
04-04-2016, 09:31 AM
First, convert all values to REALS. Second, you may run into issues when transmitting a real value over the CAN network. My recommendation is to multiply it by the number of decimal places you want to see (x100 for x.xx) convert into a word or dword then divide on the diplay side to getthe proper formatting.

NLex
04-12-2016, 03:07 AM
What kind of problems can I expect when transmitting reals via network variables?

I'm doing this for years and experiencing no problems

Erik Gerrits
04-12-2016, 11:53 AM
What kind of problems can I expect when transmitting reals via network variables?

I'm doing this for years and experiencing no problems

The issue is the byte order may not be the same when dealing with REALs.

b.koerts
06-12-2016, 06:33 AM
Sorry for my late reply. I had to finish the software quickly so I did it by trial and error. I did break up the long line to several lines and analyzed what was going on until it all started to make sense and then went back to one line step by step. I liked to send only bytes or words to keep the data load low for the can bus to avoid other issues data rate, crossing network variable boundaries until I learn more about that. Thanks for your answers, especially about sending real in network variables.

Bert