MATLAB Answers


RS232 communication code not working for controlling a 3 axis motor driver.

Asked by JT
on 21 Jan 2013


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
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);
XL = fscanf(serialport);
YL = fscanf(serialport);
ZL = fscanf(serialport);
XH = fscanf(serialport);
YH = fscanf(serialport);
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);
% 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);
XR = fscanf(serialport);
YR = fscanf(serialport);
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);
disp (['Voltage has been set to ' num2str(voltage) ' volts.'])


I am not able to write the voltages to the controller. I believe is in the last if and else statement is the problem. I am trying to control MDT693A 3 axis piezo controller. so far what I have obtained is that the index exceeds matrix dimensions, but I just don't see how.

Hi, can I confirm that this code actually worked for you? There seems to be an issue with the terminators used.

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



No products are associated with this question.

1 Answer

Answer by Walter Roberson
on 22 Jan 2013
 Accepted answer

Do you need to include a space between the 'XV' and the number? Try

fprintf(serialport, 'XV %f\n', voltage);


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!