Convert BCD HEX to INT and back

A forum devoted to the discussion of all topics having to do with scripting and other advanced programming using iX Developer.
Post Reply
RestaLG
Posts: 4
Joined: Mon Feb 13, 2012 2:33 am

Convert BCD HEX to INT and back

Post by RestaLG »

Hello,
i need convert date tag from PLC in BCD HEX format to the decimal INT format and back (INT to BCD HEX, for example:

from PLC tag: 0x1202 (= 4610 Int) i need

YEARtag: 12 Int
MONTHtag: 02 Int

(not YEARtag: 46 and MONTHtag: 10 )

I find some C# script, for example:
http://icodesnip.com/snippet/csharp/con ... into-bytes
but i can not implement this to the IX developer - script modules...

Thanks for any advice and sorry for my english :o)

Skylar
Posts: 42
Joined: Wed Jan 04, 2012 11:17 am

Re: Convert BCD HEX to INT and back

Post by Skylar »

Here is a script that I've used to convert a BCD input from the controller to a Decimal number. Once you have that, you could just split it up however you please. Attached is the screen shot of where to put this script.

uint binVal = 0;
int shift = 0;
uint multiplier = 1;

//Change "BCD" to the name of the tag
//recieving a BCD value from the controller
uint bcdVal = Globals.Tags.BCD.Value.UInt;

for (int i = 0; i < 4; i++)
{
uint digit = (bcdVal >> shift) & 0xf;

if (digit > 9)
{
binVal = 0;
break;
}
else
{
binVal += digit * multiplier;
shift += 4;
multiplier *= 10;
}
}

//Change "Decimal" to the name of the tag
//where you want to store the decimal value
Globals.Tags.Decimal.Value = binVal;
Attachments
tagBCD2DEC (1).png
tagBCD2DEC (1).png (114.48 KiB) Viewed 15864 times
Best Regards,

Beijer Electronics, Inc.
Skylar Walker | Applications Engineer

RestaLG
Posts: 4
Joined: Mon Feb 13, 2012 2:33 am

Re: Convert BCD HEX to INT and back

Post by RestaLG »

thanks Skylar, it works!

Post Reply