Checksum error in Plugwise Driver Dirk Engels

Plugwise Forum about Plugwise devices and the Source software.
Post Reply
Freemann
Member
Member
Posts: 54
Joined: Tue Jan 03, 2012 12:21 am

Checksum error in Plugwise Driver Dirk Engels

Post by Freemann »

I'm struggling for a while now with the Plugwise driver from Dirk Engels.

When i request PowerInfo from my devices, some of them doesn't response.

I found that for the devices that didn't response, the had a to short send string:

Good sendstring (without checksum):
0012000D6F0000C3A6FC(20)
generated checksum for the above string:
FC0A(4)
Combined send string is:
0012000D6F0000C3A6FCFC0A (24)


Wrong sendstring (without checksum):
0012000D6F0000D3198D(20)
generated checksum for the above string:
1BE(3)
Combined send string is:
0012000D6F0000D3198D1BE (23)

The last string is 1 char to short and so i got no response from the circle.

this is the php function that generates the CRC.

Code: Select all

    /**
*
* The make Crc Checksum function creates a 16bit CRC checksum of the input
* string.
* @param string $string
*/
    public function makeCrcCheckSum($string) {
        $crc = 0x0000;
        for ($i = 0, $j = strlen($string); $i < $j; $i++) {
            $x = (($crc >> 8) ^ ord($string[$i])) & 0xFF;
            $x ^= $x >> 4;
            $crc = (($crc << 8) ^ ($x << 12) ^ ($x << 5) ^ $x) & 0xFFFF;
        }

        return strtoupper(dechex($crc));
    }
Anyone a idea whats happening here?
Digit
Global Moderator
Global Moderator
Posts: 3388
Joined: Sat Mar 25, 2006 10:23 am
Location: Netherlands
Contact:

Re: Checksum error in Plugwise Driver Dirk Engels

Post by Digit »

Wild guess: the checksum numeric value is correct, but should be sent as 01BE, not 1BE.
Just a matter of formatting.
Freemann
Member
Member
Posts: 54
Joined: Tue Jan 03, 2012 12:21 am

Re: Checksum error in Plugwise Driver Dirk Engels

Post by Freemann »

could be...

i checked it:
============== 1 packet ==============
Request.php:204 :: Good sendstring (without checksum):
0012000D6F0000D3198D (20)

Request.php:211 :: Generated Decimal CRC
446 (3)

Request.php:212 :: CRC decimal to Hex
1be (3)

combined send string:
0012000D6F0000D3198D1BE (23)

"response from ..... (plug?) ": 0000000000C264DF
===================================

============== 2 packet ==============
Request.php:204 :: Good sendstring (without checksum):
0026000D6F0000D3198D(20)

Request.php:211 :: Generated Decimal CRC
16964(5)

Request.php:212 :: CRC decimal to Hex
4244(4)

combined send string:
0026000D6F0000D3198D4244 (24)

Response from plug: 002700DD000D6F0000D3198D3F7D709BB612A7853C62D7FA00000000BA4C
===================================

This shows that the first dec-crc is also to short.
The first combined packet/string is wrong and the plug doesn't response.
The second combined packet/string is goed and the plug does response.


[/edit]

based on the following python script, i changed the checksum function:
python-plugwise / plugwise / protocol.py
look at lines:19, 157, 161

i made the following python script:

Code: Select all

#!/usr/bin/env python

import sys,struct,crcmod

# If no arguments were given, print a helpful message
if len(sys.argv)==1:
    print 'Usage: dec'
    sys.exit(0)

crc_func = crcmod.mkCrcFun(0x11021, rev=False, initCrc=0x0000, xorOut=0x0000)
print crc_func(sys.argv[1])
in php i change the code to:

Code: Select all

        ......
        // $string = $sendstring;
	$crc = exec("/home/marco/crcfun.py ".$string."");
	return dechex($crc);
        ......
this gives the following result:
============== 1 packet ==============
Request.php:204 :: Good sendstring (without checksum):
0012000D6F0000D3198D (20)

Request.php:211 :: Generated Decimal CRC
446 (3)

Request.php:212 :: CRC decimal to Hex
1be (3)

combined send string:
0012000D6F0000D3198D1BE (23)

"response from ..... (plug?) ": 0000000000C264DF
===================================

============== 2 packet ==============
Request.php:204 :: Good sendstring (without checksum):
0026000D6F0000D3198D(20)

Request.php:211 :: Generated Decimal CRC
16964(5)

Request.php:212 :: CRC decimal to Hex
4244(4)

combined send string:
0026000D6F0000D3198D4244 (24)

Response from plug: 002700DD000D6F0000D3198D3F7D709BB612A7853C62D7FA00000000BA4C
===================================

yes,yes it is copied from above, because the python script gives exactly the same result.
Both script generate a short decimal CRC....

for those how want to play,check,debug,etc... the mac address of the plug is D3198D and i'm trying to receive the current power usage.
[/edit]

[edit2]
oke i found the following site:
crc-calculation

On this site you can generate multiple CRC on a string.
based on the driver i expect a "fc0a" CRC for "0012000D6F0000C3A6FC".
When i generate the ASCii CRC's i indeed see "CRC-CCITT (XModem) 0xFC0A"
when i generate the ASCII CRC's for "0012000D6F0000C3A6FC" i get:

Code: Select all

1 byte checksum	48
CRC-16	0xB2A7
CRC-16 (Modbus)	0xA983
CRC-16 (Sick)	0x1CFD
CRC-CCITT (XModem)	0x01BE
CRC-CCITT (0xFFFF)	0xF706
CRC-CCITT (0x1D0F)	0x9E0A
CRC-CCITT (Kermit)	0x2A5A
CRC-DNP	0xAA6D
CRC-32	0x83C31987
CRC-CCITT (XModem) 0x01BE

So it look likes that Digit had it all right!!

Probably the dechex() php function is missing the whole thing up....

The question now is, how can i solve this problem?!
[/edit2]

[edit3]

change Python to:

Code: Select all

crc_func = crcmod.mkCrcFun(0x11021, rev=False, initCrc=0x0000, xorOut=0x0000)
print str('%.4x' % crc_func(sys.argv[1]))
and....

IT WORKS!!!!!!!

the dechex() function from PHP messes the whole thing up.
anybody a native php fix?

[/edit3]
Post Reply

Return to “Plugwise Forum”