Welcome Guest ( Login | Register )



All times are UTC - 7 hours [ DST ]



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Convert BCD HEX to INT and back
PostPosted: Wed Feb 29, 2012 5:37 am 

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

Offline
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)


 Profile  
 
 Post subject: Re: Convert BCD HEX to INT and back
PostPosted: Wed Feb 29, 2012 1:07 pm 

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

Offline
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 4301 times ]

_________________
Best Regards,

Beijer Electronics, Inc.
Skylar Walker | Applications Engineer
 Profile  
 
 Post subject: Re: Convert BCD HEX to INT and back
PostPosted: Fri Mar 16, 2012 1:16 am 

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

Offline
thanks Skylar, it works!


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

All times are UTC - 7 hours [ DST ]


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:  
cron