Receive data from 2 serial devices parallel

4 ビュー (過去 30 日間)
Rob
Rob 2019 年 5 月 10 日
回答済み: Priyank Sharma 2019 年 5 月 14 日
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 :)

回答 (1 件)

Priyank Sharma
Priyank Sharma 2019 年 5 月 14 日
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.

カテゴリ

Help Center および File ExchangeParallel for-Loops (parfor) についてさらに検索

製品


リリース

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by