473,326 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,326 developers and data experts.

Python - check CRC of a frame CRC-16-CCITT

Here is some code to check the CRC of a given message. The result of 0 indicates no trouble found. Reference: http://www.ross.net/crc/download/crc_v3.txt
Expand|Select|Wrap|Line Numbers
  1. def checkCRC(message):
  2.     #CRC-16-CITT poly, the CRC sheme used by ymodem protocol
  3.     poly = 0x1021
  4.     #16bit operation register, initialized to zeros
  5.     reg = 0x0000
  6.     #pad the end of the message with the size of the poly
  7.     message += '\x00\x00' 
  8.     #for each bit in the message
  9.     for byte in message:
  10.         mask = 0x80
  11.         while(mask > 0):
  12.             #left shift by one
  13.             reg<<=1
  14.             #input the next bit from the message into the right hand side of the op reg
  15.             if ord(byte) & mask:   
  16.                 reg += 1
  17.             mask>>=1
  18.             #if a one popped out the left of the reg, xor reg w/poly
  19.             if reg > 0xffff:            
  20.                 #eliminate any one that popped out the left
  21.                 reg &= 0xffff           
  22.                 #xor with the poly, this is the remainder
  23.                 reg ^= poly
  24.     return reg
May 5 '10 #1
0 23310

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: dasif | last post by:
We are looking for several contractors with very strong C++ and Pythong development skills. Any experience working for bank/brokerage/stock exchange is a big PLUS. Please email resume to...
5
by: Susanne Strege | last post by:
Hello... I'm wondering if it is possible to perform a CRC or Checksum on the data contained in a simple Linked List that uses pointers to nodes???
0
by: Andreas Håkansson | last post by:
Hi, I'm trying to implement a 32bit Table based CRC caluclator based on the guidelines in this http://thorkildsen.no/faqsys/docs/crc.txt document. However I have run into a small "problem"...
1
by: StormyTheCat | last post by:
Perhaps this is easy, but I'm new to C# am in need of some help. I need to embed a CRC value into an XML file to be able to detect when the data contained therin is corrupted. Calculating the...
4
by: Dennis | last post by:
Does VB.Net have any class that will calculate the CRC of a binary file or byte array? If not, does anyone have any code that does this. Thanks. -- Dennis in Houston
15
by: ValK | last post by:
Hello, I’m working with handheld device that communicates with windows service thru the serial port. Transaction between device and my application looks like this: Handshaking: Device sends ...
0
by: John Crouse | last post by:
I am able to check the crc of the archive but would like to check the crc for the file in the archive. How can I accomplish this? Do I need to extract the file first? I would rather not do this...
1
by: `Zidane Tribal | last post by:
it would appear that using the command 'crc("data")' from the String::CRC returns incorrect results (although, they are at least consistently incorrect). for example, this script..... ...
3
by: mfareed | last post by:
Can somebody help me with the CRC (Cyclic Redundancy Code) algorithm implementation given below: There is a three byte packet header. <--------1st byte-----><--------2nd...
9
by: ipkiss | last post by:
I am writing a program that reads the data from the serial port on Linux. The data are sent by another device in the following frame format: ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.