Skip to content

Instantly share code, notes, and snippets.

@nag92
Created December 9, 2017 14:37
Show Gist options
  • Save nag92/cf6d47dff49db2f5af2730a047e16fdb to your computer and use it in GitHub Desktop.
Save nag92/cf6d47dff49db2f5af2730a047e16fdb to your computer and use it in GitHub Desktop.
clear all
obj = udp('localhost',9876) ;
obj.ByteOrder = 'littleEndian';
fopen(obj)
data = zeros(15,1,'single');
time = 100;
for i = 1:2
packet = single2bytes(37,data);
fwrite(obj,packet,'uint8');
tic
A = fread(obj,1)
toc
end
function thing = single2bytes(code, val)
% Create the array of bytes
returnArray=uint8(zeros((length(val)+1)*4,1));
%Create the 4 byte control code
tmp1 = typecast(int32(code), 'uint8');
for j=1:4
returnArray(j)=tmp1(j);
end
%disp('Code: ')
%disp(code)
%disp(tmp1)
for i=1:length(val)
%Convert value to a 4 byte array
tmp = typecast(single(val(i)), 'uint8');
%Copy 4 byte data to the main array
for j=1:4
returnArray((i*4)+j)=tmp(j);
end
end
thing = returnArray;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment