-
Notifications
You must be signed in to change notification settings - Fork 1
/
appendCRC16.m
57 lines (46 loc) · 1.15 KB
/
appendCRC16.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
function myans=appendCRC16(byte_array)
[low,high]=CRC16(byte_array);
myans=[byte_array, low, high];
end
function [low_byte, hi_byte]=CRC16(decinput)
poly16='8005';
poly16=[1 convert2arr(dec2bin(hex2dec(poly16),16))];
poly=poly16;
register=zeros(1,length(poly)-1);
for kk=1:length(decinput)
bin=convert2arr(dec2bin(decinput(kk),8));
bin=flip(bin);
message=bin;
if kk==length(decinput)
message=[message zeros(1,length(poly)-1)];
end
while ~isempty(message)
pop=register(1);
register=[register(2:end) message(1)];
message=message(2:end);
if pop==1
register=register+poly(2:end);
for jj=1:length(register)
register(jj)= mod(register(jj),2);
end
end
end
end
register=flip(register);
str=convert2str(register);
byte=bin2dec(str);
hi_byte=bin2dec(str(1:8));
low_byte=bin2dec(str(9:16));
function mystr=convert2str(arr)
mystr='';
for ii=1:length(arr)
mystr=[mystr num2str(arr(ii))];
end
end
function arr=convert2arr(str)
arr=[];
for ii=1:length(str)
arr=[arr str2num(str(ii))];
end
end
end