CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum by 3S-Smart Software Solutions GmbH | A member of the CODESYS Group
Deutsche Version English version russian version 
It is currently Thu Dec 13, 2018 6:31 pm

All times are UTC+01:00

Post new topic  Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Tue Jul 25, 2017 6:30 pm 

Joined: Fri Feb 24, 2017 10:59 pm
Posts: 2

I have some analog sensor threshold constants defined in my code. Even though the sensor value is delivered as a 16-bit integer, I normally like to define the thresholds using REAL data types, with SI units (example: 112.5, units of °F). This makes the code easier to read and modify.

However, in the code, I need the threshold converted to the 16-bit integer for the algorithm to use.

In C code, this was a pretty simple thing to do, as I could use a #define macro to perform the conversion, and as long as my coding style ensured that the value was "const/read-only", it would optimize and implicitly convert it to the 16-bit int (no need to perform operations with REAL data types at run-time, or even have a REAL data type stored in memory).

Is there something similar to this in structured text?

PostPosted: Sun Sep 24, 2017 6:26 pm 
Frequent User
Frequent User

Joined: Fri Sep 02, 2011 8:02 pm
Posts: 291
There is not an equivalent to the define of C language. The closest you can come is to define the conversion as a constant first (if you can) and then define your 16 bit constants as a REAL x conversion.

For example:

   TRIP_TEMP : INT := REAL_TO_INT((111.5 - 32) * 5 / 9);

But I would suggest just defining the real and making the conversion during code execution using a function. Unless you are running a low power embedded system, you wouldn't notice a problem. And even in an embedded system, you probably wouldn't notice anything as most have floating point chips anyway.

In my machine programs, I mostly convert all my variables to reals first! Fieldbus values - convert to reals every PLC scan... Then I can limit my overflow/underflow errors to a small area. Plus it's much easier to debug when I see DriveTemp = 57.3 (C) instead of 573.

Famous quote: "The real problem is that programmers have spent far too much time worrying about efficiency in the wrong places and at the wrong times; premature optimization is the root of all evil (or at least most of it) in programming." Donald Knuth

Scott Cunningham
KEB America, Inc.

Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 2 posts ] 

All times are UTC+01:00

Who is online

Users browsing this forum: No registered users and 3 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited