crc16.py
Go to the documentation of this file.
00001 """--------------------------------------------------------------------
00002 COPYRIGHT 2013 SEGWAY Inc.
00003 
00004 Software License Agreement:
00005 
00006 The software supplied herewith by Segway Inc. (the "Company") for its 
00007 RMP Robotic Platforms is intended and supplied to you, the Company's 
00008 customer, for use solely and exclusively with Segway products. The 
00009 software is owned by the Company and/or its supplier, and is protected 
00010 under applicable copyright laws.  All rights are reserved. Any use in 
00011 violation of the foregoing restrictions may subject the user to criminal 
00012 sanctions under applicable laws, as well as to civil liability for the 
00013 breach of the terms and conditions of this license. The Company may 
00014 immediately terminate this Agreement upon your use of the software with 
00015 any products that are not Segway products.
00016 
00017 The software was written using Python programming language.  Your use 
00018 of the software is therefore subject to the terms and conditions of the 
00019 OSI- approved open source license viewable at http://www.python.org/.  
00020 You are solely responsible for ensuring your compliance with the Python 
00021 open source license.
00022 
00023 You shall indemnify, defend and hold the Company harmless from any claims, 
00024 demands,liabilities or expenses, including reasonable attorneys fees, incurred 
00025 by the Company as a result of any claim or proceeding against the Company 
00026 arising out of or based upon: 
00027 
00028 (i) The combination, operation or use of the software by you with any hardware, 
00029     products, programs or data not supplied or approved in writing by the Company, 
00030     if such claim or proceeding would have been avoided but for such combination, 
00031     operation or use.
00032  
00033 (ii) The modification of the software by or on behalf of you 
00034 
00035 (iii) Your use of the software.
00036 
00037  THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
00038  WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
00039  TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00040  PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
00041  IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
00042  CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
00043  
00044  \file   crc16.py
00045 
00046  \brief  This module contains a collection of functions for calculating
00047          a CRC-16.
00048 
00049  \Platform: Cross Platform
00050 --------------------------------------------------------------------"""
00051 
00052 """
00053 CRC16 defines
00054 """
00055 CRC_ADJUSTMENT = 0xA001;
00056 CRC_TABLE_SIZE = 256;
00057 INITIAL_CRC = 0;
00058 
00059 HIGH_BYTE_MASK  = 0xFF00;
00060 LOW_BYTE_MASK   = 0x00FF;
00061 MOVE_BYTE_SHIFT = 8;
00062 LS_BIT          = 0x0001;
00063 BITS_PER_BYTE   = 8;
00064 
00065 crc_table= [0]*CRC_TABLE_SIZE;
00066 
00067 """
00068 This creates the CRC table
00069 """
00070 def generate_crc_table():
00071 
00072     for x in range(0,CRC_TABLE_SIZE):
00073 
00074         table_value = 0;
00075         k = x;
00076 
00077         for j in range(0,BITS_PER_BYTE):
00078             if (((table_value ^ k) & LS_BIT) == LS_BIT):
00079                 table_value = (table_value >> 1) ^ CRC_ADJUSTMENT;
00080             else:
00081                 table_value >>= 1;
00082             k >>= 1;
00083 
00084         crc_table[x] = table_value;
00085 
00086 """
00087 This computes an updated CRC 16 given the current value of the CRC 16 and
00088 a new data byte.
00089 """
00090 def calculate_crc_16(old_crc,new_word):
00091 
00092     temp = old_crc ^ new_word;
00093     new_crc = (old_crc >> MOVE_BYTE_SHIFT) ^ crc_table[temp & LOW_BYTE_MASK];
00094 
00095     return new_crc;
00096 
00097 """
00098 This computes the CRC for a buffer passed in
00099 """
00100 def compute_buffer_crc(byte_buffer, bytes_in_buffer):
00101     crc_index = bytes_in_buffer - 2;
00102     new_crc = 0;
00103 
00104     """
00105     We'll loop through each word of the message and update
00106     the CRC.  Start with the value chosen for CRC initialization.
00107     """
00108     for x in range(0,crc_index):
00109         """
00110         Now we'll send each byte to the CRC calculation.
00111         """
00112         new_crc = calculate_crc_16(new_crc, byte_buffer[x]);
00113 
00114 
00115     byte_buffer[crc_index]   = int((new_crc & HIGH_BYTE_MASK) >> MOVE_BYTE_SHIFT);
00116     byte_buffer[crc_index+1] = int((new_crc & LOW_BYTE_MASK));
00117     
00118 """
00119 This computes the CRC for a buffer passed in and checks it against
00120 the one contained in the buffer
00121 """
00122 def buffer_crc_is_valid(byte_buffer, bytes_in_buffer):
00123     
00124     crc_index = bytes_in_buffer - 2;
00125     new_crc = 0;
00126     success = False;
00127 
00128     """
00129     We'll loop through each word of the message and update
00130     the CRC.  Start with the value chosen for CRC initialization.
00131     """
00132     for x in range(0,crc_index):
00133         new_crc = calculate_crc_16(new_crc, byte_buffer[x]);
00134 
00135     """
00136     The new CRC is checked against that stored in the buffer.
00137     """
00138     received_crc = ((byte_buffer[crc_index] << MOVE_BYTE_SHIFT) & HIGH_BYTE_MASK);
00139     received_crc |= (byte_buffer[crc_index+1] & LOW_BYTE_MASK);
00140     
00141     if (received_crc == new_crc):
00142         success = True;
00143     else:
00144         success = False;
00145 
00146     return (success);


python_ethernet_rmp
Author(s): SEGWAY Inc.
autogenerated on Tue Mar 8 2016 00:53:38