如何在MATLAB GUI中定义回调函数来接收TCP / IP数据流?

时间:2018-04-04 11:01:34

标签: matlab callback matlab-guide tcp-ip matlab-gui

我编写了以下简单的GUI来执行此操作,但仍然没有执行回调函数(RXDATA)我想用来从远程计算机接收数据。

function com_test_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 com_test (see VARARGIN)
handles.t_rzm = tcpip('169.254.181.51', 51000);
set(handles.t_rzm,'ByteOrder','littleEndian');
fopen(handles.t_rzm);
handles.t_rzm.BytesAvailableFcn = @RXDATA;
% Choose default command line output for com_test
handles.output = hObject;
% the remote computer is sending the data
% Update handles structure
guidata(hObject, handles);
function RXDATA(hObject, eventdata, handles) % The callback function supposed to get data
% handles = guidata(hObject);
disp('yes')
dum_PB_Callback(handles.dum_PB,event,dat)
guidata(handles.dum_PB,handles) %% function ends
guidata(hObject, handles);

这是将命令发送到远程PC中的应用程序以选择数据项的子程序功能

我还定义了一个虚拟按钮来触发RXDATA功能,但它没有帮助。

% --- Executes on button press in dum_PB. (Dummy Push Button)
function dum_PB_Callback(hObject, eventdata, handles)
% hObject    handle to dum_PB (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
disp('dum_PB_Callback')
% if handles.UserData.sync==1
handles.t_rzm.BytesAvailableFcn = @RXDATA;
% end
guidata(hObject,handles) %% dummy button function ends

1 个答案:

答案 0 :(得分:0)

我想我能看出你的问题。 我会强调错误或添加代码。

检查以下代码。

handles.t_rzm = tcpip('169.254.181.51', 51000);

set(handles.t_rzm,'ByteOrder','littleEndian');

handles.t_rzm.BytesAvailableCnt = (Your Data Size)

handles.t_rzm.BytesAvailableMode = (Your Data Type)

handles.t_rzm.BytesAvailableFcn = @RXDATA;

fopen(handles.t_rzm);

function RXDATA(hObject, eventdata) // ← Only 2 Parameters 

然后每当接收数据时,回调RXDATA函数。

如果要使用句柄等参数,

handles.t_rzm.BytesAvailableFcn = {@RXDATA,handles};

然后

function RXDATA(hObject, eventdata, handles)