image thumbnail
from ADAM Connect by Brett Kuyper
Connect an Advantech ADAM 4520 and 4017 to MATLAB through the serial port and displays.

adam_connect.m
%% Script to open the serial port and begin the retrieval of data.
% Open serial port
close
clear
s=serial('/dev/ttyS0');
set(s,'Terminator','CR');
%% Connect to ADAM4017 and configure
fopen(s);
fprintf(s,'%0202OB0600');  %#ok<CTPCT>

pause

%%Create matrix in which the data is displayed
figure
xlim([0 35]);
ylim([50 500]);
%xlabel('Number of samples')
%change label names as appropriate.
xlabel('Time (min)');
ylabel('Voltage (mV)');
title('Current Plot');
grid; hold on 

%% Collect Data from ADAM4017
fout=datestr(clock);
tic;
for j=1:4200 %(runs for 35 minutes)
    fprintf(s,'#020'); %writes the command to the ADAM to send data from Channel 0
    g=fscanf(s,'%s'); % Reads the data as a string
    g=g(3:end); % Cuts the first two Characters and leaves the numbers   
    out(1,j)=toc/60;  % counts the time from tic
    out(2,j)=str2num(g); % converst the string to a number and saves to the second row of out
    plot(out(1,:),out(2,:)); %% Plot Data on fixed set of axies...         
    pause(0.5)
end

%% Write the data to file...
% clear unnecessary variables
clear j start g 

save(fout);
saveas(gcf,fout,'jpg');

fclose(s)


Contact us at files@mathworks.com