setTimeOut function for UDP?

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 .

I think it could also relate to this mDNS discussion, where I found the same time (it also makes use of Udp internally).

I’ve put my test code on gist.

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.

Ok, so I can actually use all the commands from the Stream class that I can find on the Arduino site?

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.

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.

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: