RS232 communication code not working for controlling a 3 axis motor driver.
Afficher commentaires plus anciens
Hello,
I am new to matlab, so help me out a bit here. I have a 3 axis controller (X,Y,Z) that I am trying to program using Matlab via RS232. So I know how to open the port and set the settings to the port. What I don't know, or I am having trouble understanding is how to send the commands to control each axis. According to the manufacturer the command to set the output for the X,Y and Z axis are XV, YV, and ZV. However, I am getting an error in my last section of the code when setting the voltages.
clear all
clc
priorPorts = instrfind ;%
delete(priorPorts); %
serialport = serial('COM1')
set(serialport, 'BaudRate', 115200)
set(serialport, 'DataBits', 8)
set(serialport, 'Parity', 'none')
set(serialport, 'StopBits', 1)
set(serialport, 'FlowControl', 'none')
fopen (serialport)
% SET Voltage Limits for each axis
minvoltage = 0
maxvoltage = 75
fprintf(serialport,['XL' num2str(minvoltage)]);
out = fscanf(serialport);
fprintf(serialport,['YL' num2str(minvoltage)]);
out = fscanf(serialport);
fprintf(serialport,['ZL' num2str(minvoltage)]);
out = fscanf(serialport);
fprintf(serialport,['XH' num2str(maxvoltage)]);
out = fscanf(serialport);
fprintf(serialport,['YH' num2str(maxvoltage)]);
out = fscanf(serialport);
fprintf(serialport,['ZH' num2str(maxvoltage)]);
out = fscanf(serialport);
fprintf(serialport,'XL?');
XL = fscanf(serialport);
fprintf(serialport,'YL?');
YL = fscanf(serialport);
fprintf(serialport,'ZL?');
ZL = fscanf(serialport);
fprintf(serialport,'XH?');
XH = fscanf(serialport);
fprintf(serialport,'YH?');
YH = fscanf(serialport);
fprintf(serialport,'ZH?');
ZH = fscanf(serialport);
if str2num(XL(1,2)) ~= minvoltage | str2num(YL(1,2)) ~= minvoltage | str2num(ZL(1,2)) ~= minvoltage | str2num(XH(1,2:3)) ~= maxvoltage | str2num(YH(1,2:3)) ~= maxvoltage | str2num(ZH(1,2:3)) ~= maxvoltage,
msgbox ('Error Setting All Voltages - Disconnecting Piezo Controller');
closepiezocontroller (serialport);
end
% Set individual voltage levels
% XV sets the output to the X-axis.
% YV sets the output to the X-axis.
% ZV sets the output to the X-axis.
% XR?, YR?, ZR? reads the output.
voltage = 1
fprintf(serialport,['XV' num2str(voltage)]);
fprintf(serialport,['YV' num2str(voltage)]);
fprintf(serialport,['ZV' num2str(voltage)]);
out = fscanf(serialport);
fprintf(serialport,'XR?')
XR = fscanf(serialport);
fprintf(serialport,'YR?')
YR = fscanf(serialport);
fprintf(serialport,'ZR?')
ZR = fscanf(serialport);
if length(XR) < 8 || length(YR) < 8 || length(ZR) < 8 || str2num(XR(1,3:8))...
>(voltage+1.5) || str2num(XR(1,3:8)) < (voltage-1.5)...
|| str2num(YR(1,3:8)) >(voltage+1.5)...
|| str2num(YR(1,3:8)) < (voltage-1.5)...
|| str2num(ZR(1,3:8))> (voltage+1.5)...
|| str2num(ZR(1,3:8)) < (voltage-1.5),
msgbox ('Error: All Voltages Could Not Be Set - Disconnecting Piezo Controller');
closepiezocontroller (serialport);
else
disp (['Voltage has been set to ' num2str(voltage) ' volts.'])
end
4 commentaires
Walter Roberson
le 21 Jan 2013
What error are you encountering?
Jean
le 19 Sep 2013
Hy All, I tried this code using what I believe is the same Thorl... controler, but I have errors even in the firsts fprintf. It looks like there is indeed terminaison issues. Did anyone made any progress on this ? Best
Réponse acceptée
Plus de réponses (0)
Communautés
Plus de réponses dans Power Electronics Control
Catégories
En savoir plus sur MATLAB Support Packages dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!