TCP/IP Callback Function Not Being Triggered Unless a BreakPoint is Placed In Callback Function.

조회 수: 2 (최근 30일)
I have a script that connects two locally networked machines via TCP/IP. The goal is to send information from one to the other. I wrote a simple script to test my understanding of the process, and am getting weird results.
Essentially, my BytesAvailableFcn callback is ONLY being called if I have an active breakpoint in it - otherwise, the callback is ignored.
My test script is:
t = tcpip(IP_Address,PortNumber,'NetworkRole','client')
t.BytesAvailableFcnMode = 'byte'
t.BytesAvailableFcn = @(a,b) RXDATA(a,b,t);
fopen(t)
function [data] = RXDATA(obj, EventData, t)
try
fprintf('I AM HERE!\n')
data.clock = EventData.Data.AbsTime;
data.Value = fscanf(t,'%s');
disp(data)
assignin('base','data',data)
catch
fprintf('I BROKE!\n');
end
end
I do not believe there to be any problems on the server side, as I am able to manually use fscanf in the command line to pull the sent data. I am also able to see that "BytesAvailable" in t is non zero.
The only difference between when this callback works and when it doesn't seems to be when a breakpoint is placed in the callback. I haven't experienced a breakpoint changing the operation of a function before, so any insight would be greatly appreciated.

답변 (1개)

Sourav Ghai
Sourav Ghai 2019년 10월 23일
Hi,
First you need to specify the value for 'BytesAvailableFcnCount'.
For example:
t.BytesAvailableFcnMode = 'byte'
t.BytesAvailableFcnCount = 5;
This basically means that the moment input buffer will have 5 bytes of data, the 'BytesAvailableFcn' callback function will get triggered.
Code for client side:
IP_Address = 'localhost';
PortNumber = 30000;
t = tcpip(IP_Address,PortNumber,'NetworkRole','client')
t.BytesAvailableFcnCount = 5;
t.BytesAvailableFcnMode = 'byte'
t.BytesAvailableFcn = @(a,b) RXDATA(a,b,t);
fopen(t);
function [data] = RXDATA(obj, EventData, t)
try
disp('1234')
data.clock = EventData.Data.AbsTime;
data.Value = 1:25;
disp(data)
assignin('base','data',data)
catch
fprintf('6789\n');
end
end
Code for server side:
t = tcpip('0.0.0.0', 30000, 'NetworkRole', 'server');
fopen(t);
data = 1:20;
fwrite(t,data);
fclose(t);
delete(t);
clear t;

카테고리

Help CenterFile Exchange에서 Development Computer Setup에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by