Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Void methods: Wait for response? #40

Open
h-huss opened this issue Jun 3, 2024 · 5 comments
Open

Void methods: Wait for response? #40

h-huss opened this issue Jun 3, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@h-huss
Copy link

h-huss commented Jun 3, 2024

Is your feature request related to a problem? Please describe.
I've just noticed that calling void methods / methods without a return value does not block, they seem to return immediately. This caused errors, as I was expecting that the arduino method was done when the method returned.

Describe the solution you'd like
It would be nice to document that void method dont block. Alternatively, void methods could block, to behave similar to all other functions (this would be a breaking change).

@jfjlaros
Copy link
Owner

jfjlaros commented Jun 3, 2024

A void function is called just like any other function. The only difference is that no result is written after the function has finished. You can test this by adding a large delay() to such a function and see whether it returns directly or not.

What kind of errors are you experiencing and can you share a minimal example so I can reproduce these errors?

@h-huss
Copy link
Author

h-huss commented Jun 3, 2024

Thank you for your quick response. I was a bit unconcrete: The Arduino function is called fine, however, the python function returns before the arduino function is done:

Arduino (ESP32)

void longDelay(){
  delay(5000);
}

boolean longDelayWithReturn(){
  delay(5000);
  return true;
}

void loop(void)
{
  interface(
      Serial,
      longDelay, "longDelay: ",
      longDelayWithReturn, "longDelayWithReturn: @return ret:"
  );
}

Python:

with Interface("/dev/ttyUSB0", baudrate=50000) as mcu:   
    start = time.time()
    mcu.longDelayWithReturn()
    print(time.time()-start) # 5.002 => As expected, we wait for the result
    
    start = time.time()
    mcu.longDelay()
    print(time.time()-start) # 0.0002 => We return almost immediatly. This is unexpected
    # At this point, the arduino function is still running, but the python function has returned.
    
    start = time.time()
    mcu.longDelayWithReturn()
    print(time.time()-start) #10.004 => 2*5 seconds, as we have to wait for the previous delay to be finished

This is most likely caused by https://github.com/jfjlaros/arduino-simple-rpc/blob/2a78a493031dfa06e826f42d3ec419686676eede/simple_rpc/simple_rpc.py#L186-L188 , void methods dont wait

@jfjlaros
Copy link
Owner

jfjlaros commented Jun 3, 2024

Indeed, the client has no way of knowing whether a void function has finished.

I guess we could let a void function write one byte to implement blocking behaviour, or we could implement it ourselves by changing the return type or, if this is not an option, by adding a wait function. E.g.,

On the device:

bool wait() {
  return true;
}


void loop() {
  interface(
    Serial,
    wait, "wait: Wait for the previous command to finish. @return: True."",
    // ...
  );
}

and client side:

mcu.longDelay()
mcu.wait()

I am not sure if this is a feature or a bug.

@h-huss
Copy link
Author

h-huss commented Jun 3, 2024

The wait function is an interesting idea :)

I would leave it as-is, as it could be an useful feature, but there should be a warning somewhere.

@jfjlaros
Copy link
Owner

jfjlaros commented Jun 3, 2024

I will give it some more thought.

I will leave this issue open as a reminder.

@jfjlaros jfjlaros added the enhancement New feature or request label Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants