# TCP Driver Expected ByteString Error

**URL:** <https://forum.inductiveautomation.com/t/tcp-driver-expected-bytestring-error/99515>\
**Category:** Ignition\
**Created:** [December 6, 2024, 5:26am UTC](https://forum.inductiveautomation.com/t/tcp-driver-expected-bytestring-error/99515 "2024-12-06T05:26:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![loki](https://avatars.discourse-cdn.com/v4/letter/l/3be4f8/32.png) [@loki](https://forum.inductiveautomation.com/u/loki)\
**Post date:** [December 6, 2024, 5:26am UTC](https://forum.inductiveautomation.com/t/tcp-driver-expected-bytestring-error/99515/1 "2024-12-06T05:26:09Z")

</div>

Hello!

Trying to use the TCP driver to talk to a machine.

Currently getting a "Expected ByteString, received xxx" error message when writing to the /WritableBytes field.

The tag is written to via script, see below:

```python
test = chr(2) + 'CurrentStats?' + chr(3) + chr(207)
values = [ord(i) for i in test]
tagPath = '[Test]zzLP_TESTING/SG01/Data/WriteableBytes' #writes to /Writable no 'e'
system.tag.writeBlocking([tagPath], [values])

```

![Pasted image 20241206144548](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/1/3143c1d03590917d81107c2617f52a9de36404d3.png)

 ![Pasted image 20241206144541](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/b/6b070ff04092dd559e0d9d2fb7bd229ea2c37ed4.png)

I have tried several different ways of specifying value:

```python
value = b'\x02\x43\x75\x72\x72\x65\x6E\x74\x53\x74\x61\x74\x73\x3F\x03\xCF'
value = b'\x02CurrentStats?\x03\xcf'
value = [2, 67, 117, 114, 114, 101, 110, 116, 83, 116, 97, 116, 115, 63, 3, 207]
value = array('b', [2, 67, 117, 114, 114, 101, 110, 116, 83, 116, 97, 116, 115, 63, 3, -49])

```

but each gives me the same error message "Expected ByteStream...".

Any hints on the right way to specify?

Thank you kindly!

---

<div class="post-metadata">

**Author:** ![David\_Stone](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/david_stone/32/79749_2.png) [@David\_Stone](https://forum.inductiveautomation.com/u/David_Stone)\
**Post date:** [December 6, 2024, 9:45am UTC](https://forum.inductiveautomation.com/t/tcp-driver-expected-bytestring-error/99515/2 "2024-12-06T09:45:14Z")

</div>

I have used this code before to convert a python array into a byte stream.  
I have added your values there into the value variable, maybe this could work?  
Hard to test without your system.  
I was using it for image data coming via various sources.

```python
value = array('b', [2, 67, 117, 114, 114, 101, 110, 116, 83, 116, 97, 116, 115, 63, 3, -49])
imageBytes = ''.join(map(lambda x: chr(x % 256), value)) #convert the image to a stream of bytes

```

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [December 6, 2024, 10:29pm UTC](https://forum.inductiveautomation.com/t/tcp-driver-expected-bytestring-error/99515/3 "2024-12-06T22:29:04Z")

</div>

Use the `java.io.ByteArrayOutputStream` to create the ByteStream

```python
from java.io import DataOutputStream, ByteArrayOutputStream
output = ByteArrayOutputStream()
outputStream = DataOutputStream(output)
outputStream.writeBytes(chr(2) + 'CurrentStats?' + chr(3) + chr(207))
output.toByteArray()

```

Output:

```python
>>> 
array('b', [2, 67, 117, 114, 114, 101, 110, 116, 83, 116, 97, 116, 115, 63, 3, -49])
>>>

```

I belive if you just do something like:

```python
system.tag.writeBlocking(['[Test]zzLP_TESTING/SG01/Data/WriteableBytes'],[output])

```

It will work, but I don't have anything to test that with.

---

<div class="post-metadata">

**Author:** ![loki](https://avatars.discourse-cdn.com/v4/letter/l/3be4f8/32.png) [@loki](https://forum.inductiveautomation.com/u/loki)\
**Post date:** [December 12, 2024, 11:35pm UTC](https://forum.inductiveautomation.com/t/tcp-driver-expected-bytestring-error/99515/4 "2024-12-12T23:35:05Z")

</div>

Thanks for the ideas. Tried:

```python
from org.python.core.util import StringUtil

test = chr(2) + 'CurrentStats?' + chr(3) + chr(207)
values = [ord(i) for i in test]

imageBytes_1 = ''.join(map(lambda x: chr(x % 256), values))
imageBytes_1

system.tag.writeBlocking(['[Test]zzLP_TESTING/BN07/Data/WriteableBytes'],[imageBytes_1])

valueArray = StringUtil.toBytes(test)
imageBytes_2 = ''.join(map(lambda x: chr(x % 256), valueArray))
imageBytes_2

system.tag.writeBlocking(['[Test]zzLP_TESTING/BN07/Data/WriteableBytes'],[imageBytes_2])

```

```python
>>> 
'\x02CurrentStats?\x03\xcf'
[Bad]
'\x02CurrentStats?\x03\xcf'

```

And in the driver logs:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/2/62a51f6345a64a1f39bde87266c7cbf5d16554e7.png)

And for reference:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/8/a8ec3608103cb7426c3f72cecbf5352fdba8a145.png)

---

<div class="post-metadata">

**Author:** ![loki](https://avatars.discourse-cdn.com/v4/letter/l/3be4f8/32.png) [@loki](https://forum.inductiveautomation.com/u/loki)\
**Post date:** [December 12, 2024, 11:39pm UTC](https://forum.inductiveautomation.com/t/tcp-driver-expected-bytestring-error/99515/5 "2024-12-12T23:39:28Z")

</div>

Thanks for the ideas - looks promising with the right type conversion but I get a

`java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: com.inductiveautomation.ignition.client.gateway_interface.GatewayException: Error serializing parameters.`

Error message when attempting the writeBlocking

**Code** :

```python
from java.io import DataOutputStream, ByteArrayOutputStream
output = ByteArrayOutputStream()
outputStream = DataOutputStream(output)
outputStream.writeBytes(chr(2) + 'CurrentStats?' + chr(3) + chr(207))
output.toByteArray()

type(output)
output

system.tag.writeBlocking(['[Test]zzLP_TESTING/BN07/Data/WriteableBytes'],[output])

```

**Output** :

```python
array('b', [2, 67, 117, 114, 114, 101, 110, 116, 83, 116, 97, 116, 115, 63, 3, -49])
<type 'java.io.ByteArrayOutputStream'>
CurrentStats?Ï
Java Traceback:
Traceback (most recent call last):
  File "<input>", line 67, in <module>
	at java.base/java.util.concurrent.CompletableFuture.reportGet(Unknown Source)

	at java.base/java.util.concurrent.CompletableFuture.get(Unknown Source)

	at com.inductiveautomation.ignition.client.script.ClientTagUtilities.writeBlockingImpl(ClientTagUtilities.java:200)

	at com.inductiveautomation.ignition.common.script.builtin.AbstractTagUtilities.writeBlocking(AbstractTagUtilities.java:547)

	at jdk.internal.reflect.GeneratedMethodAccessor93.invoke(Unknown Source)

	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

	at java.base/java.lang.reflect.Method.invoke(Unknown Source)

java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: com.inductiveautomation.ignition.client.gateway_interface.GatewayException: Error serializing parameters.

	at org.python.core.Py.JavaError(Py.java:545)

	at com.inductiveautomation.ignition.client.script.ClientTagUtilities.writeBlockingImpl(ClientTagUtilities.java:206)

	at com.inductiveautomation.ignition.common.script.builtin.AbstractTagUtilities.writeBlocking(AbstractTagUtilities.java:547)

	at jdk.internal.reflect.GeneratedMethodAccessor93.invoke(Unknown Source)

	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

	at java.base/java.lang.reflect.Method.invoke(Unknown Source)

	at org.python.core.PyReflectedFunction. __call__ (PyReflectedFunction.java:190)

	at com.inductiveautomation.ignition.common.script.ScriptManager$ReflectedInstanceFunction. __call__ (ScriptManager.java:553)

	at org.python.core.PyObject. __call__ (PyObject.java:477)

	at org.python.core.PyObject. __call__ (PyObject.java:481)

	at org.python.pycode._pyx173.f$0(<input>:67)

	at org.python.pycode._pyx173.call_function(<input>)

	at org.python.core.PyTableCode.call(PyTableCode.java:173)

	at org.python.core.PyCode.call(PyCode.java:18)

	at org.python.core.Py.runCode(Py.java:1703)

	at org.python.core.Py.exec(Py.java:1747)

	at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:277)

	at org.python.util.InteractiveInterpreter.runcode(InteractiveInterpreter.java:130)

	at com.inductiveautomation.ignition.designer.gui.tools.jythonconsole.JythonConsole$ConsoleWorker.doInBackground(JythonConsole.java:628)

	at com.inductiveautomation.ignition.designer.gui.tools.jythonconsole.JythonConsole$ConsoleWorker.doInBackground(JythonConsole.java:616)

	at java.desktop/javax.swing.SwingWorker$1.call(Unknown Source)

	at java.base/java.util.concurrent.FutureTask.run(Unknown Source)

	at java.desktop/javax.swing.SwingWorker.run(Unknown Source)

	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

	at java.base/java.lang.Thread.run(Unknown Source)

Caused by: java.util.concurrent.ExecutionException: com.inductiveautomation.ignition.client.gateway_interface.GatewayException: Error serializing parameters.

	at java.base/java.util.concurrent.CompletableFuture.reportGet(Unknown Source)

	at java.base/java.util.concurrent.CompletableFuture.get(Unknown Source)

	at com.inductiveautomation.ignition.client.script.ClientTagUtilities.writeBlockingImpl(ClientTagUtilities.java:200)

	... 24 more

Caused by: com.inductiveautomation.ignition.client.gateway_interface.GatewayException: Error serializing parameters.

	at com.inductiveautomation.ignition.client.gateway_interface.GatewayInterface.newGatewayException(GatewayInterface.java:360)

	at com.inductiveautomation.ignition.client.gateway_interface.GatewayInterface.invoke(GatewayInterface.java:965)

	at com.inductiveautomation.ignition.client.tags.impl.GatewayTagInterface$GatewayProviderProxy.lambda$writeAsync$1(GatewayTagInterface.java:358)

	at java.base/java.util.concurrent.CompletableFuture$AsyncRun.run(Unknown Source)

	at java.base/java.util.concurrent.CompletableFuture$AsyncRun.exec(Unknown Source)

	at java.base/java.util.concurrent.ForkJoinTask.doExec(Unknown Source)

	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(Unknown Source)

	at java.base/java.util.concurrent.ForkJoinPool.scan(Unknown Source)

	at java.base/java.util.concurrent.ForkJoinPool.runWorker(Unknown Source)

	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(Unknown Source)

Caused by: java.io.NotSerializableException: java.io.ByteArrayOutputStream

	at java.base/java.io.ObjectOutputStream.writeObject0(Unknown Source)

	at java.base/java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)

	at java.base/java.io.ObjectOutputStream.writeSerialData(Unknown Source)

	at java.base/java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)

	at java.base/java.io.ObjectOutputStream.writeObject0(Unknown Source)

	at java.base/java.io.ObjectOutputStream.writeObject(Unknown Source)

	at java.base/java.util.ArrayList.writeObject(Unknown Source)

	at java.base/jdk.internal.reflect.GeneratedMethodAccessor34.invoke(Unknown Source)

	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

	at java.base/java.lang.reflect.Method.invoke(Unknown Source)

	at java.base/java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)

	at java.base/java.io.ObjectOutputStream.writeSerialData(Unknown Source)

	at java.base/java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)

	at java.base/java.io.ObjectOutputStream.writeObject0(Unknown Source)

	at java.base/java.io.ObjectOutputStream.writeObject(Unknown Source)

	at com.inductiveautomation.ignition.common.Base64.encodeObject(Base64.java:496)

	at com.inductiveautomation.ignition.client.gateway_interface.GatewayInterface$InvokeMessage.setArgs(GatewayInterface.java:1382)

	at com.inductiveautomation.ignition.client.gateway_interface.GatewayInterface.invoke(GatewayInterface.java:963)

	... 8 more

Traceback (most recent call last):
  File "<input>", line 67, in <module>
	at java.base/java.util.concurrent.CompletableFuture.reportGet(Unknown Source)

	at java.base/java.util.concurrent.CompletableFuture.get(Unknown Source)

	at com.inductiveautomation.ignition.client.script.ClientTagUtilities.writeBlockingImpl(ClientTagUtilities.java:200)

	at com.inductiveautomation.ignition.common.script.builtin.AbstractTagUtilities.writeBlocking(AbstractTagUtilities.java:547)

	at jdk.internal.reflect.GeneratedMethodAccessor93.invoke(Unknown Source)

	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

	at java.base/java.lang.reflect.Method.invoke(Unknown Source)

java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: com.inductiveautomation.ignition.client.gateway_interface.GatewayException: Error serializing parameters.

```

---

<div class="post-metadata">

**Author:** ![loki](https://avatars.discourse-cdn.com/v4/letter/l/3be4f8/32.png) [@loki](https://forum.inductiveautomation.com/u/loki)\
**Post date:** [December 13, 2024, 1:05am UTC](https://forum.inductiveautomation.com/t/tcp-driver-expected-bytestring-error/99515/6 "2024-12-13T01:05:11Z")

</div>

Update: the data type of WritableBytes needed to be updated to be Short Array

---

<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:** [December 13, 2024, 1:41am UTC](https://forum.inductiveautomation.com/t/tcp-driver-expected-bytestring-error/99515/7 "2024-12-13T01:41:58Z")

</div>

The datatype is OPC UA ByteString, which is basically equivalent to an array of _unsigned_ bytes.

Ignition's tag system doesn't have unsigned types (nor does Java natively... 😭 ), so it's always a sized up signed type instead. When you write short array to a ByteString tag there's some logic in our OPC UA client layer to handle that conversion.
