www.pudn.com > SerialGPS.zip > ComputeChecksum.m


function cksum = ComputeChecksum(id, data, type) 
 
if (nargin < 3) 
    type = 'uchar'; 
end 
 
% Compute the 2s complement of the sum of the bytes sent: 
%    DLE, ID, SIZE, DATA 
s = id + (length(data)*sizeof(type)) + sum(data); 
cmp = bitcmp(s, 32); 
cmp = cmp + 1; 
cksum = bitand(cmp, 255);