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 )
Convert BCD HEX to INT and back
Re: Convert BCD HEX to INT and back
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;
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 (114.48 KiB) Viewed 15864 times
Best Regards,
Beijer Electronics, Inc.
Skylar Walker | Applications Engineer
Beijer Electronics, Inc.
Skylar Walker | Applications Engineer
Re: Convert BCD HEX to INT and back
thanks Skylar, it works!