# setTimeOut function for UDP?

**URL:** <https://community.particle.io/t/settimeout-function-for-udp/20668>\
**Category:** Firmware\
**Created:** [March 2, 2016, 10:01pm UTC](https://community.particle.io/t/settimeout-function-for-udp/20668 "2016-03-02T22:01:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kasper](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/kasper/32/14366_2.png) [@kasper](https://community.particle.io/u/kasper)\
**Post date:** [March 2, 2016, 10:01pm UTC](https://community.particle.io/t/settimeout-function-for-udp/20668/1 "2016-03-02T22:01:55Z")

</div>

I’d like to receive some UDP commands. I’m using the example provided in the documentation.

Since I’m developing a timing critical application (LED animations) I’d like to parse messages as quickly as possible, or at least to continue as quickly as possible if there is no message to parse. During my timing tests I noticed that the Udp.parsePacket() function takes around 997 microseconds (so 1ms). However when a packet is found, it takes me 17us to parse the whole packet.

So I have the idea that the function waits on something. At least I know that something like that happens on the Arduino with the Serial.readBytes() function.

I don’t know if I’m right and if this is the case in this situation?

I think it would be nice if I can set a time out on microsecond level. Like Arduino does (however millisecond level) for the serial port: [Arduino setTimeOut() function](https://www.arduino.cc/en/Reference/StreamSetTimeout) .

I think it could also relate to this [mDNS discussion](https://community.particle.io/t/mdns-and-dns-service-discovery-library/9550/12), where I found the same time (it also makes use of Udp internally).

I’ve put my test code on gist.

> <https://gist.github.com/kasperkamperman/1f646fecf099289c8b3d>

---

<div class="post-metadata">

**Author:** ![bko](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bko/32/20704_2.png) [@bko](https://community.particle.io/u/bko)\
**Post date:** [March 2, 2016, 10:11pm UTC](https://community.particle.io/t/settimeout-function-for-udp/20668/2 "2016-03-02T22:11:18Z")

</div>

Hi @kasper

The method you want `setTimeOut` is available since UDP inherits from stream which supplies it, but I don’t know how well it work for you. It seems to be used by the parse methods that find ints and floats in a stream and not by the main parsePacket.

---

<div class="post-metadata">

**Author:** ![kasper](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/kasper/32/14366_2.png) [@kasper](https://community.particle.io/u/kasper)\
**Post date:** [March 2, 2016, 10:22pm UTC](https://community.particle.io/t/settimeout-function-for-udp/20668/3 "2016-03-02T22:22:19Z")

</div>

Ok, so I can actually use all the commands from the Stream class that I can find on the Arduino site? [https://www.arduino.cc/en/Reference/Stream](https://www.arduino.cc/en/Reference/Stream)

Or is there a Particle specific reference as well (just starting with the Photon).

Problem with the setTimeOut is that it works on millisecond level… I would like to have it faster if possible. But I’m aware that my demands might be too high and that I have to find another solution.

---

<div class="post-metadata">

**Author:** ![bko](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bko/32/20704_2.png) [@bko](https://community.particle.io/u/bko)\
**Post date:** [March 2, 2016, 10:29pm UTC](https://community.particle.io/t/settimeout-function-for-udp/20668/4 "2016-03-02T22:29:00Z")

</div>

You can try it–it is there but it is not doc’ed and from reading the code on github, it looks like the timeout only applies to the methods from stream like peek and parseInt etc. It didn’t see it in UDP parsePacket(). I don’t think it will really help you in this case.

Does it do what you want on Arduino? I don’t believe that it does but I have not tried it.

---

<div class="post-metadata">

**Author:** ![kasper](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/kasper/32/14366_2.png) [@kasper](https://community.particle.io/u/kasper)\
**Post date:** [March 11, 2016, 5:33pm UTC](https://community.particle.io/t/settimeout-function-for-udp/20668/5 "2016-03-11T17:33:51Z")

</div>

The duration of 1ms is not really an issue anymore. I discovered that the Particle Photon supports threads, so network communication can be done in the "background" of my time critical code.

Just for people who are interested you can read more in this topic:

> [@Control system thread duration or keeping a really fixed time loop](https://community.particle.io/t/control-system-thread-duration-or-keeping-a-really-fixed-time-loop/20757):
>
> I’m working on an application where I’d like to update a led strip with a fixed rate of 2ms (so about 500 times a second). Some times I use that time of 2ms to do calculations, but other times I don’t (so I have like 1 to 1.5ms free). I read this in the manual about RTOS: The RTOS is responsible for switching between the application thread and the system thread, which it does automatically every millisecond. This has 2 main consequences: delays close to 1ms are typically much longer applica…
