CRC

Delphi'de kod yazma ile ilgili sorularınızı bu foruma yazabilirsiniz.
Cevapla
Kullanıcı avatarı
şimal
Kıdemli Üye
Mesajlar: 516
Kayıt: 10 Kas 2003 09:36
Konum: ANKARA

CRC

Mesaj gönderen şimal »

16 bit CRC ile ilgili bilgisi olan varmı?
Elektronik devre şeması var elimde ama çözemedim:(
Bunu delphi ile nasıl yapabilirim?
Kullanıcı avatarı
mege
Admin
Mesajlar: 2360
Kayıt: 05 Şub 2004 04:32
Konum: Beşiktaş
İletişim:

Mesaj gönderen mege »

http://www.efg2.com/Lab/Mathematics/CRC.htm

Esasında çok iyi bir yöntem kullanmamış ama buradan yola çıkabilirsin.
Arkadaş VCL halinde bile hazırlamış bunu :))

Kod: Tümünü seç

 Given the above  lookup table, the code for computing a CRC-16 is as follows (see initialization/finalization below):
PROCEDURE CalcCRC16 (p: pByte; nbyte: WORD; VAR CRCvalue: WORD);
  VAR
    i: WORD;
    q: pByte;

  {The following is a little cryptic (but executes very quickly).
   The algorithm is as follows:
   1. exclusive-or the input byte with the low-order byte of
      the CRC register to get an INDEX
   2. shift the CRC register eight bits to the right
   3. exclusive-or the CRC register with the contents of
      Table[INDEX]
   4. repeat steps 1 through 3 for all bytes}
BEGIN
  q := p;
  FOR i := 1 TO nBYTE DO
  BEGIN
    CRCvalue := Hi(CRCvalue) XOR Table[ q^ XOR Lo(CRCvalue) ];
    INC(q)
  END
END {CalcCRC16};
Başka bir yöntem FASTCRC
http://atlas.csd.net/~cgadd/knowbase/CRC0015.HTM

Kod: Tümünü seç

Contributor: DON PAULSEN              


