logga in.
Kontakt Downloads Artiklar Nyheter
Hem Länkar Datablad
Antal besökare
just nu: 38

Carlo Gavazzi AB
Västra Kyrkogatan 1
652 24 Karlstad
Tel: 054-851125
Fax: 054-851177

info@carlogavazzi.se
support@carlogavazzi.se order@carlogavazzi.se

 
Om cookies | Integritetspolicy
Hjälp

Visa alla Artiklar
Maila länk | Stöds av IESkriv ut

Dupline

senast uppdaterad 2004-10-11 . Med reservation för ev tryckfel.

Teknisk info

CRC Beräkning

Eftersom de flesta termer är engelska och översättning till svenska skulle bara vara förvirrande har vi valt att publicera texten på engelska.

******************** CRC Definition ****************
Cyclical Redundancy Check (CRC) is a check sum method for MODBUS communications. The check-sum field is 2 bytes, which contains a 16 bit binary value.
A CRC is calculated by first loading a 16 bit register with FFFF. Then the process begins by applying successive 8 bit bytes of the message to the current contents of the register. The start bit, stop bits, and the parity bit do not apply to CRC calculation.

Generating a CRC
1. Load a 16 bit register with FFFF. Call this the CRC register.
2. Exclusive OR the first eight bit byte of the message with the low order byte of the CRC register, with the result being put into the CRC register.
3. Shift the CRC register one bit to the right (towards the LSB). Zero fill the MSB. Extract and examine the LSB.
4. If the LSB is 0, repeat the previous step (another shift). If the LSB is a 1, Exclusive OR the CRC register with the value A001 hexadecimal (1010 0000 0000 0001).
5. Repeat steps 3 and 4 until eight shifts have been performed. When this is done, a complete eight bit byte has been processed.
6. Repeat steps 2 through 5 for the next eight bit byte of the message. Continue until all bytes have been processed.

Result The final contents of the CRC register is the CRC value.

When the CRC is placed into the message, the upper and lower bytes must be swapped.

Example When the CRC is transmitted in the message, the low order byte will be transmitted first, followed by the high order byte. If the CRC value is 1241 (hexidecimal) then the value will be transmitted as 4112 (hexidecimal).
***********************************************

Exempel på CRC-beräkning i VB-kod.

'-------------------------------------------------
Public Function CRC(buf() As Byte, lbuf As Integer) As Integer
'-------------------------------------------------

' returns the MODBUS CRC of the lbuf first bytes of "buf" buffer (buf is a global array of bytes)
Dim CRC1 As Integer

CRC1 = &HFFFF ' init CRC
For i = 0 To lbuf - 1 Step 1 ' for each byte
CRC1 = CRC1 Xor buf(i)
For j = 0 To 7 Step 1 ' for each bit
k = CRC1 And 1 ' memo bit 0 state
CRC1 = ((CRC1 And &HFFFE) / 2) And &H7FFF ' Shift right with 0 at left
If k > 0 Then CRC1 = CRC1 Xor &HA001 ' Bocuse
Next j
Next i
CRC = CRC1
End Function



Beställ Produktkatalog!


Beställ ditt exemplar!