FYI, here's the code I use to test the CRC high-byte for collisions, excluding the CRC class that calculates the CRC as per the polynomial I gave earlier:
Code:
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<unsigned char> crcCollisions[256];
for (int i=0; i<256; ++i)
{
Crc16Modbus crc;
crc.Calc((unsigned char)i);
unsigned char res = (unsigned char)(crc.Result() >> 8);
crcCollisions[res].push_back(i);
}
int numDistinct = 0;
for (int i=0; i<256; ++i)
{
if (!crcCollisions[i].empty())
++numDistinct;
}
std::cout << "There are " << numDistinct << " possible values for "
"the CRC high byte of a packet with 1 data byte" << std::endl;
for (int i=0; i<256; ++i)
{
if (!crcCollisions[i].empty())
{
std::cout << "CRC(" << i << ") ";
for (size_t j=0; j<crcCollisions[i].size(); ++j)
std::cout << (int)crcCollisions[i][j] << " ";
std::cout << std::endl;
}
}
std::getchar();
return 0;
}
And here are the results:
Code:
There are 8 possible values for the CRC high byte of a packet with 1 data byte
CRC(62) 2 14 22 26 38 42 50 62 70 74 82 94 98 110 118 122 134 138 146 158 162 174 182 186 194 206 214 218 230 234 242 254
CRC(63) 6 10 18 30 34 46 54 58 66 78 86 90 102 106 114 126 130 142 150 154 166 170 178 190 198 202 210 222 226 238 246 250
CRC(126) 1 13 21 25 37 41 49 61 69 73 81 93 97 109 117 121 133 137 145 157 161 173 181 185 193 205 213 217 229 233 241 253
CRC(127) 5 9 17 29 33 45 53 57 65 77 85 89 101 105 113 125 129 141 149 153 165 169 177 189 197 201 209 221 225 237 245 249
CRC(190) 4 8 16 28 32 44 52 56 64 76 84 88 100 104 112 124 128 140 148 152 164 168 176 188 196 200 208 220 224 236 244 248
CRC(191) 0 12 20 24 36 40 48 60 68 72 80 92 96 108 116 120 132 136 144 156 160 172 180 184 192 204 212 216 228 232 240 252
CRC(254) 7 11 19 31 35 47 55 59 67 79 87 91 103 107 115 127 131 143 151 155 167171 179 191 199 203 211 223 227 239 247 251
CRC(255) 3 15 23 27 39 43 51 63 71 75 83 95 99 111 119 123 135 139 147 159 163 175 183 187 195 207 215 219 231 235 243 255
Don't worry, the implementation of Crc16Modbus is known to be correct, and I think it's usage above should be pretty self explanatory.
Edit: I also confirmed that for all possible packets containing two data bytes that the possible CRC values are evenly distributed. So really the big problem is really only with replies containing just a single data byte, which just so happens to be the size of every reply (it's the polls that are up to 6 bytes).