TCP/IP Callback Function Not Being Triggered Unless a BreakPoint is Placed In Callback Function.
2 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Sourav Ghai
on 23 Oct 2019
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 Comments
See Also
Categories
Find more on Development Computer Setup in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!