Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #44261

Re: Fixing escaped characters python-xbee

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'string': 0.09; 'converts': 0.09; 'escape': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:characters': 0.09; 'xor': 0.09; 'python': 0.11; 'def': 0.12; 'stored': 0.12; '20)': 0.16; '3):': 0.16; 'byte,': 0.16; 'hexadecimal': 0.16; 'marker': 0.16; 'occurs,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'simplest': 0.16; 'things...': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'appears': 0.22; 'header:User-Agent:1': 0.23; 'char': 0.24; 'sends': 0.24; 'purposes': 0.26; 'this:': 0.26; 'read,': 0.26; 'header:X-Complaints-To:1': 0.27; 'tried': 0.27; 'character': 0.29; 'characters': 0.30; 'network.': 0.30; 'another': 0.32; 'ago': 0.33; 'checking': 0.33; "can't": 0.35; 'something': 0.35; 'convert': 0.35; 'but': 0.35; 'next': 0.36; 'list': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'days': 0.60; 'read': 0.60; 'solve': 0.60; 'new': 0.61; 'range': 0.61; 'first': 0.61; 'email addr:gmail.com': 0.63; 'kind': 0.63; 'finally': 0.65; 'receive': 0.70; 'serial': 0.72; 'case?': 0.84; 'compose': 0.84; 'n):': 0.84; 'recover': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Fixing escaped characters python-xbee
Date Wed, 24 Apr 2013 13:49:48 +0200
Organization None
References <704e1981-1e13-4030-b4ca-a7305ec98b32@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084bfc0.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1018.1366804172.3114.python-list@python.org> (permalink)
Lines 62
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1366804172 news.xs4all.nl 15873 [2001:888:2000:d::a6]:48320
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:44261

Show key headers only | View raw


pabloblo85@gmail.com wrote:

> I am using a XBee to receive data from an arduino network.
> 
> But they have AP=2 which means escaped characters are used when a 11 or 13
> appears (and some more...)
> 
> When this occurs, XBee sends 7D and inmediatly XOR operation with char and
> 0x20.
> 
> I am trying to recover the original character in python but I don't know
> ho to do it.
> 
> I tried something like this:
> 
> read = ser.read(4) #Read 4 chars from serial port
> for x in range (0,4):
> if(toHex(read[x]) != '7d'): #toHex converts it to hexadecimal just for
> checking purposes if(x < 3):
> read[x] = logical_xor(read[x+1], 20) #XOR
> for y in range (x+1,3):
> read[y] = read[y+1]
> read[3] = ser.read()
> else:
> read[x] = logical_xor(ser.read(), 20) #XOR
> 
> data = struct.unpack('<f', read)[0]
> 
> logical_xor is:
> 
> def logical_xor(str1, str2):
>     return bool(str1) ^ bool(str2)
> 
> I check if 7D character is in the first 3 chars read, I use the next char
> to convert it, if it is the 4th, I read another one.
> 
> But I read in python strings are inmutables and I can't change their value
> once they have one.
> 
> What would you do in this case? I started some days ago with python and I
> don't know how to solve this kind of things...

As you cannot change the old string you have to compose a new one. I think 
the simplest approach is to always read one byte, and if it's the escape 
marker read another one and decode it. The decoded bytes/chars are then 
stored in a list and finally joined:

# Python 2
# untested
def read_nbytes(ser, n):
    accu = []
    for i in xrange(n):
        b = ser.read(1)
        if b == "\x7d":
            b = chr(ord(ser.read(1)) ^ 0x20)
        accu.append(b)
    return "".join(accu)

b = read_nbytes(ser, 4)
data = struct.unpack('<f', b)[0]

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Fixing escaped characters python-xbee pabloblo85@gmail.com - 2013-04-24 00:33 -0700
  Re: Fixing escaped characters python-xbee MRAB <python@mrabarnett.plus.com> - 2013-04-24 11:53 +0100
  Re: Fixing escaped characters python-xbee Peter Otten <__peter__@web.de> - 2013-04-24 13:49 +0200
  Re: Fixing escaped characters python-xbee Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-04-24 20:29 -0400
    Re: Fixing escaped characters python-xbee pabloblo85@gmail.com - 2013-04-26 01:17 -0700

csiph-web