# Having trouble writing to bits in Schneider M258 PLC through Modbus TCP. Error :Bad

**URL:** <https://forum.inductiveautomation.com/t/having-trouble-writing-to-bits-in-schneider-m258-plc-through-modbus-tcp-error-bad/49642>\
**Category:** Ignition\
**Created:** [August 6, 2021, 1:15pm UTC](https://forum.inductiveautomation.com/t/having-trouble-writing-to-bits-in-schneider-m258-plc-through-modbus-tcp-error-bad/49642 "2021-08-06T13:15:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![RVSG\_GC](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@RVSG\_GC](https://forum.inductiveautomation.com/u/RVSG_GC)\
**Post date:** [August 6, 2021, 1:15pm UTC](https://forum.inductiveautomation.com/t/having-trouble-writing-to-bits-in-schneider-m258-plc-through-modbus-tcp-error-bad/49642/1 "2021-08-06T13:15:51Z")

</div>

My client has given me an exchange table with this M258 PLC. The connection is Modbus TCP.

I am able to read bits, integers and strings from addresses that are meant for me to read. I have selected Zero Based Addressing and Reverse String Byte Order in the Modbus TCP driver configuration.

However, I am unable to write to bits of other addresses that they have specified for incoming communication. The error shown when I try to change the Boolean bit to high is:

Error writing to TestTag1: Bad

My OPC item path is [M258PLC]HR503.0 and I changed the bit high by clicking the checkbox in the tag browser.

---

<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:** [August 6, 2021, 1:18pm UTC](https://forum.inductiveautomation.com/t/having-trouble-writing-to-bits-in-schneider-m258-plc-through-modbus-tcp-error-bad/49642/2 "2021-08-06T13:18:20Z")

</div>

Is there an error in the gateway logs when you attempt this?

Unfortunately it’s common for Modbus devices to not support the Mask Write Register function, which is required to safely write to bits within a register.

---

<div class="post-metadata">

**Author:** ![RVSG\_GC](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@RVSG\_GC](https://forum.inductiveautomation.com/u/RVSG_GC)\
**Post date:** [August 6, 2021, 1:24pm UTC](https://forum.inductiveautomation.com/t/having-trouble-writing-to-bits-in-schneider-m258-plc-through-modbus-tcp-error-bad/49642/3 "2021-08-06T13:24:16Z")

</div>

Yes, i think it is the mask write register function.

MaskWriteRegisterRequest Received response with ExceptionCode: 0x01 (IllegalFunction).

Im getting a lot of log messages , hopefully this is the error caused by the write.

---

<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:** [August 6, 2021, 1:25pm UTC](https://forum.inductiveautomation.com/t/having-trouble-writing-to-bits-in-schneider-m258-plc-through-modbus-tcp-error-bad/49642/4 "2021-08-06T13:25:45Z")

</div>

Yes, this is caused by the write and it’s the PLC telling you it doesn’t support that function.

You’re going to have to resort to doing read/modify/write via scripting, or if you’re using Ignition 8 it might be possible with derived tags, in order to new write register values with whatever bits you’re interested in modifying.

---

<div class="post-metadata">

**Author:** ![RVSG\_GC](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@RVSG\_GC](https://forum.inductiveautomation.com/u/RVSG_GC)\
**Post date:** [August 6, 2021, 1:26pm UTC](https://forum.inductiveautomation.com/t/having-trouble-writing-to-bits-in-schneider-m258-plc-through-modbus-tcp-error-bad/49642/5 "2021-08-06T13:26:21Z")

</div>

Okay, i am using Ignition 8.1.3

---

<div class="post-metadata">

**Author:** ![RVSG\_GC](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@RVSG\_GC](https://forum.inductiveautomation.com/u/RVSG_GC)\
**Post date:** [August 6, 2021, 1:32pm UTC](https://forum.inductiveautomation.com/t/having-trouble-writing-to-bits-in-schneider-m258-plc-through-modbus-tcp-error-bad/49642/6 "2021-08-06T13:32:38Z")

</div>

I found one of your posts on using expressions. May I know if this is correct to write to HR503.0 ?

```auto

if({1}, 
	toInt({tag path of the modbus tag with entire HR503 word }) | (1 << 0), 
	toInt({tag path of the modbus tag with entire HR503 word}) & ~(1 << 0))

```

---

<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:** [August 6, 2021, 1:35pm UTC](https://forum.inductiveautomation.com/t/having-trouble-writing-to-bits-in-schneider-m258-plc-through-modbus-tcp-error-bad/49642/7 "2021-08-06T13:35:58Z")

</div>

Yeah, for the Write Expression of a Derived Tag that seems right-ish.

The original post, for reference:

> [@Troubles making a Derived Boolean Tag work as a mask for a Short Tag](https://forum.inductiveautomation.com/t/troubles-making-a-derived-boolean-tag-work-as-a-mask-for-a-short-tag/15203/2):
>
> I’ve made it work using the following expressions: Read: (toInt({source}) \>\> N) & 1 Write: if({value}, toInt({source}) | (1 \<\< N), toInt({source}) & ~(1 \<\< N)) Where N will vary based on the bit number (0-15 for a Modbus register). You should also turn on the “OPC Optimistic Writes” flag for the scan class these tags belong to or you’re going to have a race condition if you try to write to multiple bits in the same word too quickly.

---

<div class="post-metadata">

**Author:** ![RVSG\_GC](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@RVSG\_GC](https://forum.inductiveautomation.com/u/RVSG_GC)\
**Post date:** [August 6, 2021, 1:48pm UTC](https://forum.inductiveautomation.com/t/having-trouble-writing-to-bits-in-schneider-m258-plc-through-modbus-tcp-error-bad/49642/8 "2021-08-06T13:48:44Z")

</div>

It worked!! Thank you so much!!

---

<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:** [August 6, 2021, 1:50pm UTC](https://forum.inductiveautomation.com/t/having-trouble-writing-to-bits-in-schneider-m258-plc-through-modbus-tcp-error-bad/49642/9 "2021-08-06T13:50:32Z")

</div>

If you’re going to use this strategy with multiple bits in the same register you need to make sure that Optimistic Writes is enabled for the Tag Group these tags all belong to.
