# BCD Converter

**URL:** <https://forum.inductiveautomation.com/t/bcd-converter/8279>\
**Category:** 3rd Party Modules\
**Created:** [April 8, 2014, 10:30pm UTC](https://forum.inductiveautomation.com/t/bcd-converter/8279 "2014-04-08T22:30:42Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![downerczx](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/downerczx/32/11649_2.png) [@downerczx](https://forum.inductiveautomation.com/u/downerczx)\
**Post date:** [April 8, 2014, 10:30pm UTC](https://forum.inductiveautomation.com/t/bcd-converter/8279/1 "2014-04-08T22:30:42Z")

</div>

Hello,

I’ve written some expression code to scale Omron BCD values over OPC to regular values in Ignition since Ignition does not natively support BCD. I’ve also written some python script to allow the user to write a value back to a BCD point.

Is there a way that I can use the SDK to take out some of the layers? I currently have to have a “raw values” tag, a “scaled values” tag, etc. Could I somehow put it under “scaling” with a plugin via SDK? Is there some other way I could do this?

thanks

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 8, 2014, 11:09pm UTC](https://forum.inductiveautomation.com/t/bcd-converter/8279/2 "2014-04-08T23:09:31Z")

</div>

Are you sure you can’t just change the data type of the tag in Kepware to ‘Short’ or something like that?

---

<div class="post-metadata">

**Author:** ![downerczx](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/downerczx/32/11649_2.png) [@downerczx](https://forum.inductiveautomation.com/u/downerczx)\
**Post date:** [April 9, 2014, 2:37pm UTC](https://forum.inductiveautomation.com/t/bcd-converter/8279/3 "2014-04-09T14:37:44Z")

</div>

Unfortunately, no, kepserver won’t cast types. It still serves the data to Ignition as BCD, which Ignition doesn’t support.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 9, 2014, 3:33pm UTC](https://forum.inductiveautomation.com/t/bcd-converter/8279/4 "2014-04-09T15:33:52Z")

</div>

Well the thing is that OPC doesn’t support BCD either. It’s simply not a valid datatype. I’m surprised Kepware is serving up an incorrect value for a tag it must be claiming is an Int16/UInt16 over its OPC interface.

I’m pretty sure we’ve seen other people dealing with BCD encoded value sources before… I wonder how they’re dealing with it.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 9, 2014, 5:45pm UTC](https://forum.inductiveautomation.com/t/bcd-converter/8279/5 "2014-04-09T17:45:48Z")

</div>

Is the Data Type in Kepware set to BCD?

EDIT: Here’s a pic to illustrate.

---

<div class="post-metadata">

**Author:** ![downerczx](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/downerczx/32/11649_2.png) [@downerczx](https://forum.inductiveautomation.com/u/downerczx)\
**Post date:** [April 11, 2014, 5:04pm UTC](https://forum.inductiveautomation.com/t/bcd-converter/8279/6 "2014-04-11T17:04:31Z")

</div>

Observe how the Kepware browser correctly interprets the BCD value, but the Ignition OPC quick browser is unable to interpret the BCD as anything but a strange number:

---

<div class="post-metadata">

**Author:** ![adamaustin](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@adamaustin](https://forum.inductiveautomation.com/u/adamaustin)\
**Post date:** [April 11, 2014, 6:14pm UTC](https://forum.inductiveautomation.com/t/bcd-converter/8279/7 "2014-04-11T18:14:10Z")

</div>

You could use this:

```auto
def BCDtoDEC(x):
	result = ""
	o = "%o" % x
	neg = (o[0] == "-")
	for i in range(neg, len(o)):
		if (4 & int(o[i])):
			result += "1"
		else:
			result += "0"
		if (2 & int(o[i])):
			result += "1"
		else:
			result += "0"
		if (1 & int(o[i])):
			result += "1"
		else:
			result += "0"
	result = ("0" * (4 - (len(result) % 4))) + result
	finalresult = ""
	for i in range(0, len(result), 4):
		finalresult += str(int(result[i:i+4],2))
	return int(finalresult) * (1 - 2 * neg)
```

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 11, 2014, 6:53pm UTC](https://forum.inductiveautomation.com/t/bcd-converter/8279/8 "2014-04-11T18:53:54Z")

</div>

This looks like a bug in Kepware to me. It seems that you get a different value for the tag depending whether you’re accessing Kepware over OPC-DA or OPC-UA.

Try using the Ignition’s COM module, making a DA connection to Kepware, and see if the value is what you’re expecting when coming from the DA connection.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 11, 2014, 6:57pm UTC](https://forum.inductiveautomation.com/t/bcd-converter/8279/9 "2014-04-11T18:57:06Z")

</div>

> [@downerczx](#):
>
> Observe how the Kepware browser correctly interprets the BCD value, but the Ignition OPC quick browser is unable to interpret the BCD as anything but a strange number:

Don't know what to say, as we've been doing it for a couple of years. Just tried it out on a fresh install:

EDIT: Dang! Kevin Beat me to it.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 11, 2014, 7:12pm UTC](https://forum.inductiveautomation.com/t/bcd-converter/8279/10 "2014-04-11T19:12:13Z")

</div>

[quote=“adamaustin”]You could use this:

`
def BCDtoDEC(x):
	result = ""
	o = "%o" % x
	neg = (o[0] == "-")
	for i in range(neg, len(o)):
		if (4 & int(o[i])):
			result += "1"
		else:
			result += "0"
		if (2 & int(o[i])):
			result += "1"
		else:
			result += "0"
		if (1 & int(o[i])):
			result += "1"
		else:
			result += "0"
	result = ("0" * (len(result) % 4)) + result
	finalresult = ""
	for i in range(0, len(result), 4):
		finalresult += str(int(result[i:i+4],2))
	return int(finalresult) * (1 - 2 * neg)
`[/quote]  
He’s getting the decimal representation of a BCD Number. Since BCD is really just hex without A-F, you just need to convert to hex to get it back-- which makes for a lot shorter code…

`def dec2BCD(decNumIn):
   return int(str(hex(decNumIn))[2:])`

---

<div class="post-metadata">

**Author:** ![adamaustin](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@adamaustin](https://forum.inductiveautomation.com/u/adamaustin)\
**Post date:** [April 11, 2014, 7:18pm UTC](https://forum.inductiveautomation.com/t/bcd-converter/8279/11 "2014-04-11T19:18:26Z")

</div>

[quote=“JordanCClark”]…you just need to convert to hex to get it back-- which makes for a lot shorter code…

`def dec2BCD(decNumIn):
   return int(str(hex(decNumIn))[2:])`[/quote]

Awesome!

I kept trying to use [bin](https://docs.python.org/2/library/functions.html#bin) but it just wouldn’t work.

---

<div class="post-metadata">

**Author:** ![Pat](https://avatars.discourse-cdn.com/v4/letter/p/8491ac/32.png) [@Pat](https://forum.inductiveautomation.com/u/Pat)\
**Post date:** [April 11, 2014, 8:13pm UTC](https://forum.inductiveautomation.com/t/bcd-converter/8279/12 "2014-04-11T20:13:10Z")

</div>

> [@downerczx](#):
>
> Observe how the Kepware browser correctly interprets the BCD value, but the Ignition OPC quick browser is unable to interpret the BCD as anything but a strange number:

That's because Kepware is returning the value as BCD and Ignition is interpreting that value as an Int or Float depending on how you created the SQLTag.

If you go into Kepware and turn on Scaling for your tag, you can scale the tag from BCD to Int or Float and then Ignition should read the value correctly.

If you want to access the memory location directly through Kepware then you would add @BCD or @LBCD to the end of the memory location - D11002@BCD for 16bit or D11002@LBCD for 32bit.

---

<div class="post-metadata">

**Author:** ![downerczx](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/downerczx/32/11649_2.png) [@downerczx](https://forum.inductiveautomation.com/u/downerczx)\
**Post date:** [April 11, 2014, 10:30pm UTC](https://forum.inductiveautomation.com/t/bcd-converter/8279/13 "2014-04-11T22:30:36Z")

</div>

Using DA worked, but requires I buy the DA module. I don’t believe you can scale tags in Kepserver without “Advanced tags” plugin.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 11, 2014, 10:39pm UTC](https://forum.inductiveautomation.com/t/bcd-converter/8279/14 "2014-04-11T22:39:41Z")

</div>

Unless you go the expression tag route, I have to assume buying the COM module for Ignition is your cheapest option.

---

<div class="post-metadata">

**Author:** ![Pat](https://avatars.discourse-cdn.com/v4/letter/p/8491ac/32.png) [@Pat](https://forum.inductiveautomation.com/u/Pat)\
**Post date:** [April 12, 2014, 4:35pm UTC](https://forum.inductiveautomation.com/t/bcd-converter/8279/15 "2014-04-12T16:35:25Z")

</div>

There isn’t a Scaling tab under Tag Properties in your version of KepServer? It’s there in AutomationDirect’s version of KepServer, this is where you can have KepServer convert the BCD value to Int or Float.

[kepware.com/Products/feature-datascaling.asp](http://www.kepware.com/Products/feature-datascaling.asp)

---

<div class="post-metadata">

**Author:** ![downerczx](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/downerczx/32/11649_2.png) [@downerczx](https://forum.inductiveautomation.com/u/downerczx)\
**Post date:** [April 18, 2014, 10:30pm UTC](https://forum.inductiveautomation.com/t/bcd-converter/8279/16 "2014-04-18T22:30:13Z")

</div>

It turns out the issue had to do with not properly addressing the PLC points with a letter at the end that provided the casting.
