List Arduinos and connect to multiple Simulink

5 ビュー (過去 30 日間)
Min
Min 2024 年 9 月 11 日
コメント済み: Umar 2024 年 9 月 13 日
Hi,
I have 2 questions. 1 is about the com port and 2 is about the setting up the simulink via code.
So I found out that 2024A version has the function to list the Arduino using arduinolist - List available Arduino hardware - MATLAB (mathworks.com) this function.
However, I am currently unable to get 2024a but has 2023b version so I am looking for some suggestions how to make this list automatically so I can utilize. My start idea is to list the comport using the serialportlist("available") to obtain the comport that are connected which was successful but couldn't find a way to obtain the information from the comport to detect what which Arduino is connected to which comport.
Can anyone give me some suggestions?
The second question is about how to connect the Simulink to these each Arduino.
I am able to open the Simulink using the open_system But couldn't get each Simulink to connect to each Arduino. Is there a way to code so that the specific Simulink can connect to a specific Arduino. Any ideas please?
Thanks!

回答 (1 件)

Umar
Umar 2024 年 9 月 12 日

Hi @Min,

Let me address your first question, “So I found out that 2024A version has the function to list the Arduino using arduinolist - List available Arduino hardware MATLAB (mathworks.com) this function. However, I am currently unable to get 2024a but has 2023b version so I am looking for some suggestions how to make this list automatically so I can utilize. My start idea is to list the comport using the serialportlist("available") to obtain the comport that are connected which was successful but couldn't find a way to obtain the information from the comport to detect what which Arduino is connected to which comport.Can anyone give me some suggestions?”

Please see my response to your comments below.

After reviewing the documentation for Arduino provided at the link below.

https://www.mathworks.com/help/matlab/supportpkg/arduino.html?searchHighlight=arduino&s_tid=srchtitle_support_results_1_arduino#mw_c00e99d2-1165-443e-8dce-595596a641a4

Here is how I can address your need for identifying connected Arduino devices in MATLAB 2023b:

Identify Available COM Ports

As you mentioned, you can use the command `serialportlist("available")` to get a list of all available COM ports.

     comPorts = serialportlist("available");

Attempt to Connect and Identify Each Device

You can iterate through the list of COM ports and attempt to create an arduino object for each port. If the connection is successful, you can retrieve the board type associated with that port. Here’s a sample script to help you achieve this:

     % Get available COM ports
     HcomPorts = serialportlist("available");
     % Initialize an array to hold connected devices
     connectedDevices = {};
     % Loop through each COM port
     for i = 1:length(comPorts)
       try
           % Attempt to create an arduino object (specify board type if known)
           a = arduino(comPorts(i), 'Uno'); % Change 'Uno' to your expected board 
   type if necessary
           % If successful, store the information
           connectedDevices{end + 1} = struct('Port', a.Port, 'Board', a.Board);
           % Clear object to avoid connection issues in next iteration
           clear a;
       catch ME
           % Handle connection errors (optional)
           fprintf('Failed to connect on %s: %s\n', comPorts(i), ME.message);
       end
     end
     % Display connected devices
     disp(connectedDevices);

Now, if you are unsure of which types of boards might be connected, you could modify the script to check multiple board types or handle exceptions more gracefully. After running the above code, your connectedDevices variable will contain structures with the port and board type of each connected Arduino device. Please bear in mind that the above method is effective but may require knowledge of what boards you expect to connect. If you want a more dynamic approach, consider checking against known board types using conditional statements within your loop.

Now addressing your second question, “how to connect the Simulink to these each Arduino.I am able to open the Simulink using the open_system But couldn't get each Simulink to connect to each Arduino. Is there a way to code so that the specific Simulink can connect to a specific Arduino. Any ideas please?”

Please see my response to your comments below.

After reviewing the following documentations provided below

https://www.mathworks.com/help/simulink/supportpkg/arduino_ref/communicating-with-arduino-hardware.html

https://m.youtube.com/watch?v=Hgtmy7pH59A&t=23

Here is how you can connect specific Simulink models to designated Arduino board by following the steps mentioned below.

Open the Desired Simulink Model

Use open_system(your_model_name) in MATLAB to open your specific Simulink model.

Configure the Hardware Settings

Go to the Modeling tab. Click on Model Settings (or Configuration Parameters). In the Hardware Implementation pane, select the appropriate Arduino board from the "Hardware board" dropdown list. This step is crucial as it makes sure that the model is set up for the correct hardware specifications.

Establish Communication

Connect your Arduino board to your computer via a USB cable. On the Hardware tab, select Run on board from the Mode section. Choose Monitor & Tune to allow real-time parameter adjustments while the model is running on the Arduino.

Deploying the Model

Click on Build, Deploy & Start. This action compiles your model and uploads it to the connected Arduino board. You should see feedback from any connected sensors or actuators in real-time through scopes in your Simulink model.

Repeat for Other Models/Boards

To connect different models to other Arduino boards, repeat steps 1-4 for each specific model and ensure you change the "Hardware board" parameter accordingly.

Now, if you encounter issues with serial communication (e.g., error messages about TCP/IP ports), check that no other applications are using those ports. Make sure that your Arduino's firmware is up-to-date; outdated firmware can lead to compatibility issues. If using multiple boards simultaneously, make sure each one is uniquely identified and connected through different USB ports. Also, consider using MATLAB’s arduino function if you want a more programmatic approach for reading and writing data without needing a full Simulink model.

Hope this answers your questions.

  5 件のコメント
Min
Min 2024 年 9 月 12 日
Hey Umar, sorry for the another question.
So I modified so that I can encounter multiple Arduinos but I am definitely doing something wrong.
%Auto collect Arduino Test
% Get available COM ports
HcomPorts = serialportlist("available");
% Initialize an array to hold connected devices
connectedDevices = {};
%known arduinos
arduino_list = ["Due", "Uno"];
% Loop through each COM port
for i = 1:length(HcomPorts)
for ii = 1:length(arduino_list)
try
% Attempt to create an arduino object (specify board type if known)
a = arduino(HcomPorts(i), arduino_list(ii)); % Change 'Uno' to your expected board
% Clear object to avoid connection issues in next iteration
clear a;
catch ME
% Handle connection errors (optional)
fprintf('Failed to connect on %s: %s\n', HcomPorts(i), ME.message);
end
end
end
% Display connected devices
disp(connectedDevices);
This is the code I have where it will go through as a loop to try to connect but what it did is trying to connect a wrong port then it skips to try another one then decided to move to another port then fails again.
Here is the error.
>> Autocollect_arduino
Updating server code on board Due (COM8). This may take a few minutes.
Failed to connect on COM8: Cannot program board Due (COM8). Please make sure the board is supported and the port and board type are correct. For more information, see Arduino Hardware Troubleshooting.
Updating server code on board Uno (COM9). This may take a few minutes.
Failed to connect on COM9: Cannot program board Uno (COM9). Please make sure the board is supported and the port and board type are correct. For more information, see Arduino Hardware Troubleshooting.
>> HcomPorts = serialportlist("available");
>> arduino('COM9','Due')
The Due was at 9 not 8.
Umar
Umar 2024 年 9 月 13 日

Hi @Min,

After reviewing the documentation provided at the link below, it does not list any supported hardware for Arduino Uno but does support hardware for Arduino Due.

https://www.mathworks.com/help/simulink/supportpkg/arduino_ref/send-and-receive-serial-data-using-arduino-hardware.html

The error message shared in your comments indicates that the connection attempt was made on the wrong COM port (e.g., COM8 instead of COM9 for the Due) which suggests that the logic for connecting to devices may not be accurately identifying which boards are connected to which ports. Your loop structure correctly iterates through both available COM ports and known Arduino types. However, you need to make sure that you store successfully connected devices in your connectedDevices array. So, modify your code to capture successful connections and check for the correct board type before attempting to clear the object. Here’s an updated version of your code:

% Auto collect Arduino Test
% Get available COM ports
HcomPorts = serialportlist("available");
% Initialize an array to hold connected devices
connectedDevices = {};
% Known arduinos
arduino_list = ["Due", "Uno"];
% Loop through each COM port
for i = 1:length(HcomPorts)
  for ii = 1:length(arduino_list)
      try
          % Attempt to create an arduino object (specify board 
          type if known)
          a = arduino(HcomPorts(i), arduino_list(ii)); 
          % If successful, add to connected devices
          connectedDevices{end+1} = struct('Port', HcomPorts(i), 
          'Board',arduino_list(ii));
          fprintf('Successfully connected to %s on %s\n', 
          arduino_list(ii), HcomPorts(i));
          % Clear object to avoid connection issues in next iteration
          clear a;
      catch ME
          % Handle connection errors (optional)
          fprintf('Failed to connect on %s: %s\n', HcomPorts(i), 
          ME.message);
      end
  end
end
% Display connected devices
disp(connectedDevices);

The revised code now stores successfully connected devices in connectedDevices, allowing you to see which boards were successfully connected and the clear a; command is executed only after a successful connection, to make sure you can debug more effectively if needed. If you have any further questions, please let me know.

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

カテゴリ

Help Center および File ExchangeModeling についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by