# Modbus TCP swap bytes

**URL:** <https://forum.inductiveautomation.com/t/modbus-tcp-swap-bytes/25009>\
**Category:** Ignition\
**Created:** [May 15, 2019, 2:49am UTC](https://forum.inductiveautomation.com/t/modbus-tcp-swap-bytes/25009 "2019-05-15T02:49:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![abishur](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/abishur/32/9096_2.png) [@abishur](https://forum.inductiveautomation.com/u/abishur)\
**Post date:** [May 15, 2019, 2:49am UTC](https://forum.inductiveautomation.com/t/modbus-tcp-swap-bytes/25009/1 "2019-05-15T02:49:28Z")

</div>

I’m using Ignition 7.9.9 and I have a Modbus TCP device that holds a 16 bit INT in HR2. I know that the value in HR2 should be -32768 or 0000000000000001 in binary. However in Ignition I’m getting 128 or 0000000100000000 in binary.

Is there a version of the Modbus TCP driver that allows you to swap High/Low bytes in 16 bit integers the same way you can swap High/Low words in 32 bit integers?

If not, I could make an expression tag that uses binEnc(getBit{[default]Tagpath},8),getBit(Tagpath,9…)) and so on and manually place each bit in the right spot, but I’m really hoping there’s a built in feature in the driver that I’m just missing.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [May 15, 2019, 11:28am UTC](https://forum.inductiveautomation.com/t/modbus-tcp-swap-bytes/25009/2 "2019-05-15T11:28:36Z")

</div>

Modbus protocols leave a lot to the implementer, but not that. (ps, you threw me off showing your binary backwards…)

---

<div class="post-metadata">

**Author:** ![abishur](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/abishur/32/9096_2.png) [@abishur](https://forum.inductiveautomation.com/u/abishur)\
**Post date:** [May 15, 2019, 1:06pm UTC](https://forum.inductiveautomation.com/t/modbus-tcp-swap-bytes/25009/3 "2019-05-15T13:06:15Z")

</div>

The devices are some scales that offer several ways of sending the data via Modbus (standard, byte swap, word swap, and double word swap), but unfortunately to be compatible with another device they have to use this byte swapping option. I’ll have to jump over to the feature request site and see if we can’t get some of the odder Modbus options thrown in there as I run into this kind of thing frequently on fringe devices, like power quality meters or scales.

Ha ha, yeah I threw myself off for a minute after posting the binary in little endian, I spend too much time in Allen-Bradley PLCs. Fortunately after I made a test tag I found that the binEnc function in Ignition also packs them little endian.

---

<div class="post-metadata">

**Author:** ![abishur](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/abishur/32/9096_2.png) [@abishur](https://forum.inductiveautomation.com/u/abishur)\
**Post date:** [May 15, 2019, 1:08pm UTC](https://forum.inductiveautomation.com/t/modbus-tcp-swap-bytes/25009/4 "2019-05-15T13:08:57Z")

</div>

For anyone else who comes across this thread having the same issue here was my work around. I made an OPC tag that looked at the appropriate Modbus address and got the data jumbled. Then I made an expression tag that used the following code to manually perform the byte swap:

```auto
binEnc(
getBit({[default]TagPath},8)
,getBit({[default]TagPath},9)
,getBit({[default]TagPath},10)
,getBit({[default]TagPath},11)
,getBit({[default]TagPath},12)
,getBit({[default]TagPath},13)
,getBit({[default]TagPath},14)
,getBit({[default]TagPath},15)
,getBit({[default]TagPath},0)
,getBit({[default]TagPath},1)
,getBit({[default]TagPath},2)
,getBit({[default]TagPath},3)
,getBit({[default]TagPath},4)
,getBit({[default]TagPath},5)
,getBit({[default]TagPath},6)
,getBit({[default]TagPath},7)
)

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [May 15, 2019, 1:14pm UTC](https://forum.inductiveautomation.com/t/modbus-tcp-swap-bytes/25009/5 "2019-05-15T13:14:31Z")

</div>

This would probably run faster:

```auto
(({[default]TagPath} & 0xff00) >> 8) | (({[default]TagPath} & 0xff) << 8)

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [May 15, 2019, 1:16pm UTC](https://forum.inductiveautomation.com/t/modbus-tcp-swap-bytes/25009/6 "2019-05-15T13:16:15Z")

</div>

> [@abishur](#):
>
> but unfortunately to be compatible with another device they have to use this byte swapping option.

Sounds like a device whose functionality needs to be replaced (with Ignition, perhaps). That's the kind of grief that makes systems unmaintainable.

---

<div class="post-metadata">

**Author:** ![abishur](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/abishur/32/9096_2.png) [@abishur](https://forum.inductiveautomation.com/u/abishur)\
**Post date:** [May 15, 2019, 1:28pm UTC](https://forum.inductiveautomation.com/t/modbus-tcp-swap-bytes/25009/7 "2019-05-15T13:28:15Z")

</div>

That’s the ticket! I went in thinking about shifting and then immediately became distracted. Thanks, Phil!

---

<div class="post-metadata">

**Author:** ![pphillips](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@pphillips](https://forum.inductiveautomation.com/u/pphillips)\
**Post date:** [October 6, 2020, 10:21pm UTC](https://forum.inductiveautomation.com/t/modbus-tcp-swap-bytes/25009/8 "2020-10-06T22:21:05Z")

</div>

> [@Solved by pturmel in post #5](#):
>
> & 0xff) \<\< 8)

Thanks to you both for this. Ran into the same issue with WEG Soft Starters. You would think they would know better... 😑

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [October 7, 2020, 12:46am UTC](https://forum.inductiveautomation.com/t/modbus-tcp-swap-bytes/25009/9 "2020-10-07T00:46:14Z")

</div>

Now you’re gonna make me put this into my new Modbus driver. (Client & Server & a few other goodies.) I had talked myself out of it a couple weeks ago. /:

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 8, 2020, 9:02pm UTC](https://forum.inductiveautomation.com/t/modbus-tcp-swap-bytes/25009/10 "2020-12-08T21:02:06Z")

</div>

Sorry to disappoint, but I decided to _not_ implement byte swapping for integers. There's many things in the Modbus spec that have been left to implementers, but this one wasn't. Sorry. Lots of other goodies, there, though:

> [@Automation Professionals' Advanced Modbus Driver](http://forum.inductiveautomation.com/t/automation-professionals-advanced-modbus-driver/41312):
>
> Automation Professionals is pleased to announce the first beta for its Advanced Modbus Driver, for Ignition version 7.9. Key features: Server mode, emulating one or more slave [devices](https://www.automation-pros.com/modbus/UserManual.pdf), using any of the three typical connection types, including simultaneous connections of multiple types. Client mode, incorporating all three connection modes in one driver. Support for Modbus Function Codes 20 and 21, File Records. Configure protocol tweaks for troublesome devices per slave unit, enabling suppor…
