-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjAER_matlab_live.m
59 lines (56 loc) · 2.28 KB
/
jAER_matlab_live.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
% Receives events continuosly from jaer AEViewer which sends them
% using AEUnicastOutput on default port 8991 on localhost.
% Requires InstrumentControlToolbox.
% In AEViewer, use menu item File/Remote/Enable AEUnicastOutput and
% select jAER Defaults for the settings, but set the buffer size to 8192
% bytes which is the maximum buffer supported by matlab's udp.
% TODO not yet really working, should use async reading with callback
port=7941; % default port in jAER for AEUnicast connections
bufsize=8192; %81
eventsize=8;
count = 0;
r1 = zeros(2000,2000);
r2 = zeros(2000,2000);
try
fprintf('opening datagram input to localhost:%d\n',port);
u=udp('localhost','localport',port,'timeout',1,'inputbuffersize',bufsize,'DatagramTerminateMode','on');
fopen(u);
lastSeqNum=0;
while 1,
% raw=fread(u);
if(u.BytesAvailable==0),
pause(.1);
continue;
end %
raw=fread(u,u.BytesAvailable/4,'int32');
if ~isempty(raw),
seqNum=raw(1); % current broken size we cannot easily convert to int from array of bytes,and we cannot know size to read if reading synchronously so we can't use fread's conversion capability. lame.
if(seqNum~=lastSeqNum+1), fprintf('dropped %d packets\n',(seqNum-lastSeqNum)); end
lastSeqNum=seqNum;
% fprintf('%d bytes\n',length(b));
if length(raw)>2,
allAddr=raw(2:2:end); % addr are each 4 bytes (uint32) separated by 4 byte timestamps
allTs=raw(3:2:end); % ts are 4 bytes (uint32) skipping 2 bytes after each
end
%fprintf('%d events\n',length(allAddr));
%fprintf('%d events\n',length(raw));
fprintf('%d \nAddress\n', allAddr);
fprintf('%d Time Stamps\n', allTs(1));
count = count + 1;
fprintf('%d total frames\n', count);
[a1,m1] = size(allAddr);
[a2,m2] = size(allTs);
r1(1:a1,count) = allAddr(:,1); % each column contains events against one frame
r2(1:a2,count) = allTs(:,1); % each column contains time_stamps against one frame
%events
end
pause(.1);
end
catch ME
ME
fclose(u);
%save r1
%save r2
delete(u);
clear u
end