% --- Executes just before untitled is made visible.
function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to untitled (see VARARGIN)
% Choose default command line output for untitled
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
if not(libisloaded('zauxdll'))%加载函数库
loadlibrary('zauxdll.dll','zauxdll2.h');
end
disp("加载函数库");%命令行打印
global g_handleptr;%定义连接句柄
global g_nAxis;%定义轴号
global g_Dir;%定义运动方向
global g_Moveway;%定义运动方式
g_handleptr= libpointer('voidPtrPtr');
g_Dir=1;
g_Moveway=0;
g_nAxis=0;
4.编写“连接控制器”按钮程序,调用接口“ZAux_OpenEth()”,使点击按钮时链接控制器。
% --- Executes on button press in pushbutton_openZmc.
function pushbutton_openZmc_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_openZmc (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.timer = timer('Period',0.05,'ExecutionMode','FixedRate',...
'TimerFcn',{@UpdateSliderData, handles});%定时器
global g_handleptr;%定义连接句柄
ip =get(handles.edit_Ip,'String');
disp("连接控制器:"+ip);
zmc_ip = char(ip);
[res,~] = calllib('zauxdll','ZAux_OpenEth',zmc_ip ,g_handleptr);
commandCheckHandler("ZAux_OpenEth",res);
if res==0
fprintf('连接控制器成功\n');
set(gcf,'NumberTitle', 'off', 'Name', '连接成功');
%msgbox('Connection successful');
start( handles.timer );%启动定时器
else
fprintf('连接控制器失败,错误码%d\n',res);
set(gcf,'NumberTitle', 'off', 'Name', '连接失败');
msgbox('Connection failure,Please check the IP!');%连接控制器失败,请检查IP地址
return;
end
5.主界面右键添加销毁函数,调用“ZAux_Close()”关闭连接。
% --- Executes during object deletion, before destroying properties.关闭连接
function figure1_DeleteFcn(hObject, eventdata, handles)
global g_handleptr;%定义连接句柄
calllib('zauxdll','ZAux_Close',g_handleptr);
disp("关闭与控制器连接");%打印
6.通过定时器更新控制器轴状态:当前坐标、当前速度等。
function UpdateSliderData(obj, events, handles)
global g_handleptr;%定义连接句柄
global g_nAxis;%定义轴号
[res,~,runstate]=calllib('zauxdll','ZAux_Direct_GetIfIdle',g_handleptr,g_nAxis, 0);
commandCheckHandler("ZAux_Direct_GetIfIdle",res);
[res,~,curpos]=calllib('zauxdll','ZAux_Direct_GetDpos',g_handleptr, g_nAxis, 0);
commandCheckHandler("ZAux_Direct_GetDpos",res);
[res,~,curspeed]=calllib('zauxdll','ZAux_Direct_GetVpSpeed',g_handleptr, g_nAxis, 0);
commandCheckHandler("ZAux_Direct_GetVpSpeed",res);
str_curpos=num2str(curpos);
str_curspeed=num2str(curspeed);
if (runstate==0)
set(handles.edit_runstate,'String',"当前运动状态:运行");
else
set(handles.edit_runstate,'String',"当前运动状态:停止");
end
set(handles.edit_curpos,'String',"当前轴坐标:" +str_curpos);
set(handles.edit_curspeed,'String',"当前轴速度:" +str_curspeed);
7.通过启动按钮的事件处理函数来设置轴参数并开始运动。
% --- Executes on button press in pushbutton_runmove.运动
function pushbutton_runmove_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_runmove (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global g_handleptr;%定义连接句柄
global g_nAxis;%定义轴号
global g_Dir;%定义运动方向
global g_Moveway;%定义运动方式
calllib('zauxdll','ZAux_Direct_SetAtype',g_handleptr, g_nAxis,0); calllib('zauxdll','ZAux_Direct_SetUnits',g_handleptr,g_nAxis,str2num(get(handles.edit_units,'String')));
calllib('zauxdll','ZAux_Direct_SetLspeed',g_handleptr, g_nAxis, str2num(get(handles.edit_lspeed,'String')));
calllib('zauxdll','ZAux_Direct_SetSpeed',g_handleptr, g_nAxis, str2num(get(handles.edit_speed,'String')));
calllib('zauxdll','ZAux_Direct_SetAccel',g_handleptr, g_nAxis, str2num(get(handles.edit_acc,'String')));
calllib('zauxdll','ZAux_Direct_SetDecel',g_handleptr, g_nAxis, str2num(get(handles.edit_dec,'String')));
calllib('zauxdll','ZAux_Direct_SetSramp',g_handleptr, g_nAxis, str2num(get(handles.edit_sramp,'String')));
if (g_Moveway) %//连续运动
calllib('zauxdll','ZAux_Direct_Single_Vmove',g_handleptr, g_nAxis,g_Dir);
else % //寸动
calllib('zauxdll','ZAux_Direct_Single_Move',g_handleptr, g_nAxis, g_Dir * str2num(get(handles.edit_step,'String')));
end
8.通过停止按钮的事件处理函数来停止插补运动。
% --- Executes on button press in pushbutton_stopMove.单轴停止
function pushbutton_stopMove_Callback(hObject, eventdata, handles)
global g_handleptr;%定义连接句柄
global g_nAxis;
calllib('zauxdll','ZAux_Direct_Single_Cancel',g_handleptr,g_nAxis,2);