Re: [Tutor] Consistant Overhead Byte Stuffing (COBS) algorithm help

2005-10-05 Thread Kent Johnson
Michael Cotherman wrote:
 I am a noob to converting pointers in C++ to arrays in
 python, although the first time I see it done, I will
 have no problem. Can you help converting the below
 (what I think is the 'decoder' section) to python?

OK I'll bite. Your code is copying from a src buffer to a dst buffer. The src 
buffer has a count byte followed by count-1 bytes of data. If the count is less 
that 0xFF, a zero byte has to be inserted into the dst. So the decode loop gets 
a count, copies that many bytes to the dst buffer, then optionally appends a 0.

Python doesn't have a direct equivalent to this sort of manipulation of memory 
pointers and raw memory buffers. They are commonly replaced by strings or 
lists. My function takes a string as an input argument, builds the output as 
string fragments in a list, then builds a string to return.

Note that Python strings include an implicit length so there is no need to pass 
and return a length argument.

I included a test case with the data from your previous email. Your data is 
evidently hex-encoded as well as COBS encoded - in other words your strings are 
hex values where each two chars represent a single byte. I have converted to 
and from byte strings to feed this to the decoder.

Kent

PS Please send your replies to the whole list, not to me personally. Thanks!


def unstuff(src):
# src is a COBS compressed string
current = 0 # index into src
dst = []# a list that will build the result

while current  len(src):
# Get the count and convert it to an integer
count = ord(src[current])

# Append count-1 chars from src to dst
dst.append(src[current+1:current+count])

# Do we need to add a zero byte?
if count  0xFF:
dst.append('\x00')

# Bump the counter and continue
if count0:
current += count
else:
current += 1

# dst is a list of string fragments; this converts it to a single string
return ''.join(dst)

def hexToString(hexStr):
''' Convert a string of two-digit hex values to a string of bytes with 
those values '''
return ''.join([chr(int(hexStr[i:i+2], 16)) for i in range(0, len(hexStr), 
2)])

def stringToHex(src):
''' Convert a byte string to a string of two-digit hex values '''
return ''.join([ '%02x' % ord(s) for s in src ])


if __name__ == '__main__':
data = '0002860104DB203F0100'
print data
data = hexToString(data)
print

newData = unstuff(data)
print stringToHex(newData)


 UINT CCobsPackets::UnStuffData(unsigned char *src,
 unsigned char *dst, UINT length)
 {
   unsigned char *dstStart = dst;
   unsigned char *end = src + length;
 
   while (src  end)
   {
   int i, code = *src++;
 
   for (i=1; icode; i++) 
   {
   *dst++ = *src++;
   }
   
   if (code  0xFF) 
   {
   *dst++ = 0;
   }
   }
 
   return (UINT)(dst - dstStart);
 }

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Consistant Overhead Byte Stuffing (COBS) algorithm help

2005-10-04 Thread Michael Cotherman
A little more info is below:


With miniterm modified to output hex to the screen,
here is the data coming in unformatted. (note zero
bytes delimit end of packet):

c:\Python23python miniterm1.1a.py
--- Miniterm --- type ESC to quit
0002860104DB203F0102860504CB1A740102860504CB1B740102860504CB1B740100
0002860504CB1B740102860504CB1B740102860504CB1B740102860504CB1B740100
0002860504CB1B740102860504CB1B740102860504CB1B740100



Here I have formatted it(one line equals a packet,
currently about every 30 seconds):

c:\Python23python miniterm1.1a.py
--- Miniterm --- type ESC to quit
00-02-86-05-04-CB-1B-74-01-00
00-02-86-05-04-CB-1B-74-01-00
00-02-86-01-04-DB-20-3E-01-00
00-02-86-05-04-CB-1B-74-01-00
00-02-86-05-04-CB-1B-74-01-00
00-02-86-05-04-CB-1B-74-01-00
00-02-86-05-04-CB-1B-74-01-00
00-02-86-05-04-CB-1B-74-01-00
00-02-86-05-04-CB-1B-74-01-00
00-02-86-05-04-CB-1B-74-01-00
00-02-86-05-04-CB-1B-74-01-00
00-02-86-05-04-CB-1B-74-01-00



here is what the data will look like after COBS
decoding (the zero bytes are real data):
86-00-04-CB-1B-74-00
86-00-04-CB-1B-74-00
86-00-04-CB-1B-74-00
86-00-04-CB-1B-74-00
86-00-04-CB-1B-74-00
86-00-04-CB-1B-74-00
86-00-04-CB-1B-74-00


For those who are curious as to what I am doing, here
is what telemetry is in the packet:
packe type= 0x86
Device ID = 0x0004
Payload:
Batt Volt.= 0xCB
Pot Volt. = 0x1B
Temp Volt.= 0x74
sw 1  = 0
sw 2  = 0
sw 3  = 0
sw 4  = 0
Btn 1 = 0
Btn 2 = 0
Yel LED   = 0
Red LED   = 0


Thanks,
mike





__ 
Yahoo! for Good 
Donate to the Hurricane Katrina relief effort. 
http://store.yahoo.com/redcross-donate3/ 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor