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

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.

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

1 Like

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?

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.

How can I setup the background thread in ignition ?

system.util.invokeAsynchronous.