tool/cmd_alias.cpp
changeset 1126 b09fd81894cb
parent 1125 9976f7b9fe66
child 1136 a0982873d655
equal deleted inserted replaced
1125:9976f7b9fe66 1126:b09fd81894cb
     8 #include <iomanip>
     8 #include <iomanip>
     9 #include <sstream>
     9 #include <sstream>
    10 using namespace std;
    10 using namespace std;
    11 
    11 
    12 #include "globals.h"
    12 #include "globals.h"
       
    13 #include "sii_crc.h"
    13 
    14 
    14 /*****************************************************************************/
    15 /*****************************************************************************/
    15 
    16 
    16 const char *help_alias =
    17 const char *help_alias =
    17     "[OPTIONS] <ALIAS>\n"
    18     "[OPTIONS] <ALIAS>\n"
    80     }
    81     }
    81 }
    82 }
    82 
    83 
    83 /*****************************************************************************/
    84 /*****************************************************************************/
    84 
    85 
    85 /** Calculates the SII checksum field.
       
    86  *
       
    87  * The checksum is generated with the polynom x^8+x^2+x+1 (0x07) and an
       
    88  * initial value of 0xff (see IEC 61158-6-12 ch. 5.4).
       
    89  *
       
    90  * The below code was originally generated with PYCRC
       
    91  * http://www.tty1.net/pycrc
       
    92  *
       
    93  * ./pycrc.py --width=8 --poly=0x07 --reflect-in=0 --xor-in=0xff
       
    94  *   --reflect-out=0 --xor-out=0 --generate c --algorithm=bit-by-bit
       
    95  *
       
    96  * \return CRC8
       
    97  */
       
    98 uint8_t calcSiiCrc(
       
    99         const uint8_t *data, /**< pointer to data */
       
   100         size_t length /**< number of bytes in \a data */
       
   101         )
       
   102 {
       
   103     unsigned int i;
       
   104     uint8_t bit, byte, crc = 0x48;
       
   105 
       
   106     while (length--) {
       
   107         byte = *data++;
       
   108         for (i = 0; i < 8; i++) {
       
   109             bit = crc & 0x80;
       
   110             crc = (crc << 1) | ((byte >> (7 - i)) & 0x01);
       
   111             if (bit) crc ^= 0x07;
       
   112         }
       
   113     }
       
   114 
       
   115     for (i = 0; i < 8; i++) {
       
   116         bit = crc & 0x80;
       
   117         crc <<= 1;
       
   118         if (bit) crc ^= 0x07;
       
   119     }
       
   120 
       
   121     return crc;
       
   122 }
       
   123 
       
   124 /*****************************************************************************/
       
   125 
       
   126 /** Writes the Secondary slave address (alias) to the slave's SII.
    86 /** Writes the Secondary slave address (alias) to the slave's SII.
   127  */
    87  */
   128 void writeSlaveAlias(
    88 void writeSlaveAlias(
   129         uint16_t slavePosition,
    89         uint16_t slavePosition,
   130         uint16_t alias
    90         uint16_t alias