# Best place to run system.serial.readBytes() in gateway scope for unsolicited data

**URL:** <https://forum.inductiveautomation.com/t/best-place-to-run-system-serial-readbytes-in-gateway-scope-for-unsolicited-data/37248>\
**Category:** Ignition\
**Created:** [July 31, 2020, 8:49am UTC](https://forum.inductiveautomation.com/t/best-place-to-run-system-serial-readbytes-in-gateway-scope-for-unsolicited-data/37248 "2020-07-31T08:49:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nader.chinichian](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nader.chinichian/32/9261_2.png) [@nader.chinichian](https://forum.inductiveautomation.com/u/nader.chinichian)\
**Post date:** [July 31, 2020, 8:49am UTC](https://forum.inductiveautomation.com/t/best-place-to-run-system-serial-readbytes-in-gateway-scope-for-unsolicited-data/37248/1 "2020-07-31T08:49:21Z")

</div>

Hi  
As system.serial.readBytes() block script until its timeout pass, what is best place and method to call this functions without causing gateway CPU usage increasing.  
I’d like a mechanism like interrupt when I receive a char in serial line and then I start to capture my packet.  
As my device send data unsolicited putting it in a gateway timer may cause I lost packet.

---

<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:** [July 31, 2020, 12:27pm UTC](https://forum.inductiveautomation.com/t/best-place-to-run-system-serial-readbytes-in-gateway-scope-for-unsolicited-data/37248/2 "2020-07-31T12:27:04Z")

</div>

A long-lived background thread that blocks on readBytes() is the only reliable method. But see this comment:

> [@Managing multiple asynchronous threads](http://forum.inductiveautomation.com/t/managing-multiple-asynchronous-threads/37185/2):
>
> To manage long-running jython threads through scripting restarts, you need to: Place the thread object in a dictionary that retains its content through script restarts. I used to recommend the dictionary from system.util.getGlobals(), but that is no longer suitable. I created a free [Life Cycle Module](http://forum.inductiveautomation.com/t/system-util-getglobals-not-persisted-across-designer-save/33869/14) that provides such dictionaries going forward. Write the asynchronous function’s loop so that it regularly checks [Thread.interrupted()](https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html#interrupted--), particularly after any java I/O operations. Clean up …

---

<div class="post-metadata">

**Author:** ![nader.chinichian](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nader.chinichian/32/9261_2.png) [@nader.chinichian](https://forum.inductiveautomation.com/u/nader.chinichian)\
**Post date:** [July 31, 2020, 6:14pm UTC](https://forum.inductiveautomation.com/t/best-place-to-run-system-serial-readbytes-in-gateway-scope-for-unsolicited-data/37248/3 "2020-07-31T18:14:54Z")

</div>

Thanks. But guide me how I can create such thread? or if I put my code in gateway timer with dedicated thread check is that ok?

---

<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:** [July 31, 2020, 8:19pm UTC](https://forum.inductiveautomation.com/t/best-place-to-run-system-serial-readbytes-in-gateway-scope-for-unsolicited-data/37248/4 "2020-07-31T20:19:50Z")

</div>

> [@nader.chinichian](#):
>
> is that ok?

No, the readBlocking needs to be in the background thread, not in any event script. The background thread should write any received data to one or more tags, which you can then monitor for changes in the usual way.

A gateway timer thread should regularly call a do-nothing function in the script module that contains the background function, just to ensure the script module is loaded. The background thread would be started on script module load/reload.

---

<div class="post-metadata">

**Author:** ![nader.chinichian](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nader.chinichian/32/9261_2.png) [@nader.chinichian](https://forum.inductiveautomation.com/u/nader.chinichian)\
**Post date:** [July 31, 2020, 8:21pm UTC](https://forum.inductiveautomation.com/t/best-place-to-run-system-serial-readbytes-in-gateway-scope-for-unsolicited-data/37248/5 "2020-07-31T20:21:34Z")

</div>

How can I setup the background thread in ignition ?

---

<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:** [July 31, 2020, 8:23pm UTC](https://forum.inductiveautomation.com/t/best-place-to-run-system-serial-readbytes-in-gateway-scope-for-unsolicited-data/37248/6 "2020-07-31T20:23:27Z")

</div>

system.util.invokeAsynchronous.
