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.
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://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.