Using try/catch to condition on losing Arduino connection, then establishing a new one? Code runs in command window, but not in function?

6 views (last 30 days)
I have an experimental setup using a Matlab connection to an Arduino to control some motors. There are times I need to take data continuously for a long time (sometimes ~12 hrs) and I get an error stating the Arduino board has lost connection:
"The host and client connection is lost. Make sure the board is plugged in and recreate arduino and related objects."
identifier: 'MATLAB:arduinoio:general:connectionIsLost'
This results in the Matlab script terminating, and my data (not to mention my time) being lost.
I now want to condition on this error occuring with try/catch statements, re-establsh the Arduino connection, then continue taking data.
If I try to call the same function I used to establish the initial Arduino connection
dev = arduino_setup_function();
function [dev] = arduino_setup_function()
...
a = arduino(COM_Port, 'Uno', 'Libraries', 'SPI');
dev = spidev(a, SYNC,'mode', 1,'bitorder','msbfirst','bitrate',500000);
...
end
I get the error:
"MATLAB connection to Arduino Uno at COM10 exists in your workspace. To create a new connection, clear the existing object."
identifier: 'MATLAB:arduinoio:general:connectionExists'
Okay, so now I attempt to clear/disconect my Arduino initial connection. I initially tried clear dev as the obvious choice, but if I run my arduino_setup_function(), I get the same error. After reading through some other posts, I found a way to clear the Arduino connection using:
clear dev
if ~isempty(instrfind)
fclose(instrfind);
delete (instrfind);
end
clear arduino
Note: dev is the arduino.spidev connection which is the variable I input to other fucntions like writeRead() for SPI communication and is therefore listed in my workspace, instrfind is a Matlab function that returns all valid serial port objects, and thus I am deleting those objects/connections, arduino is not a variable, nor is it listed in my workspace (I believe it is a object class, but I'm not 100% sure), but clear arduino is necessary to completely clear the Arduino connection
After typing the above lines in the command window, then calling dev = Arduino_setup(); again, it works. I am able to establish a new Arduino connection.
However, when I use this same code in the function that is actually acquiring my data as follows:
function data_acquisition_function() % function I am using to acquire data
...
while % condition on while I am trying to acquire data
try
arduino_output_function(); % function that tells the Arduino to move the motors
catch ME
if strcmp(ME,'MATLAB:arduinoio:general:connectionIsLost') == 1 % condition on if I get the initial error i.e. if the Arduino connection was lost
% perform the steps to clear the Arduino connection
clear dev
if ~isempty(instrfind)
fclose(instrfind);
delete (instrfind);
end
clear arduino
dev = arduino_setup_function(Vmax,Vref,COM_Port); % call my arduino setup function to establish a new Arduino connection
end
end
... % code that records data
end
....
end
I now get the folowing error when calling my arduino_setup_function() :
"Failed to open a connection at serial port COM10. Make sure the port or address is correct and the Arduino hardware is properly
plugged in. For troubleshooting, see Arduino Hardware Troubleshooting."
identifier: 'MATLAB:arduinoio:general:openFailed'
This is bizzare since if I run the same lines of code folowing the first if statement up to calling arduino_setup_function() in the command window, it works fine. Even more bizzare if I run the code with a breakpoint after the first if statement, the code pauses, then if I hit continue, it works fine.
I don't want to sit by my computer for 12hrs waiting to for the catch statement to hit and the breakpoint to pause the code, just for me to push continue. I am trying to figure out a way to establish the new Arduino connection.
I have tried using try/catch statements to condition on this openFailed error after trying to call my arduino_setup_function(), pausing, and trying again thinking it was somehow the added time from the breakpoint that allowed the code to run.
Does anyone have any idea why this code will not run in the function, but runs in the command window? Also does anyone know why clear arduino is necessary here (this may be related)?
This is a bit of a convoluted question, so if you need more information, plase let me know.
Any suggestions are very much appreciated.
Thanks

Answers (0)

Categories

Find more on Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!