How can I view arduino Serial monitor data in matlab, when I have 2 arduinos?

49 ビュー (過去 30 日間)
avram alter
avram alter 2019 年 5 月 15 日
編集済み: avram alter 2021 年 11 月 26 日
I have 2 arduino megas running the same program connected to the same computer. What script would allow me to iterate through the serial monitors of both of the arduino data, without losing the data from either one? My arduino code is set to 9600 baud, but I don't want to risk losing any data from one of the arduinos, while the matlab code is outputting the other one? I do not want to save the data in any file, I just want it to be read out into the matlab environment.

採用された回答

Mark Sherstan
Mark Sherstan 2019 年 5 月 17 日
Try creating two serial objects and running something similar to the example below (if you are worried about loosing data you will need to perform some sort of header check). If you want to acquire data at the exact same time you will need to run multiple threads and should look at the parallel computing toolbox.
% Connect to serial port and set properties
serial1 = serial('/dev/cu.usbmodem14101', 'BaudRate', 9600);
serial2 = serial('/dev/cu.usbmodem14101', 'BaudRate', 9600);
fopen(serial1);
fopen(serial2);
while 1
% Get 2 bytes of data from each device
data1 = fread(s, 2);
data2 = fread(s, 2);
% Print the data
fprintf("Data1: %d \t Data2: %d\n",data1,data2)
end
% Close connection(s)
fclose(serial1);
delete(serial1)
clear serial1
fclose(serial2);
delete(serial2)
clear serial2
  2 件のコメント
Lars Urban
Lars Urban 2021 年 11 月 25 日
Hi Mark, thanks for this answere. I know its an old thread but maybe youre still in: I need to read IMU sensor data from 4 IMUs at a reasonable rate. Via the Serial Explorer i could only read one line at a time. Idealy I would get the data at 20Hz (not 100% sure).
I tried your code above but dont understand the "fread(s,2)" part of it. What does s stand for?
avram alter
avram alter 2021 年 11 月 26 日
編集済み: avram alter 2021 年 11 月 26 日
Replace s with the file ID you want to open

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB Support Package for Arduino Hardware についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by