stm32 - CRC hesaplama

Başlatan magnetron, 08 Eylül 2016, 03:18:46

magnetron

merhaba forum,

modbus RTU ile STM32 yi haberleştirmek istiyorum

modbus paketin sonunda 16 bitlik bir CRC sayısı var

şimdi STM32 STD Library de bir CRC örneği vermiş ama

bunun sonucu ve giriş dataları 32 bitlik

16 bit için nasıl hesaplattırıcam ? ( ayrıca modbus ta okuduğum kadarıyla

mesaj paketinden byte byte çekiyor )

teşekkür

1n4001

#1
uint16_t ModRTU_CRC(uint8_t *buf, uint8_t start, uint8_t len){
   uint16_t crc = 0xFFFF;
   uint8_t pos;
   
   for (pos = start; pos < len; pos++){
      crc ^= (uint16_t)buf[pos];    // XOR byte into least sig. byte of crc

      for (int i = 8; i != 0; i--){ // Loop over each bit
         if ((crc & 0x0001) != 0){  // If the LSB is set
            crc >>= 1;              // Shift right and XOR 0xA001
            crc ^= 0xA001;
         }
         else                       // Else LSB is not set
         crc >>= 1;                 // Just shift right
      }
   }
   // Note, this number has low and high bytes swapped, so use it accordingly (or swap bytes)
   return crc;  
}


// Örnek Kullanım: ModRTU_CRC(dizi, baslangic_elemani, uzunluk);
crc16 = ModRTU_CRC(buffer, 0, 5);


Mesaj paketinin byte olması problem değil, buffer dizisi byte tanimli.