Fixing escaped characters python-xbee
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('http://mail.python.org/mailman/listinfo/python-list
Re: Fixing escaped characters python-xbee
> > the following in gmane.comp.python.general: > > > > > 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 > > > > Why read 4 at a time if you need to detect the escape marker... > > > > PSEUDO_CODE -- UNTESTED: > > > > for c in ser.read():#presumes it will function as an iterator > > if ord(c) == 0x7D: > > c =chr(ord(ser.read(1)) ^ 0x20) > > #do something with c (save to a list for later joining as a > > string?) > > #probably need some condition to exit the read loop too > > > def logical_xor(str1, str2): > > > return bool(str1) ^ bool(str2) > > > > > bool() returns True or False based on the argument... Any non-empty > > string will be True. Instead what you want is to x-or the bits of the > > character itself. > > -- It works! Thank you so much. Now I can go ahead with my work! -- http://mail.python.org/mailman/listinfo/python-list