contoh code & Prosedur menghitung CRC16

Flow Chart CRC16

Prosedur menghitung Nilai CRC :

1. Isi sebuah register 16–bit dgn  FFFF hex . misal kita namakan register CRC.

2. ExOR  8–bit byte pertama data  dgn  low–order byte Register 16 bit CRC, simpan hasil di register CRC.

3. Geser kekanan 1 bit  CRC register, isi  MSB dgn 0. Ambil dan cek  LSB apakah  1 atau 0.

4. (jika LSB = 0): ulangi  langkah 3 (geser lagi 1 bit).

(jika LSB = 1): ExOR  CRC register  dgn  0xA001 (1010 0000 0000 0001).

5. Ulangi langkah  3 dan 4 sampai  8 kali pergeseran dilakukan. Setelah selesai ,sebuah  byte siap  akan diproses .

6.Ulangi langkah  2 sampai  5 untuk byte  berikutnya dari data. Lanjutkan sampai semua byte data diproses .

7. Hasil Akhir isi  CRC register adalah nilai  CRC.

8. Ketika  CRC akan di gabungkan dgn pesan yg akan dikirim , byte upper dan   lower  harus diswap/ditukar.

Contoh menghitung CRC16    data    02 07     :

Contoh Proses mencari CRC16 dari data: 02 07

Contoh code menghitung CRC16 dengan Visual basic 6:

*********************************************************************** 

Function CRC_16(OutputString As String) As String
Dim Generator, CRC As Long
Dim i As Integer, j As Integer, Length As Integer
Dim Bit As Boolean
'Dim Temp As Integer
Dim Temp As Byte
Length = Len(OutputString)
CRC = 65535
Generator = 40961

For i = 1 To Length
    Temp = Asc(Mid(OutputString, i, 1))
    CRC = CRC Xor Temp
    For j = 1 To 8
        Bit = CRC And 1
        CRC = CRC \ 2
        If Bit = True Then
           CRC = CRC Xor Generator
        End If
     Next j
Next i
CRC_16 = Chr(CRC Mod 256) & Chr(CRC \ 256)
'Buffer = Buffer & Chr(CRC Mod 256)
'Buffer = Buffer & Chr(CRC \ 256)
'Text1.Text = Str(CRC \ 256)
'Text2.Text = Str(CRC Mod 256)
End Function

Contoh code menghitung CRC16 dengan C#:

******************************************************************

        private void GetCRC(byte[] message, ref byte[] CRC)
        {
            //Function expects a modbus message of any length as well as a 2 byte CRC array in which to
            //return the CRC values:

            ushort CRCFull = 0xFFFF;
            byte CRCHigh = 0xFF, CRCLow = 0xFF;
            char CRCLSB;

            for (int i = 0; i < (message.Length) - 2; i++)
            {
                CRCFull = (ushort)(CRCFull ^ message[i]);

                for (int j = 0; j < 8; j++)
                {
                    CRCLSB = (char)(CRCFull & 0x0001);
                    CRCFull = (ushort)((CRCFull >> 1) & 0x7FFF);

                    if (CRCLSB == 1)
                        CRCFull = (ushort)(CRCFull ^ 0xA001);
                }
            }
            CRC[1] = CRCHigh = (byte)((CRCFull >> 8 ) & 0xFF);
            CRC[0] = CRCLow = (byte)(CRCFull & 0xFF);
        }
        #endregion

        private void BuildMessage(byte address, byte type, ushort start, ushort registers, ref byte[] message)
        {
            //Array to receive CRC bytes:
            byte[] CRC = new byte[2];

            message[0] = address;
            message[1] = type;
            message[2] = (byte)(start >> 8);
            message[3] = (byte)start;
            message[4] = (byte)(registers >> 8);
            message[5] = (byte)registers;

            GetCRC(message, ref CRC);
            message[message.Length - 2] = CRC[0];
            message[message.Length - 1] = CRC[1];
        }

Sebagai perbandingan anda bisa menghitung CRC disini

referensi:
http://www.modbus.pl/Visual_Basic.html
http://www.modbus.org/

About pccontrol

Berisi Tutorial Menggunakan PC untuk mengontrol Peralatan dengan cara mudah & praktis.

Posted on 08/06/2011, in Contoh-contoh Aplikasi. Bookmark the permalink. Leave a comment.

Komentar ,Saran atau Pertanyaan

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: