Matlab Solenoid Example Program

Cut and paste this example program in to a blank m-file in Matlab, then save it on your engin website with the name of your group and instrument (remember, Matlab allows only alphanumeric characters and no spaces before the ".m"). Modify the program (including the commented upper section) to fit with your hardware.

% program solenoid.m
% opens the COM1 serial port and sends a series of commands to a stepper
% motor at location S2 on a Pontech S200B controller board
% written 27 October 2005 by E. Carr Everbach
clear % clear all previous variables
ncount = 1;
ser = serial('COM1');   % establish connection between Matlab and COM1
set(ser, 'Terminator', 'CR'); % set communication string to end on ASCII 13
set(ser, 'BaudRate', 9600);
set(ser, 'StopBits', 1);    % Pontech controllers ask for these parameters
set(ser, 'DataBits', 8);
set(ser, 'Parity', 'none');
fopen(ser); % open the serial port connection
fprintf(ser,'BD0\n'); % address the controller board (assume only one of them)
fprintf('servo controller is on-line.\n');
nsolenoids = input('Enter number of solenoids to test: ');
for index = 1:nsolenoids
    fprintf(ser,'SV%d M0\n', index); % write out initialization commands
end
while (ncount)
    ncount = input('Enter number of in/out pulses to each solenoid (0 to quit): ');
    for index = 1:ncount
        for yndex = 1:nsolenoids % for each solenoid
            fprintf(ser,'PS%d\n', yndex);
            pause(0.2);
            fprintf(ser,'PC%d\n', yndex);
            pause(0.5)
        end
    end
end
disp('End of program');
fclose(ser);

Matlab Servo Example Program

Cut and paste this example program in to a blank m-file in Matlab, then save it on your engin website with the name of your group and instrument (remember, Matlab allows only alphanumeric characters and no spaces before the ".m"). Modify the program (including the commented upper section) to fit with your hardware.

% program servo.m
% opens the COM1 serial port and sends a series of commands to a servo
% motor at location S1 on a Pontech S200B controller board
% written 3 October 2005 by E. Carr Everbach
clear % clear all previous variables
ser = serial('COM1');   % establish connection between Matlab and COM1
set(ser, 'Terminator', 'CR'); % set communication string to end on ASCII 13
set(ser, 'BaudRate', 9600);
set(ser, 'StopBits', 1);    % Pontech controllers ask for these parameters
set(ser, 'DataBits', 8);
set(ser, 'Parity', 'none');
fopen(ser); % open the serial port connection
fprintf(ser,'BD0 SV1\n'); % write out initialization commands
fprintf('servo controller is on-line.\n');
fprintf('Hit space to see servo toggle through its range\n');
pause
fprintf('M255\n');
fprintf(ser,'M255\n');
pause(1)
fprintf('M1\n');
fprintf(ser,'M1\n');
pause(1)
position = 1; % variable to hold angular position info
while (position)
    position = input('Enter integer position (1-255) or 0 to quit: ');
    fprintf(ser,'M%d\n',position);
    fprintf('M%d\n',position);
end
disp('End of program; servo off');
fclose(ser);

For more information about communicating in Matlab with data acquisition devices, serial ports, etc., visit http://www.swarthmore.edu/NatSci/ceverba1/Class/MatlabDAQprocs.html