Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #44253
| Path | csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python@mrabarnett.plus.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.005 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'converts': 0.09; 'subject:characters': 0.09; 'try:': 0.09; 'valueerror:': 0.09; 'xor': 0.09; 'python': 0.11; 'def': 0.12; '20)': 0.16; '3):': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'hexadecimal': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'occurs,': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'things...': 0.16; 'true:': 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; 'pass': 0.26; 'read,': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'character': 0.29; 'characters': 0.30; 'converting': 0.30; 'network.': 0.30; 'pos': 0.31; 'another': 0.32; 'ago': 0.33; 'checking': 0.33; 'could': 0.34; "can't": 0.35; 'except': 0.35; 'something': 0.35; 'convert': 0.35; 'received:84': 0.35; 'but': 0.35; 'there': 0.35; 'next': 0.36; 'to:addr:python- list': 0.38; 'list,': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'days': 0.60; 'read': 0.60; 'solve': 0.60; 'range': 0.61; 'first': 0.61; 'email addr:gmail.com': 0.63; 'kind': 0.63; 'header :Reply-To:1': 0.67; 'receive': 0.70; 'reply-to:no real name:2**0': 0.71; 'serial': 0.72; 'case?': 0.84; 'reply-to:addr:python.org': 0.84; 'recover': 0.91 |
| X-CM-Score | 0.00 |
| X-CNFS-Analysis | v=2.1 cv=JsTI8qIC c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=7AxPfEIvyrUA:10 a=5Ygi5Na7as4A:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=e-K5mnbOu4QA:10 a=pGLkceISAAAA:8 a=HLmApTYaMVRXeyQq5sIA:9 a=wPNLvfGTeEIA:10 a=MSl-tDqOz04A:10 |
| X-AUTH | mrabarnett:2500 |
| Date | Wed, 24 Apr 2013 11:53:09 +0100 |
| From | MRAB <python@mrabarnett.plus.com> |
| User-Agent | Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130328 Thunderbird/17.0.5 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: Fixing escaped characters python-xbee |
| References | <704e1981-1e13-4030-b4ca-a7305ec98b32@googlegroups.com> |
| In-Reply-To | <704e1981-1e13-4030-b4ca-a7305ec98b32@googlegroups.com> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| Reply-To | python-list@python.org |
| 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.1014.1366800795.3114.python-list@python.org> (permalink) |
| Lines | 70 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1366800795 news.xs4all.nl 15868 [2001:888:2000:d::a6]:53258 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:44253 |
Show key headers only | View raw
On 24/04/2013 08:33, 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...
>
You could try converting to a list, which is mutable.
# Python 3
read = list(ser.read(4))
pos = 0
try:
while True:
pos = read.index(0x7D, pos)
del read[pos]
read.extend(ser.read())
read[pos] ^= 0x20
except ValueError:
# There are no (more) 0x7D in the data.
pass
data = struct.unpack('<f', bytes(read))[0]
# Python 2
read = [ord(c) for c in ser.read(4)]
pos = 0
try:
while True:
pos = read.index(0x7D, pos)
del read[pos]
read.append(ord(ser.read()))
read[pos] ^= 0x20
except ValueError:
# There are no (more) 0x7D in the data.
pass
data = struct.unpack('<f', b"".join(chr(c) for c in read))[0]
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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