Receive data from 2 serial devices parallel

17 vues (au cours des 30 derniers jours)
Rob
Rob le 10 Mai 2019
Dear community,
I want to get data from 2 different serial devices (scale & wireless temp-sensor).
To get the weight from the scale I made a timer with a period of 1 sec (receiving data takes about ~0.15 sec).
Also for the temperature sensor I made a timer, but it needs more time to receive the data via fread, so I put the period on 3 sec.
(I guess) The reason for the longer time is the communication between base station and remote unit.
The data I receive from both devices needs to be shown in a GUI (&also to be recorded).
The Timers work fine on their own. --> So if I start only the timer for the scale I receive the live data in my GUI every second.
Also if I only start the timer for the temperature, I get the data every 3 seconds in the GUI.
But if I combine both --> The 3-seconds-timer “blocks” the process and the timer for the scale doesn’t work at all.
I read about the parallel pools, but I couldn’t manage to get the live data from the workers to display it in my GUI.
So how can I receive the live values from both devices?
I’m thankful for any help.
Greets,
Rob
P.S.: If you need any example code or information just let me know :)

Réponses (1)

Priyank Sharma
Priyank Sharma le 14 Mai 2019
Yes, you can use the Parallel Computing Toolbox's SPMD feature to do this. You can have each worker MATLAB thread accessing individual COM ports as seen in the following example.
In this example, the two COM ports are COM1 and COM6. Depending on the return value of the 'labindex' function, each worker will access their respective assigned COM port to read and write from it. Note that this assumes you have two loopback serial connectors connected to your machine.
matlabpool(2)
spmd(2)
if labindex == 1
portID = 1;
else
portID = 6;
end
disp(labindex);
s = serial(['COM' num2str(portID)]);
fopen(s);
fprintf(s, ['Hello from port number ' num2str(portID)]);
out = fscanf(s, '%c');
fclose(s);
delete(s);
end
out{1}
out{2}
matlabpool close
The other alternative is to do time-slicing, where asynchronous reads would be performed using the 'bytesavailablefcn' callback.

Catégories

En savoir plus sur Downloads dans Help Center et File Exchange

Produits


Version

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by