How to use parallel computing for a BytesAvailableFnc of a serial object?

1 view (last 30 days)
Hi,
I am using BytesAvailableFcn to read a specific number of bytes from a serial input. On the same time I am using videoinput to record a video and also evaluate the values of the serial input. Because there is so much happening at the same time, the serial bytes are not always read in when they hit the specific number of bytes, so there is a delay.
I want to use the Parallel Computing Toolbox to make the BytesAvailableFcn a background function, which reads the bytes without waiting for other task to finish. How or where can I specify the function as parfeval? Or can I just use parfeval somewhere at the beginning and the function will be compute in the background?
Thank you

Accepted Answer

Harald
Harald on 30 Jun 2023
Hi Oliver,
you could use the backgroundPool function. This even works without Parallel Computing Toolbox. As soon as you run the parfeval command (you can do so "somewhere at the beginning"), the evaluation of that command starts, and further commands will be run at the same time.
Best wishes,
Harald
  2 Comments
Oliver Klein
Oliver Klein on 18 Jul 2023
Edited: Oliver Klein on 18 Jul 2023
So even if I use the BytesAvailableFcn from a serial object, parfeval still works? Or do I have to change my code so there is a background function which starts to read out the serial puffer and doesnt stop. The function which is given to parfeval will be called directly doesnt it?
So is the best version to make the serial object and after that call an independent function through parfeval(backgroundPool, @function) which includes an while(1) and always read out the input buffer of the serial object?
Or is the best version just to call parfeval(backgroundPool, @BytesAvailableFnc) before or after the initialization of the serial object?
I hope my questions are understandable...
Harald
Harald on 18 Jul 2023
Edited: Harald on 18 Jul 2023
Hi Oliver,
let's put it that way: if you have something that does not work, then please share a code example for it and the problematic behavior (e.g., the full error message). At least for me, that is easier to follow than a lot of words.
In general I would say that the safest way of making sure that the serial input object is available in the function you pass to parfeval is to create it inside that function.
Best wishes,
Harald

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!