Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. % The sensor data
  2.  
  3. sensorNr = [002;002;002;002];
  4. lengthMsg = {'8';'8';'8';'8'};
  5. byte1 = {'55';'9A';'EF';'79'};
  6. byte2 = {'D1';'D2';'D3';'D4'};
  7. byte3 = {'1F';'1F';'1F';'1F'};
  8. byte4 = {'81';'81';'81';'81'};
  9. byte5 = {'00';'00';'00';'00'};
  10. byte6 = {'00';'00';'00';'00'};
  11. byte7 = {'00';'00';'00';'00'};
  12. byte8 = {'00';'00';'00';'00'};
  13. T = table(sensorNr,byte1,byte2,byte3,byte4,byte5,byte6,byte7,byte8);
  14. time = [0.01;0.03;0.05;0.07];
  15.  
  16. for sensorNr = 002:002
  17. for timeIdx = 1:size(time,1)
  18. %% To create a zero vector and add the rearranged binary data to it
  19.  
  20. k = 1;
  21. Big_Binary_Vector = false(1,str2double(cell2mat(lengthMsg(timeIdx)))*8); % To create a default Vector with zeros (64 bits)
  22.  
  23. for byte = 1:str2double(cell2mat(lengthMsg(1)))
  24. byteName = ['byte',num2str(byte)];
  25. lengthBin = size(hexToBinaryVector(T.(byteName)(timeIdx)),2); % Takes one byte at a time and converts into binary
  26. One_byte_Vector = false(1,8); % Creates a zero vector to place the binary vector created above
  27. One_byte_Vector(9-lengthBin:8) = hexToBinaryVector(T.(byteName)(timeIdx));
  28. 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
  29.  
  30. end
  31.  
  32.  
  33. %% To try out all the possible combinations of binary data and converting them to decimal accordingly
  34.  
  35. for StartByte = 1:8
  36. for StartBit = 1:8
  37. for SignalLength = 12:4:20 % Iterates for the length 12 then 16 then 20
  38. time(timeIdx,1) = (time(timeIdx))/1000; % convert from ms into s
  39. idName = ['id',num2str(sensorNr)];
  40.  
  41. Decimal_Combinations.(idName)(timeIdx,k) = bitCombinations(Big_Binary_Vector,StartByte,StartBit,SignalLength); % converts the binary to decimal using the function 'bitCombinations'
  42. k = k+1;
  43. end
  44. end
  45. end
  46. end
  47. end
  48.  
  49. function decnum = bitCombinations(ByteArray,Sbyte,Sbit,lengthSignal)
  50.  
  51. %function extracts the required bits from a byte array and
  52. %returns the decimal equivalent of the bits.
  53.  
  54. %Inputs:
  55. %Sbyte - Starting byte
  56. %Sbit - Starting bit in the given byte
  57. %length - length of bits to be extracted
  58.  
  59. %Output:
  60. %dec - Returns the dec
  61.  
  62. startbit_pos = ((Sbyte-1)*8+Sbit);
  63. endbit_pos = ((Sbyte-1)*8+Sbit+lengthSignal-1);
  64.  
  65. if endbit_pos <= 64
  66. extractedbits = ByteArray(startbit_pos:endbit_pos);
  67. extractedbits = fliplr(extractedbits);
  68. decnum = bi2de(extractedbits);
  69. %decnum=bin2dec(num2str(extractedbits));
  70. else
  71. decnum = NaN;
  72. end
  73.  
  74. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement