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 Everbachclear % clear all previous variablesncount = 1;ser = serial('COM1'); % establish connection between Matlab and COM1set(ser, 'Terminator', 'CR'); % set communication string to end on ASCII 13set(ser, 'BaudRate', 9600);set(ser, 'StopBits', 1); % Pontech controllers ask for these parametersset(ser, 'DataBits', 8);set(ser, 'Parity', 'none');fopen(ser); % open the serial port connectionfprintf(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:nsolenoidsfprintf(ser,'SV%d M0\n', index); % write out initialization commandsendwhile (ncount)ncount = input('Enter number of in/out pulses to each solenoid (0 to quit): ');for index = 1:ncountfor yndex = 1:nsolenoids % for each solenoidfprintf(ser,'PS%d\n', yndex);pause(0.2);fprintf(ser,'PC%d\n', yndex);pause(0.5)endendenddisp('End of program');fclose(ser);
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 Everbachclear % clear all previous variablesser = serial('COM1'); % establish connection between Matlab and COM1set(ser, 'Terminator', 'CR'); % set communication string to end on ASCII 13set(ser, 'BaudRate', 9600);set(ser, 'StopBits', 1); % Pontech controllers ask for these parametersset(ser, 'DataBits', 8);set(ser, 'Parity', 'none');fopen(ser); % open the serial port connectionfprintf(ser,'BD0 SV1\n'); % write out initialization commandsfprintf('servo controller is on-line.\n');fprintf('Hit space to see servo toggle through its range\n');pausefprintf('M255\n');fprintf(ser,'M255\n');pause(1)fprintf('M1\n');fprintf(ser,'M1\n');pause(1)position = 1; % variable to hold angular position infowhile (position)position = input('Enter integer position (1-255) or 0 to quit: ');fprintf(ser,'M%d\n',position);fprintf('M%d\n',position);enddisp('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