newbie sending hex values over UDP socket

Rob McCrea robmccrea at spaamadelphiaspremoveam.net
Mon Sep 13 19:58:20 EDT 2004


Bill Seitz wrote:

> Peter Hansen <peter at engcorp.com> wrote in message news:<QYGdnckJ3ukLf9zcRVn-sQ at powergate.ca>...
> 
>>Bill Seitz wrote:
>>
>>>Are you saying I can just do something as simple as
>>>sock.send('ri0')
>>>?
>>
>>Exactly.  That sends three bytes corresponding to the
>>"raw" byte values you showed before.
>>
>>
>>>(This interface is typically used over a modem connection between 2
>>>embedded/hardware devices, if that provides a clue/context. So it's
>>>entirely possible that I've thought the situation was more complicated
>>>than it really is because the other guy is used to dealing with these
>>>low-level devices...)
>>
>>The beauty of Python... makes working with even low-level hardware
>>a pleasure compared to many languages. :-)
>>
>>-Peter
> 
> 
> 
> Excellent - now how about calculating a parity-check byte?

well, I'm new to python, but the language hardly matters here, i believe.

is the parity a bit, a few bits, or a full byte?  granted the bits will 
be stored in a byte, but we need to know exactly, of course.  I believe 
parity usually indicates a single bit (on or off), but I don't trust 
anybody.

I'll mention these hard-coded example, but I wouldn't be at all 
surprised if python has built in bit-methods.

I number the 8 bits in a byte from 7 to 0, where 7 is the high bit.

if I wanted to check the bit number 5 in thatbyte, I would do simply, 
(well, as I said, I'm new, I'm going to use extra parens)

#untested code
     if (thatbyte & (2**5)) == (2**5):
         print 'Bit 5 is on.'
     else:
         print 'Bit 5 is off.'


for more than one bit, say the "first" 3 bits:  7, 6, and 5:

#untested code
     for bit in [7, 6, 5]:
         if (thatbyte & (2**bit)) == (2**bit):
             print 'Bit ' + str(bit) + 'is on.'
         else:
             print 'Bit ' + str(bit) + 'is off.'


Its bit math.
I think those're right.  hope so.

Rob


	







More information about the Python-list mailing list