[Tutor] help with translating a c function to a python function
shawn bright
nephish at gmail.com
Mon Jul 9 03:34:04 CEST 2007
Hey thanks,
i finally did get a function working.
i posted it on www.bitsbam.com
i did guess that the puchMsg++ ment that it was iterating through the bytes
of an array.
And Kent and Alan helped me get through the other parts.
I am glad for all this help, because this is an issue that comes up
increasingly often.
so i am also glad for the email archiving by gmail. he he
thanks all,
shawn
On 7/8/07, Tiger12506 <keridee at jayco.net> wrote:
>
>
> >> i have a c function from some modbus documentation that i need to
> >> translate into python.
>
> >> 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) ;
> >> }
>
> I found this link which may provide some insight into what's going on
> here.
> (google "modbus CRC16")
>
> http://www.modbustools.com/modbus_crc16.htm
>
> This proves to me that auchCRCHi is a lookup table that you do not have
> access to. Happily :-) that link provides the table.
>
> Hmmm... let's see, the difficult C stuff... *puchMsgg++ means to return
> the
> current character in a string, and then increment the pointer, so that
> when
> the C code encounters *puchMsgg++ again it reads the next character,
> increments, etc. You can emulate this with an index and array notation in
> python.
>
> ^ , << , and | are all bitwise operators, and python uses all of these
> in the same way as C
>
> '^' means XOR exclusive OR.
> 0101 ^ 0011 = 0110 i.e. 5 ^ 3 = 6
>
> '<< ' means left - shift
> 0010 << 2 = 1000 i.e. a << b = a * (2**b)
>
> '|' means OR.
> 0101 ^ 0011 = 0111 i.e. 5 ^ 3 = 7
>
> puchMsgg is basically a string
> and all the unsigned stuff are (very roughly) integers.
>
> HTH,
> Jacob S.
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070708/5b78077a/attachment-0001.html
More information about the Tutor
mailing list