need help with converting c function to python function

N

nephish

hello all,

i have a c function from some modbus documentation that i need to
translate into python.

it looks like this:


unsigned short CRC16(puchMsg, usDataLen)
unsigned char *puchMsg ;
unsigned short usDataLen ;
{
unsigned char uchCRCHi = 0xFF ;
unsigned char uchCRCLo = 0xFF ;
unsigned uIndex ;
while (usDataLen--)
{
uIndex = uchCRCHi ^ *puchMsgg++ ;
uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex} ;
uchCRCLo = auchCRCLo[uIndex] ;
}
return (uchCRCHi << 8 | uchCRCLo) ;
}

some of it i can make out, but i can't seem to figgure out
this part ' auchCRCHi[uIndex};
it looks like a typo, because there is a closing } that does not match
the opening [.


here is what i have so far, but is not giving me the right values

def crc16(data):
crc_hi = 0xff
crc_lo = 0xff
for x in data:
crc_index = crc_hi ^ x
crc_hi = crc_lo ^ (crc_hi | crc_index)
crc_lo = crc_lo | crc_index
return (crc_hi << 8 | crc_lo)

whaddya think?

thanks
 
A

Anton Vredegoor

i have a c function from some modbus documentation that i need to
translate into python.

it looks like this:


unsigned short CRC16(puchMsg, usDataLen)
unsigned char *puchMsg ;
unsigned short usDataLen ;
{
unsigned char uchCRCHi = 0xFF ;
unsigned char uchCRCLo = 0xFF ;
unsigned uIndex ;
while (usDataLen--)
{
uIndex = uchCRCHi ^ *puchMsgg++ ;
uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex} ;
uchCRCLo = auchCRCLo[uIndex] ;
}
return (uchCRCHi << 8 | uchCRCLo) ;
}

some of it i can make out, but i can't seem to figgure out
this part ' auchCRCHi[uIndex};
it looks like a typo, because there is a closing } that does not match
the opening [.


here is what i have so far, but is not giving me the right values

def crc16(data):
crc_hi = 0xff
crc_lo = 0xff
for x in data:
crc_index = crc_hi ^ x
crc_hi = crc_lo ^ (crc_hi | crc_index)
crc_lo = crc_lo | crc_index
return (crc_hi << 8 | crc_lo)

whaddya think?

Use lateral thinking. CRC usually means some standard data checking
algorithm. If you google for crc16 you will find various python
implementations. Off hand I would first try this one because he seems to
have been thinking about it:

http://mail.python.org/pipermail/python-list/2005-September/342097.html

But I don't really know what it is you are looking for, cyclic
redundancy check?

http://en.wikipedia.org/wiki/Cyclic_redundancy_check

A.
 
J

John Machin

hello all,

i have a c function from some modbus documentation that i need to
translate into python.

it looks like this:

unsigned short CRC16(puchMsg, usDataLen)
unsigned char *puchMsg ;
unsigned short usDataLen ;
{
unsigned char uchCRCHi = 0xFF ;
unsigned char uchCRCLo = 0xFF ;
unsigned uIndex ;
while (usDataLen--)
{
uIndex = uchCRCHi ^ *puchMsgg++ ;
uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex} ;
uchCRCLo = auchCRCLo[uIndex] ;
}
return (uchCRCHi << 8 | uchCRCLo) ;

}

some of it i can make out, but i can't seem to figgure out
this part ' auchCRCHi[uIndex};
it looks like a typo, because there is a closing } that does not match
the opening [.

Yes, that's one problem.
The other problems are:

(1) you haven't noticed that auchCRClo is *NOT* the same as uchCRClo
(ditto "hi").

(2) the C code is missing global declarations, something like this:

unsigned char auchCRCLo[256] = { yadda yadda yadda}; /* ditto "hi" */

These two tables of 8-bit constants correspond to the single "table"
of 16-bit constants in the reference that Anton gave you.

Aside: the code looks like it was originally Z80 assembly language
subsequently transliterated into BDS C (of blessed memory).


(3) you have misread/mistranslated auchCRCLo[uIndex] blindly and
unconcernedly as uchCRCLo ^ uIndex !!! (ditto "hi")
here is what i have so far, but is not giving me the right values

def crc16(data):
crc_hi = 0xff
crc_lo = 0xff
for x in data:
crc_index = crc_hi ^ x
crc_hi = crc_lo ^ (crc_hi | crc_index)
crc_lo = crc_lo | crc_index
return (crc_hi << 8 | crc_lo)

whaddya think?

I don't think you really want to know :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top