# Best way to measure serial communication Latency | way to collect latencies in the program

**URL:** <https://forums.openmv.io/t/best-way-to-measure-serial-communication-latency-way-to-collect-latencies-in-the-program/1223>\
**Category:** OpenMV Boards\
**Created:** [April 25, 2019, 12:18am UTC](https://forums.openmv.io/t/best-way-to-measure-serial-communication-latency-way-to-collect-latencies-in-the-program/1223 "2019-04-25T00:18:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jivrik](https://avatars.discourse-cdn.com/v4/letter/j/dec6dc/32.png) [@Jivrik](https://forums.openmv.io/u/Jivrik)\
**Post date:** [April 25, 2019, 12:18am UTC](https://forums.openmv.io/t/best-way-to-measure-serial-communication-latency-way-to-collect-latencies-in-the-program/1223/1 "2019-04-25T00:18:02Z")

</div>

Hi,  
  
  
Please what would be the best way to measure the latency that any process could consume during the execution of program?  
  
  
For example, what I am currently doing is adding timestamps before and after the procedure that I want to verify. Then the timestamps differences are the latency that I should be looking for. The timestamps values come from the pyb.micros() funcition. So, in the code below:

```python
t_0 = pyb.micros()
    img = sensor.snapshot()
    #break
    t_1 = pyb.micros()
    diff = t_1 - t_0

    show_process_timing(t_0,t_1,"Init Snapshot")

```

The init snapshot is the id of the process that I want to verify. Please here is here is the implementation of the show\_timing\_process() function

```python
def show_process_timing(t_x , t_y , id_pos):
    print("\n{}".format(id_pos))
    print("\nDifference: {} us\n".format(t_y - t_x))

```

Should I rely on the values returned by the pyb.micros() function?  
  
  
Also How would you evaluate the delay taken by an SPI , I2C or a UART transaction?  
  
  
  
  
Thank you!  
  
  
  
Benjamin.

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [April 25, 2019, 5:42am UTC](https://forums.openmv.io/t/best-way-to-measure-serial-communication-latency-way-to-collect-latencies-in-the-program/1223/2 "2019-04-25T05:42:57Z")

</div>

Yeah, this is pretty much valid.

As for the UART/SPI. The main thread waits until the data is sent. So, it’s fine to put a timer around any call.

---

<div class="post-metadata">

**Author:** ![Jivrik](https://avatars.discourse-cdn.com/v4/letter/j/dec6dc/32.png) [@Jivrik](https://forums.openmv.io/u/Jivrik)\
**Post date:** [April 26, 2019, 12:30am UTC](https://forums.openmv.io/t/best-way-to-measure-serial-communication-latency-way-to-collect-latencies-in-the-program/1223/3 "2019-04-26T00:30:42Z")

</div>

Thank you.

I have this concern, is it normal that the UART transaction becomes faster than the I2C transaction?

if yes, How come?

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [April 27, 2019, 6:06am UTC](https://forums.openmv.io/t/best-way-to-measure-serial-communication-latency-way-to-collect-latencies-in-the-program/1223/4 "2019-04-27T06:06:00Z")

</div>

Hi, microcontrollers usually communicate to each other via UARTs. SPI and I2C are for communication to more simple hardware chips typically.

In terms of coding… when using the OpenMV Cam you have a camera sensor that’s doing work. If you write your code to wait for I2C bus access always then no work can be done. The UART allows bytes to be received asynchronously however. SO, you can process bytes in a loop while doing vision stuff too and then send out results.
