Converting from Decmial to Hex in Ladder

dbh6

Lifetime Supporting Member
Join Date
Jan 2013
Location
Central, NJ
Posts
552
Hello Once again,

Anyone have a method of converting a decimal Value in Decimal to Hexadecimal in ladder logic, PLC i'm using is Logix 5000.
 
You don't need to 'convert' anything; the data is stored in either integer or floating point encoding.

You can change the display of that data in the database view by selecting Hexadecimal radix.

In the instruction view, you can specify the display method with a prefix.

2#xxxx means Binary.
16#xxxx means Hexadecimal.

What exactly do you want to see or do with the data value ?
 
Ken its just for "knowing how to do" purposes, but you mentioned that you can change the display in of the data base view by selecting hexadecimal radix, where do you change that??? also in instruction view you stated that you can display prefixes as you stated, i have a simple mov instruction that's moving a some decimal value (DINT) to a destination thats also a DINT, so i wanted to be able to view the destination tag as a Hexadecimal, so while the PLC was running i out the prefix 16# to whatever decimal value was being moved to the destination tag but nothing happen.

Ultimately i have fault codes in Hex coming from a yaskawa vfd, the customer wants the hex fault code displayed on the HMI Ex. Say the fault code in Hex was 16#9000 i want to display in decimal 9000 so it can be displayed on the HMI because the HMI might not accept Hex values and thats the way they want it done, so whenever they get a hex value, that value will be displayed in decimal then they would have to look up in the manual what that fault code means. So let me know what you think.
 
@passwordg thanks, @ bernie, i thought of that as well, fortunately the fault codes in the manual do not contain A-F rather each fault code is a 4 digit hex value and the highest number that a single digit goes up to is 9 Ex. 8321, 9000, 4200 etc, so once again, i just need a way to display that exact 4 digit hex value to the same value but in decimal format so 16#8321 = 8321 decimal, 16#9000 = 9000 decimal etc,
 
As others have alluded to, a number is stored in the computer in binary form. No matter what radix you choose to display the number, in the computer it is still in binary form.

Now lets talk about how to display a number. When a number is displayed no matter what radix is chosen, a computer routine will convert the number to a string of characters to be displayed on the screen. For our purposes we'll skip how the characters are converted to pixels and fonts. Suffice it to say, if we pick a binary radix then the number is converted to a string containing the characters 0 and 1. If we pick an octal radix its a string containing the characters 01234567. Decimal radix is a string containing the characters 0123456789, and a hex radix is a string containing the characters 0123456789ABCEDF. With that basic understanding you should be able to start to see how to do what you are asking.

If your HMI can support a hex radix then just use it. Let the software in the HMI take care of what you need. If however it does not support a hex radix then you can convert the number to a string and display the string. You can do this in either the PLC or the HMI if is supports scripting. Binary to Hexadecimal_String conversion happens to be really easy, much easier than decimal string conversion.

First define either a string HexCharString or an ascii array containing "0123456789ABCEDF".

Next define a four element unsigned integer or double integer array. We are going to break our number into four 4bit nibbles.
NibbleArray[0] = FaultCode AND 000Fh first four bits
NibbleArray[1] = (FaultCode AND 00F0h)/16 second four, shifted right
NibbleArray[2] = FaultCode AND 0F00h)/256 third four, shifted right
NibbleArray[3] = (FaultCode AND F000h)/4096 last four, shifted right

(Depending on your platform you might have to compensate for signed division at the last element if you are using a signed integer array.)

Now each nibble is a number 0 to 15, and you can simply use this number to select a character from the string we assigned first.
FaultCodeString[0] = HexCharString[NibbleArray[3]]
FaultCodeString[1] = HexCharString[NibbleArray[2]]
FaultCodeString[2] = HexCharString[NibbleArray[1]]
FaultCodeString[3] = HexCharString[NibbleArray[0]]

Now you have a string representing the fault code value formatted as a hex number.

This is basically what happens behind the scenes whether in your PC or your HMI or even your smartphone to display a number in any radix.
 
Last edited:
@ Tconnolly thanks for the detailed explanation i'll try that out, @bernie, im not sure, but from what i understand it cannot display Hex.
 
Hi dbh6,
Just out of interest, how many fault codes can be output from the VFD? If there are only a small amount it might be as easy to create a small list of faults in the HMI. You could compare the decimal value in the PLC and set a fault or message bit to the HMI depending on result of the comparison.

Regards,
Donnacha
 
Try the following line pasted into the mnemonic entry area of an empty rung.

BST AND SourceDINT 16#f000 Temp1 NXB CPT Target "(Temp1 * 1000) / 4096" NXB AND SourceDINT 16#f00 Temp1 NXB CPT Target "Target + ((Temp1 * 100) / 256)" NXB AND SourceDINT 16#f0 Temp1 NXB CPT Target "Target + ((Temp1 * 10) / 16)" NXB AND SourceDINT 16#f Temp1 NXB CPT Target "Target + Temp1" BND

Or refer to the attached PDF
 
Here is an AOI I threw together and tested that converts an integer to a string of four HEX characters. Save the text file with an .L5X extension and import it as a Logix5000 AOI. It would be easy to expand it to convert a DINT with an 8 character string. It will also work as is for a BCD value to convert it to a string.

The AOI uses an internal SINT array HexChars[16] containing the ascii characters '0' through 'F'.
Starting with the most significant four bits of the source, BTD extracts four bits at a time to a tag named Nibble. Nibble is used to move the matching character from the HexChars array to the first character of the destination string. Then it repeats on the second four bits and second character and so on. Last the destination string length is set to 4.

BTD Source 12 Nibble 0 4 MOV HexChars[Nibble] Destination.DATA[0]
BTD Source 8 Nibble 0 4 MOV HexChars[Nibble] Destination.DATA[1]
BTD Source 4 Nibble 0 4 MOV HexChars[Nibble] Destination.DATA[2]
BTD Source 0 Nibble 0 4 MOV HexChars[Nibble] Destination.DATA[3]
MOV 4 Destination.Len


AB has technote 26796 that has a different way using two FOR/NEXT loops with 16 comparisons and DTOS instructions. IMO its a rather clunky way to do it but it might be worth it to you to take a look if you have a tech connect contract.
 
Last edited:
Hi dbh6,
Just out of interest, how many fault codes can be output from the VFD? If there are only a small amount it might be as easy to create a small list of faults in the HMI. You could compare the decimal value in the PLC and set a fault or message bit to the HMI depending on result of the comparison.

Regards,
Donnacha

That is actually a really good idea - especially if the HMI supports multi-state or multi-message indicators. Instead of displaying a fault code you can display a descriptive fault message.

You're obviously not using a Crimson3 compatible HMI because those support hex, but in Crimson I would configure an integer tag as multi-state, give it 44 states, and enter an actual text message for each fault code. If your HMI can do that then it would improve the end product.
 
Ken Roach said:
What exactly do you want to see or do with the data value ?

dbh6 said:
Ken its just for "knowing how to do" purposes...value will be displayed in decimal then they would have to look up in the manual what that fault code means.

Donnacha said:
If there are only a small amount it might be as easy to create a small list of faults in the HMI

dbh6 said:
i was thinking of doind that but there are over 44 fault codes

Unless the HMI is very limited in screen size or number of messages it can display, etc, I would have thought this would be the natural way to approach this? HMI allowing, 44+ messages/alarms assigned to the VFD HEX fault codes, which you already have DINT tags for, displaying the exact or abbreviated versions of the manuals description of the faults. This would save time looking up the manual for the fault code description when in a crisis? Again, all Dependant on the HMIs capabilities and/or your access to it for this job.

I display PowerFlex 4 Fault Codes on HMIs. All 24 of them, all written exactly as you would read them from the manual. If there was 50, I'd still do it for what you gain when a drive faults. The operators can read the message and reset if possible or report exactly what's wrong. You can also log the message/alarm on the HMI in layman's terms instead of a numeric value.

What they want doesn't make sense to me? Why go so far and stop so short? I'm sure there is a good reason for it, but what is it?

Sorry my curiosity has gotten the better of me o_O

By the way, I'm not knocking anyone's suggestions as to how to convert this, excellent work, as this provides the "knowing how to do" request. It's just starting to look over complicated to me for what could be easier to do with less code and yield a more verbose result? I know there's something I'm missing here? :oops:

EDIT: Ah good TC! I see I wasn't mad after all, you've seen Donnacha's reply. It tweaked my interest at that point too.
G.
 

Similar Topics

Hello everyone, can anyone help me with covert the STL code to ladder. Iam using plc s71200. A %DB1.DBX33.7 // angel of vaccum...
Replies
2
Views
203
Hello PLCs Forum, I am in a bit of a pickle and was hoping someone could offer me some help. I have a .rss file and just need to see the ladder...
Replies
2
Views
120
Hello nice to meet you, im new in here, I'm currently trying to convert code written in STL for a S7-400 to SCL for an S7-1500, because when i run...
Replies
5
Views
314
Hello, did anybody know, if there exist an converting cable like the1492-CM1746-M01 (for an 1746-IB16 to an 5069-IB16), for an 1746-HSCE to an...
Replies
3
Views
385
Hello, This will be my first time converting powerflex 40's and 400's from devicenet to ethernet. I did some research, and it seems I will need...
Replies
4
Views
734
Back
Top Bottom