Troubleshooting
Why a Java byte array fails writeFile after an MQTT tag transfer
Short answer
The TypeError from system.file.writeFile is a local type mismatch, not an MQTT delivery fault. Once the receiving tag holds the expected bytes, either type the receiving tag as Binary Data or convert the value with the Jython jarray module and the 'b' typecode, mapping unsigned bytes 128 through 255 down by 256.

On this page
- What the coercion error actually means
- Prove the transport delivered before changing types
- Fix one: type the receiving tag as Binary Data
- Fix two: convert to a Java byte array with jarray
- Worked example: two bytes through the mapping
- When the file saves as array('b', [-31, 58, 11]) text
- Which fix to commit to
- Questions asked
The TypeError is not an MQTT fault. When Gateway B's receiving tag already holds the expected bytes and system.file.writeFile still raises TypeError("writeFile(): 2nd arg can't be coerced to String, byte",), the script is handing the function an object it cannot accept: a formatted string, a generic Python array, or a tag-read wrapper instead of a Java byte array. Fix it at that last boundary. Either type the receiving tag as Binary Data, or convert the value with the Jython jarray module before writing. This article gives the checks that separate transport from conversion, both fixes with the arithmetic they rely on, and the decision for each case.
What the coercion error actually means #
The scripting function that writes data to a file, system.file.writeFile, is documented in the Ignition 8.1 manual as taking the data and a filename; its data argument must coerce, at the Java level, to a string or to a byte array. The value arriving through a Byte Array tag has the runtime type Java.Byte.Array, while a Binary Data tag presents it as Array.Array. Those are the tag-level representations; the script still has to pass an object the Java-backed function accepts.
A printed value in the tag browser is display text, not the runtime object. That is why a tag that looks correct in the designer can still fail the write. Diagnose the object immediately before the call, not its rendering.
Prove the transport delivered before changing types #
Work through the boundaries in order and record what each one shows:
- Broker reachability: both gateways hold a connected client session to their configured broker endpoint, including any satellite or routed path between them.
- Publication: Gateway A writes its tag and the publishing module sends the change.
- Subscription: Gateway B's receiving module receives the message and updates the receiving tag.
- Delivery proof: read the receiving tag from a script, check the quality is good, and compare the element count and several values against the source bytes.
- Writer boundary: extract the contained value, not the tag-read result object, and pass it to an isolated test write.
If the receiving tag updates with the correct length and matching values and the write still fails, transport is proven good and the failure is the object type at the writer. Do not touch the network configuration from here on. A delivered value can still look like unreadable bytes in client tools; why Ignition Edge MQTT payloads look like unreadable bytes covers that display behavior separately, and it is not this error.
Fix one: type the receiving tag as Binary Data #
Change the receiving tag's datatype on Gateway B to Binary Data and leave Gateway A's Byte Array tag untouched. The script then receives a value that system.file.writeFile accepts, and the publishing side publishes exactly the same bytes as before.
Verify the fix survives the events that can restore a tag definition: a tag provider restart, tag recreation, or a configuration refresh from the sending side. After each, check the receiving datatype is still Binary Data, resend a fresh file, and compare the written file against the source byte-for-byte. If a refresh reverts the datatype, move the conversion into the script instead, where it cannot be undone by configuration.
Fix two: convert to a Java byte array with jarray #
The Jython jarray module constructs low-level Java arrays. Its b typecode requests a Java byte array, which is what the file writer wants. Because Java bytes are signed while a Python bytearray exposes values from 0 through 255, map each value below 128 unchanged and subtract 256 from each value of 128 or greater:
import jarray
def pythonByteString_to_jarray(pyString):
return jarray.array(
((x if x < 128 else (x - 256)) for x in bytearray(pyString)),
'b'
)
This helper takes actual byte-string content. It does not parse the printed text of an array, unwrap a tag-read result, or decode an image. If the input is already a sequence of signed byte elements, build the Java array from those elements directly and skip the mapping, because applying it twice drives values out of the signed range and the conversion fails.
Worked example: two bytes through the mapping #
Take a source file whose first two bytes have the unsigned values 225 and 58.
- 58 is below 128, so it passes through unchanged: 58.
- 225 is 128 or greater, so it maps to 225 - 256 = -31.
Check the bits: 225 in unsigned 8-bit form is 11100001, and -31 in two's complement 8-bit form is also 11100001. Likewise 58 is 00111010 in both forms. The mapping changes only the signed interpretation, never the bit pattern, so the written bytes equal the source bytes.
Prove it on the target: write the converted array to a test file, read that file back as a byte string, and compare it against the source over the full element count with an equality check. That comparison, not a printed dump, is the acceptance test for the fix.
When the file saves as array('b', [-31, 58, 11]) text #
If the written file opens as the characters array('b', [-31, 58, 11]) instead of binary content, some earlier operation converted the array to its string form. The common causes are string concatenation before the write and curly-brace parameters in a SQL statement, which perform string substitution. Push a byte array to a database only through question-mark placeholders in a prepared query, or through the named parameters of a named query. For the file write itself, make sure the argument you pass is the array object, inspected with its runtime type, and never a string built from it.
Which fix to commit to #
| Case | Fix |
|---|---|
| Receiving tag updates correctly, script controls the write | Convert with jarray and the b typecode inside the script |
| Configuration refreshes keep reverting tag datatypes | Script conversion, so the fix cannot be undone by config |
| Datatype stays put after restarts and refreshes, minimal change wanted | Binary Data receiving tag on Gateway B, Byte Array kept on Gateway A |
| Many or large files, and the link can carry protocol overhead | The file transmission mechanism in MQTT Transmission and MQTT Engine |
On a metered or satellite link, the tag-based byte transfer carries the least traffic, which is why it remains the right transport here; the cost is that you own the conversion at the receiving boundary. Commit to the script conversion when you need the behavior to survive tag recreation, and to the Binary Data receiving tag when the configuration is stable and you want the smallest change. Either way, the acceptance test is the same: a fresh file after a reconnect, written successfully, matching the source byte-for-byte.
Questions asked
Does typing the receiving tag as Binary Data change what the sending gateway publishes?
No. Gateway A keeps its Byte Array tag and publishes exactly the same bytes. The change is only how Gateway B represents the received value locally, so the script hands the file writer an object it accepts. After any tag provider restart, tag recreation or configuration refresh, resend a fresh file and confirm the receiving datatype is still Binary Data and the written file matches the source bytes.
Why does my unsigned byte 225 show up as -31 after conversion?
Java bytes are signed and hold values from -128 through 127, while a Python bytearray exposes 0 through 255. Both store the same eight bits: 225 in unsigned form and -31 in two's complement are both the bit pattern 11100001. The mapping is subtract 256 from any value of 128 or greater, leave values below 128 unchanged. The bits, and therefore the file content, are preserved.
My saved file contains array('b', [-31, 58, 11]) as text. Where did it go wrong?
Somewhere upstream the array was turned into its printed representation instead of being passed as an object. The usual culprit is string substitution, such as curly-brace parameters in a SQL statement or string concatenation before the write. Use question-mark placeholders in a prepared query or named parameters in a named query, and pass the array object itself to the file writer, never its printed form.
When is the dedicated file transmission mechanism the better choice?
MQTT Transmission and MQTT Engine include a general-purpose file transfer mechanism that handles chunking and delivery for you. Choose it when the link can carry the extra protocol overhead. On a metered or satellite link where traffic is the constraint, a byte-array tag transfer carries less overhead, but you own the conversion at the receiving boundary as described in this article.


