JUST GO THROUGH THIS
%% Using MATLAB with Serial Devices (such as RS-232)
% MATLAB supports communication with serial devices including RS-232 when
% using the Instrument Control Toolbox. There are many devices with serial
% interfaces, including gas chronometers, mass spectrometers,
% imaging devices, pulse oximeters, and instruments.
%
% The toolbox provides a graphical tool that allows you to configure,
% control, and acquire data from your serial device without writing code.
% The tool automatically generates MATLAB code that allows you to reuse
% your work. The code example below was automatically generated by the
% tool. Type "tmtool" at the MATLAB command line to launch this tool.
%
% This code example below shows you how you can communicate with your
% serial device using MATLAB. The "*IDN?" command was used which is a
% typical command for communicating with an instrument. The commands you
% can use will depend on what your serial device supports.
%% Automatically generating a report in MATLAB
% Press the "Save and Publish to HTML" button in the MATLAB Editor to
% execute this code example and automatically generate a report of your
% work with the serial device.
%% Automatically generating MATLAB script for your RS-232 device
% To automatically create your own MATLAB script, launch "tmtool". Open the
% "Hardware" node, open the "Serial" node, select your serial port (such
% as COM1, press the "connect" button. Once connected, enter your device
% commands in the right pane, press "Session log" to see the code generated,
% and press "Save Session" to save the code to a MATLAB (.m) file.
%% MATLAB script automatically generated for the RS-232 device
% The following MATLAB script was automatically generated by interacting
% with the device configuration tool provided by the toolbox.
% Creation time: 03-Oct-2006 20:36:43
% Create a serial port object.
obj1 = instrfind('Type', 'serial', 'Port', 'COM3', 'Tag', '');
% Create the serial port object if it does not exist
% otherwise use the object that was found.
if isempty(obj1)
obj1 = serial('COM3');
else
fclose(obj1);
obj1 = obj1(1)
end
% Connect to instrument object, obj1.
fopen(obj1);
% Communicating with instrument object, obj1.
data1 = query(obj1, '*IDN?');
% Disconnect from instrument object, obj1.
fclose(obj1);
% Clean up all objects.
delete(obj1);