Matlab serial port opening arduino

1 view (last 30 days)
Cory Morales
Cory Morales on 7 Jul 2015
Answered: Vinod on 8 Aug 2015
Hello
I am trying to get a program to open the serial port to access an arduino with sensors attached. It seems to only want to run about 1/6th of the time and only every other day. When it doesnt work, it gives the warning: Unsuccessful read: A timeout occurred before the Terminator was reached. Is there a way to get it to run more consistently?
Here is the part of the code that is causing in the issue:
function initializeSerial()
global serial_;
global baud_rate_;
global size_data_array_;
disp('Initializing serial communication...')
set(serial_,'Baudrate',baud_rate_);
set(serial_,'DataBits', 8);
set(serial_,'StopBits', 1);
fopen(serial_)
serial_.ReadAsyncMode = 'continuous';
disp('Waiting for serial to come online...');
pause(3); % make sure arduino boots up if reset on connection
count = 0;
for i=1:10
serial_line = fscanf(serial_, '%s')
end
while length(serial_line) <= 1 % test to make sure lines are being read
serial_line = fscanf(serial_,'%s')
count = count + 1;
if count > 1000
disp('Cannot connect to serial port... exiting');
return
end
end
% get size of data array
size_data_array_ = size(eval([ '[', serial_line, ']' ]),2);
end
Here are the variables and parameters:
%***** GLOBAL VARIABLES *****%
global serial_; % controlls serial communications
global baud_rate_; % baud rate for serial communication
global num_readings_; % the number of sensor readings to take for each data point
global num_data_pts_; % the number of data points that will be collected in each trial
global size_data_array_; % the size of the data array coming over serial
global data_; % the variable that holds the collected data
%***** TEST PARAMETERS *****%
serial_ = serial('/dev/ttyACM0');
baud_rate_ = 115200;
num_readings_ = 10;
num_data_pts_ = 2;
num_trials_ = 10;

Answers (1)

Vinod
Vinod on 8 Aug 2015
Any reason you aren't using the Arduino IO Support Package?

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!