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 count>0:
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; 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 Kent Johnson
I'm not sure what the question is here. It looks like you need to write a COBS 
encoder / decoder in Python, maybe using your .NET code as a model. Then you 
can integrate that with comms code taken from miniterm. What help do you need?

Kent

Michael Cotherman wrote:
> Hello, I am really new to python and really have not
> programmed much since college, I played with it a
> little now, and it seems to be a tool I feel
> comfortable implementing a personal project in.
> 
> I wish to communicate via a serial port to a device
> that is using COBS. I wish to configure it and then
> receive data at an interval and store it in a rrd.
> The device itself receives telemetry information from
> other devices, and this telemetry info is going to get
> graphed and made available via a web page.
> 
> The serial port will be com or tty, for which I
> prepped by playing with pygarmin and miniterm. The
> device is working and communicable? via a program
> written in .NET by a friend of the friend who gave it
> to me. The program has so many things I wish to change
> that it would be easiest to start from scratch. I have
> some of the source for bits and pieces that may be
> needed.
> 
> The data coming in/going out will be COBS encoded,
> which changes/escapes all 0x00 bytes, then uses a 0x00
> byte for the framing.  
> 
> 
> COBS theory is explained here:
> http://www.stuartcheshire.org/papers/COBSforToN.pdf
> 
> and it looks like a version written in c is at:
> http://gtk-gnutella.sourceforge.net/doxygen/cobs_8c.htm
> 
> 
> I would initially be happy creating a cobs.py and then
> modding the initial 1.1 release of miniterm and seeing
> if I could listen to the device... The device will be
> sending packets of 2-12 bytes at regular intervals
> (99% will be 7 byte packets every minute or so), and I
> can just move the serial cable over from the com port
> with the working application to the cobs-miniterm one
> to see if I am getting the right.
> 
> Thanks in advance!
> 
> -mike
> clearwater, fl
> 
> 
>   
> __ 
> Yahoo! Mail - PC Magazine Editors' Choice 2005 
> http://mail.yahoo.com
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 

___
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:\Python23>python 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:\Python23>python 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


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

2005-10-03 Thread Michael Cotherman
Hello, I am really new to python and really have not
programmed much since college, I played with it a
little now, and it seems to be a tool I feel
comfortable implementing a personal project in.

I wish to communicate via a serial port to a device
that is using COBS. I wish to configure it and then
receive data at an interval and store it in a rrd.
The device itself receives telemetry information from
other devices, and this telemetry info is going to get
graphed and made available via a web page.

The serial port will be com or tty, for which I
prepped by playing with pygarmin and miniterm. The
device is working and communicable? via a program
written in .NET by a friend of the friend who gave it
to me. The program has so many things I wish to change
that it would be easiest to start from scratch. I have
some of the source for bits and pieces that may be
needed.

The data coming in/going out will be COBS encoded,
which changes/escapes all 0x00 bytes, then uses a 0x00
byte for the framing.  


COBS theory is explained here:
http://www.stuartcheshire.org/papers/COBSforToN.pdf

and it looks like a version written in c is at:
http://gtk-gnutella.sourceforge.net/doxygen/cobs_8c.htm


I would initially be happy creating a cobs.py and then
modding the initial 1.1 release of miniterm and seeing
if I could listen to the device... The device will be
sending packets of 2-12 bytes at regular intervals
(99% will be 7 byte packets every minute or so), and I
can just move the serial cable over from the com port
with the working application to the cobs-miniterm one
to see if I am getting the right.

Thanks in advance!

-mike
clearwater, fl



__ 
Yahoo! Mail - PC Magazine Editors' Choice 2005 
http://mail.yahoo.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor