Hello everyone,
I want send packets across the serial port to my base with matlab, and to do
that I need to put CRC value like the calculated by the motes. I know what
is the algorithm that the motes uses to find this CRC:

public static int calcByte(int crc, int b)
{
         crc = crc ^ (int)b << 8;
         for (int i = 0; i < 8; i++)
         {
                  if ((crc & 0x8000) == 0x8000)
                  crc = crc << 1 ^ 0x1021;
                  else
                  crc = crc << 1;
         }
         return crc & 0xffff;
}

public static int calc(byte[] packet, int index, int count)
{
         int crc = 0;
         int i;
         while (count > 0)
         {
         crc = calcByte(crc, packet[index++]);
         count--;
         }
         return crc;
}
The polynomial to find this CRC is 0x1021. I try to obtain that CRC with
Matlab but my CRC calculated with the same polynomial doesn't correspond
whith the CRC of the mote. At first glance my Matlab's algorithm does the
same that mote.
% File CRC16.m
%**************************************************************************
% This script calculates the 16-bit ITU-T CRC, as described in 7.2.1.9
% IEEE 802.15.4-2006 std. (ZigBee).
%
% Author: Everton Leandro Alves
% Date: 06/19/2008
%
% The generator polynomial is G(x)=x^16+x^12+x^5+1. From the given
% explanation the steps are:
% 1 - Remainder register 'r' is initialized to 0;
% 2 - The message is shifted into the divider (b0 first);
% 3 - Operations are done in the order: a) XORs, b) left shift of r
% register and c) r3 and r10 update.
% 4 - The r register is appended to the message.
%
%**************************************************************************

clear all
clc
%msg = [66;125;94;0;1;125;93;12;132;2;0;0;0;0;0;0;192;2;132;0];
msg = [132;2;0;0;0;0;0;0;192;2;132;0];
msg = de2bi(msg,8);
msg = reshape(msg,1,8*length(msg));

r=zeros(1,16);      % Remainder register initialization

for c3=1:length(msg)

    s1=bitxor(msg(c3),r(1));    % XOR between r0 and the message bit
    s2=bitxor(s1,r(12));        % XOR r11
    s3=bitxor(s1,r(5));         % XOR r4

    r=[r(2:16) s1];             % Left shift of r, and r15 update

    r(11)=s2;                   % r10 update
    r(4)=s3;                    % r3 update
end

msg_FCS=[msg r];                % Message + FCS field
r                                        %CRC  ##Bytes of CRC from packet of
the mote: 0xFA 0x04

Somebody knows what is the problem? I use the wrong polynomial?

Thanks in advance, regards.
_______________________________________________
Tinyos-help mailing list
[email protected]
https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help

Reply via email to