Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % The sensor data
- sensorNr = [002;002;002;002];
- lengthMsg = {'8';'8';'8';'8'};
- byte1 = {'55';'9A';'EF';'79'};
- byte2 = {'D1';'D2';'D3';'D4'};
- byte3 = {'1F';'1F';'1F';'1F'};
- byte4 = {'81';'81';'81';'81'};
- byte5 = {'00';'00';'00';'00'};
- byte6 = {'00';'00';'00';'00'};
- byte7 = {'00';'00';'00';'00'};
- byte8 = {'00';'00';'00';'00'};
- T = table(sensorNr,byte1,byte2,byte3,byte4,byte5,byte6,byte7,byte8);
- time = [0.01;0.03;0.05;0.07];
- for sensorNr = 002:002
- for timeIdx = 1:size(time,1)
- %% To create a zero vector and add the rearranged binary data to it
- k = 1;
- Big_Binary_Vector = false(1,str2double(cell2mat(lengthMsg(timeIdx)))*8); % To create a default Vector with zeros (64 bits)
- for byte = 1:str2double(cell2mat(lengthMsg(1)))
- byteName = ['byte',num2str(byte)];
- lengthBin = size(hexToBinaryVector(T.(byteName)(timeIdx)),2); % Takes one byte at a time and converts into binary
- One_byte_Vector = false(1,8); % Creates a zero vector to place the binary vector created above
- One_byte_Vector(9-lengthBin:8) = hexToBinaryVector(T.(byteName)(timeIdx));
- Big_Binary_Vector(1+8*(byte-1):byte*8) = (One_byte_Vector); % Places the converted Hex value in the big binary vector of 64 bits
- end
- %% To try out all the possible combinations of binary data and converting them to decimal accordingly
- for StartByte = 1:8
- for StartBit = 1:8
- for SignalLength = 12:4:20 % Iterates for the length 12 then 16 then 20
- time(timeIdx,1) = (time(timeIdx))/1000; % convert from ms into s
- idName = ['id',num2str(sensorNr)];
- Decimal_Combinations.(idName)(timeIdx,k) = bitCombinations(Big_Binary_Vector,StartByte,StartBit,SignalLength); % converts the binary to decimal using the function 'bitCombinations'
- k = k+1;
- end
- end
- end
- end
- end
- function decnum = bitCombinations(ByteArray,Sbyte,Sbit,lengthSignal)
- %function extracts the required bits from a byte array and
- %returns the decimal equivalent of the bits.
- %Inputs:
- %Sbyte - Starting byte
- %Sbit - Starting bit in the given byte
- %length - length of bits to be extracted
- %Output:
- %dec - Returns the dec
- startbit_pos = ((Sbyte-1)*8+Sbit);
- endbit_pos = ((Sbyte-1)*8+Sbit+lengthSignal-1);
- if endbit_pos <= 64
- extractedbits = ByteArray(startbit_pos:endbit_pos);
- extractedbits = fliplr(extractedbits);
- decnum = bi2de(extractedbits);
- %decnum=bin2dec(num2str(extractedbits));
- else
- decnum = NaN;
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement