[URGENT] Measuring time interval for serial communication

Hi guys, I’m a newbie to both arduino and python programming.

My aim is to establish a serial communication between arduino and pc by using pyserial and then measure the reaction time of both sides.

STEPS :

  1. upload sketch to arduino
  2. run python code
  3. something prompt by the python coding which requires arduino to react by sending something back to python
    4 ) python calculates the time taken for this

This is quite urgent for me :frowning:

I have basic codes for the serial communication
but i have no idea how to combine with the time measurements

serial_test_2.ino (201 Bytes)

int cnt=0;
void setup() {

  Serial.begin(9600);
  
}

void loop() {
  
  Serial.print("I am Counting to: ");
  Serial.print(cnt);
  Serial.println(" Mississippi.");
  cnt=cnt+1;
  delay(1000);
  
}

Is this a Python or Arduino query ? Are you aware of the Arduino micros() and millis() functions that would allow you to measure time intervals down to 4 microseconds an 1 millisecond respectively ?

This Python - Arduino demo may be helpful.

It should not be too difficult to add code on the Python side to measure the round trip time.

...R

I have basic codes for the serial communication

would help if you had shared them...

J-M-L: would help if you had shared them...

I guess it's the code that OP attached and that AWOL kindly posted ;)

sterretje: I guess it's the code that OP attached and that AWOL kindly posted ;)

where is the python? that's not a communication, that's a monologue (and not a very interesting one :) )

J-M-L: where is the python?

Somewhere in the bush (rock python will be somewhere on the rocks having a nap) :D

Python on the rocks? Ugh. I'll stick with whiskey on the rocks.

We have not heard from the OP so it seems the word "[URGENT]" has taken on a meaning with which I am not familiar.

...R

Robin2: We have not heard from the OP so it seems the word "[URGENT]" has taken on a meaning with which I am not familiar.

...R

Maybe he needed an answer in 5 minutes because he was going back into his class and had not done his assignment... now it's too late... :)

If python is an interpreted language, then a chunk of the time will be python itself doing its thing.

Another chunk will be the serial communication. This depends on the baud rate and the length of the massages you are passing back and forth. "I am counting to 2 mississippi\n". 30 characters, 3 milliseconds-ish. Probably.

The arduino time - well - it depends on what the arduino is doing. Oh, I see - you are doing a 1000ms delay. Are you aware that the arduino clock can be off by 5-10%? It's one reason why they are so cheap: no crystal. If you need to measure time accurately, you need a RTC (real-time clock) module.

In any case,

int cnt=0;
void setup() {

  Serial.begin(9600);
  
}

void loop() {
  if(!Serial.available()) return;
  if(Serial.read() != '#') return;
  Serial.println("*");
  Serial.print("I am Counting to: ");
  Serial.print(cnt);
  Serial.println(" Mississippi.");
  cnt=cnt+1;
  delay(1000);
  Serial.println("!");  
}

Have python write a '#'. It should then read a '', some characters, there will be a delay, and then it will read '!'. Time the gap between reading the '' and the '!' in python (however you do that - probably lambdas or something) and there you go.

PaulMurrayCbr: If python is an interpreted language, then a chunk of the time will be python itself doing its thing.

Another chunk will be the serial communication. This depends on the baud rate and the length of the massages you are passing back and forth. "I am counting to 2 mississippi\n". 30 characters, 3 milliseconds-ish. Probably.

The arduino time - well - it depends on what the arduino is doing.

To be fair, I see no reason to think that the OP is trying to measure anything other than the combined impact of all of that.

It can be useful to know the full "round-trip" time if you want to figure out how many messages can be sent per second. And once you have a baseline time you can see the effect of tweaking different parts.

...R

Thanks for the prompt reply guys. it is a arduino-python enquiry actually.

So sorry that i didnt know the codes werent attached properly...

I would like to calculate the round trip time in milliseconds ( microseconds will be awesome ! )

I am actually very lost in this , been looking at those info regarding on pyserial and how it links with my arduino code. It has given me a big headache :confused:

int cnt=0;
void setup() {

  Serial.begin(9600);
  
}

void loop() {
  
  Serial.print("I am Counting to: ");
  Serial.print(cnt);
  Serial.println(" Mississippi.");
  cnt=cnt+1;
  delay(1000);
  
}
import serial 

arduinoSerialData = serial.Serial('com4',9600) 

while (1==1):
    if (arduinoSerialData.inWaiting()>0):
        myData = arduinoSerialData.readline()
        print myData

u guys can modify my codes tho. i just need a simple code

Can't help with the python side; the below will simply echo a character received from the PC.

void loop()
{
  if (Serial.available() > 0)
  {
    Serial.write(Serial.read());
  }
}

The python side can do the timing. The basics: set a start time send the character set the end time when the echo is received calculate the difference.

Alright, thank you ! Will look at it and try to build the python code

trxy: I would like to calculate the round trip time in milliseconds ( microseconds will be awesome ! )

Have you studied the link I gave you in Reply #3

...R

yaya i did..

trxy: yaya i did..

And ...

...R