TCP/IP Callback Function Not Being Triggered Unless a BreakPoint is Placed In Callback Function.
1 次查看(过去 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.
0 个评论
回答(1 个)
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;
For more infromation you can refer https://www.mathworks.com/help/instrument/bytesavailablefcn.html.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Development Computer Setup 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!