{
RE:     SWAG submission
        This 16-bit CRC function is compatible with those used in Chuck
        Forsberg's X-modem protocol.  It's very fast, because I unrolled
        the "for (i = 0; i < 8; ++i)" loop.  If a 32-bit CRC is not
        necessary, this is a great alternative because of its speed and
        small size.



{==============================================================}
FUNCTION Crc16 (var buffer; size, seed: word): word; assembler;

{ Set size parameter to 0 to process 64K.  If processing only one buffer, set
  seed parameter to 0 -- otherwise set to result from previous calculation.
  C code translated by Don Paulsen. }

(* This routine is a translation of the following C code by Chuck Forsberg.
   The added "seed" parameter allows for finding the CRC value of data spanning
   multiple buffers.  The innermost loop has been unrolled at a cost of 32
   bytes in code, but the speed increase is nearly two-fold.

     int    Crc16 (ptr, count)
     char   *ptr;
     int    count;

     {   int crc, i;

         crc = 0;
         while (--count >= 0) {
            crc = crc ^ (int)*ptr++ << 8;
            for (i = 0; i < 8; ++i)
                if (crc & 0x8000)
                    crc = crc << 1 ^ 0x1021;
                else
                    crc = crc << 1;
          }
         return (crc & 0xFFFF);
     }
*)

ASM
    les     di, buffer
    mov     dx, size
    mov     ax, seed
    mov     si, 1021h
@next:
    xor     bl, bl
    mov     bh, es:[di]
    xor     ax, bx

    shl  ax, 1;    jnc  @noXor1;    xor  ax, si
@noXor1:
    shl  ax, 1;    jnc  @noXor2;    xor  ax, si
@noXor2:
    shl  ax, 1;    jnc  @noXor3;    xor  ax, si
@noXor3:
    shl  ax, 1;    jnc  @noXor4;    xor  ax, si
@noXor4:
    shl  ax, 1;    jnc  @noXor5;    xor  ax, si
@noXor5:
    shl  ax, 1;    jnc  @noXor6;    xor  ax, si
@noXor6:
    shl  ax, 1;    jnc  @noXor7;    xor  ax, si
@noXor7:
    shl  ax, 1;    jnc  @noXor8;    xor  ax, si
@noXor8:

    inc     di
    dec     dx
    jnz     @next
END;
Hata sezme ile ilgili (Hamming Distance) arkadaşımın okul notları var. istenirse admine gönderebilirim. faydalı bir eser.
mbt
Üye
Mesajlar: 165
Kayıt: 27 Şub 2004 01:23

Mesaj gönderen mbt »

Bu konu hakkında pascal kodu olan bir örnek program var. Turbo Pascal 7.0 ile uyumlu ama delphi'ye de sizler uyarlayabilirsiniz. Belki yardımı olur kanısındayım.

Kod: Tümünü seç

UNIT CRC;

{CRC calculates a cyclic redundancy code (CRC), known as CRC-16, using
  a byte-wise algorithm.

  (C) Copyright 1989, Earl F. Glynn, Overland Park, KS.  Compuserve 73257,3527.
  All Rights Reserved.  This Turbo Pascal 5.5 UNIT may be freely distributed
  for non-commercial use.

  This UNIT was derived from the CRCT FORTRAN 77 program given in
  "Byte-wise CRC Calculations" by Aram Perez in IEEE Micro, June 1983,
  pp. 40-50.  The constants here are for the CRC-16 generator polynomial,
  X^16 + X^15 + X^2 + 1.  While the constants are specific to the
  CRC-16 polynomial, the algorithm is general and will calculate the
  CRC for whatever set of constants is present.

  This CRC algorithm emphasizes speed at the expense of the 512-byte
  lookup table.}

INTERFACE

  PROCEDURE CalcCRC (p:  pointer; nbyte:  WORD; VAR CRCvalue:  WORD);
  PROCEDURE CalcFileCRC (FromName:  STRING; VAR CRCvalue:  WORD;
              VAR IOBuffer:  pointer;  BufferSize:  WORD; VAR error:  WORD);

IMPLEMENTATION

  CONST
    table:  ARRAY[0..255] OF WORD =
     ($0000,$C0C1,$C181,$0140,$C301,$03C0,$0280,$C241,$C601,$06C0,$0780,
      $C741,$0500,$C5C1,$C481,$0440,$CC01,$0CC0,$0D80,$CD41,$0F00,$CFC1,
      $CE81,$0E40,$0A00,$CAC1,$CB81,$0B40,$C901,$09C0,$0880,$C841,$D801,
      $18C0,$1980,$D941,$1B00,$DBC1,$DA81,$1A40,$1E00,$DEC1,$DF81,$1F40,
      $DD01,$1DC0,$1C80,$DC41,$1400,$D4C1,$D581,$1540,$D701,$17C0,$1680,
      $D641,$D201,$12C0,$1380,$D341,$1100,$D1C1,$D081,$1040,$F001,$30C0,
      $3180,$F141,$3300,$F3C1,$F281,$3240,$3600,$F6C1,$F781,$3740,$F501,
      $35C0,$3480,$F441,$3C00,$FCC1,$FD81,$3D40,$FF01,$3FC0,$3E80,$FE41,
      $FA01,$3AC0,$3B80,$FB41,$3900,$F9C1,$F881,$3840,$2800,$E8C1,$E981,
      $2940,$EB01,$2BC0,$2A80,$EA41,$EE01,$2EC0,$2F80,$EF41,$2D00,$EDC1,
      $EC81,$2C40,$E401,$24C0,$2580,$E541,$2700,$E7C1,$E681,$2640,$2200,
      $E2C1,$E381,$2340,$E101,$21C0,$2080,$E041,$A001,$60C0,$6180,$A141,
      $6300,$A3C1,$A281,$6240,$6600,$A6C1,$A781,$6740,$A501,$65C0,$6480,
      $A441,$6C00,$ACC1,$AD81,$6D40,$AF01,$6FC0,$6E80,$AE41,$AA01,$6AC0,
      $6B80,$AB41,$6900,$A9C1,$A881,$6840,$7800,$B8C1,$B981,$7940,$BB01,
      $7BC0,$7A80,$BA41,$BE01,$7EC0,$7F80,$BF41,$7D00,$BDC1,$BC81,$7C40,
      $B401,$74C0,$7580,$B541,$7700,$B7C1,$B681,$7640,$7200,$B2C1,$B381,
      $7340,$B101,$71C0,$7080,$B041,$5000,$90C1,$9181,$5140,$9301,$53C0,
      $5280,$9241,$9601,$56C0,$5780,$9741,$5500,$95C1,$9481,$5440,$9C01,
      $5CC0,$5D80,$9D41,$5F00,$9FC1,$9E81,$5E40,$5A00,$9AC1,$9B81,$5B40,
      $9901,$59C0,$5880,$9841,$8801,$48C0,$4980,$8941,$4B00,$8BC1,$8A81,
      $4A40,$4E00,$8EC1,$8F81,$4F40,$8D01,$4DC0,$4C80,$8C41,$4400,$84C1,
      $8581,$4540,$8701,$47C0,$4680,$8641,$8201,$42C0,$4380,$8341,$4100,
      $81C1,$8081,$4040);

  TYPE
    buffer = ARRAY[1..65521] OF BYTE;  {largest buffer that can be}
                                       {allocated on heap         }
  VAR
    i:  WORD;
    q:  ^buffer;

  PROCEDURE CalcCRC (p:  pointer; nbyte:  WORD; VAR CRCvalue:  WORD);
   {The following is a little cryptic (but executes very quickly).
    The algorithm is as follows:
      1.  exclusive-or the input byte with the low-order byte of
          the CRC register to get an INDEX
      2.  shift the CRC register eight bits to the right
      3.  exclusive-or the CRC register with the contents of
          Table[INDEX]
      4.  repeat steps 1 through 3 for all bytes}
  BEGIN
    q := p;
    FOR   i := 1 TO nBYTE DO
      CRCvalue := Hi(CRCvalue)  XOR  Table[ q^[i] XOR Lo(CRCvalue) ]
  END {CalcCRC};

  PROCEDURE CalcFileCRC (FromName:  STRING; VAR CRCvalue:  WORD;
              VAR IOBuffer:  pointer;  BufferSize:  WORD; VAR error:  WORD);
    VAR
      BytesRead:  WORD;
      FromFile :  FILE;
      i        :  WORD;
  BEGIN
    FileMode := 0;  {Turbo default is 2 for R/W; 0 is for R/O}
    CRCValue := 0;
    ASSIGN (FromFile,FromName);
    {$I-} RESET (FromFile,1); {$I+}
    error := IOResult;
    IF   error = 0
    THEN BEGIN
      REPEAT
        BlockRead (FromFile,IOBuffer^,BufferSize,BytesRead);
        CalcCRC (IOBuffer,BytesRead,CRCvalue)
      UNTIL BytesRead = 0;
      CLOSE (FromFile)
    END;
  END; {CalcFileCRC};

END. {CRC}.
Kullanıcı avatarı
şimal
Kıdemli Üye
Mesajlar: 516
Kayıt: 10 Kas 2003 09:36
Konum: ANKARA

Mesaj gönderen şimal »

çok teşekkürler
mege beyi in verdiği linki detaylıca inceledim
http://www.efg2.com/Lab/Mathematics/CRC.htm

ikinci gelen örnek kodun bir benzeri burada var...

sıradaki sorum 32 bit CRC idi onada cvp veren bir link...
örnek proje mevcut çok yardımcı oldu çççççooooookkkkk
tşkler sağlıcakla kalın :)
Kullanıcı avatarı
şimal
Kıdemli Üye
Mesajlar: 516
Kayıt: 10 Kas 2003 09:36
Konum: ANKARA

Mesaj gönderen şimal »

http://www.ibrtses.com/delphi/dcrc.html

CRC-16 yı çok şık biçimde anlatıyor:) vede çok kolay bu yöntemi çok sevdim...

[quote]"Hata sezme ile ilgili (Hamming Distance) arkadaşımın okul notları var. istenirse admine gönderebilirim. faydalı bir eser."[/quote]
demişsiniz mege bey

mümkünse size ve adminimize zahmet olmazsa alabilir miyim?
(hangi okulun hangi derse ait ne notu bu?)
Cevapla