Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!tudelft.nl!txtfeed1.tudelft.nl!feeder2.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.85.MISMATCH!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'mentioned,': 0.04; 'ok.': 0.04; 'high-level': 0.05; 'subject:Python': 0.05; 'typed': 0.07; 'python': 0.08; '21,': 0.09; 'iterate': 0.09; 'nicely': 0.09; 'return,': 0.09; 'word)': 0.09; 'am,': 0.12; 'exception': 0.12; 'python?': 0.15; '(sorry': 0.16; 'bit.': 0.16; 'iterator,': 0.16; 'nonsense': 0.16; 'perl.': 0.16; 'set,': 0.16; 'wrote:': 0.18; 'bytes': 0.18; 'perl': 0.18; 'string,': 0.18; 'convert': 0.19; 'insert': 0.19; '(which': 0.19; 'to:2**1': 0.21; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'received:209.85.212.46': 0.23; 'received:mail-vw0-f46.google.com': 0.23; 'interface': 0.23; 'sfxlen:0': 0.23; "shouldn't": 0.23; 'byte': 0.24; 'function': 0.27; 'do.': 0.28; 'raise': 0.28; 'bit': 0.28; 'extreme': 0.29; 'nov': 0.29; 'array': 0.30; 'generally': 0.30; 'iterating': 0.30; 'strings,': 0.30; 'zero.': 0.30; "i've": 0.31; 'list': 0.32; 'familiar': 0.32; '----': 0.32; 'pretty': 0.32; 'message- id:@gmail.com': 0.33; 'actually': 0.33; 'there': 0.33; 'to:addr :python-list': 0.34; 'it.': 0.34; 'character': 0.34; 'received:209.85.212': 0.34; 'things': 0.34; 'subject:/': 0.35; 'test': 0.35; 'device': 0.36; 'subject:with': 0.36; 'example,': 0.37; 'none': 0.37; 'charset:us-ascii': 0.37; 'skip:" 10': 0.37; 'but': 0.37; 'received:192': 0.37; 'received:google.com': 0.37; 'think': 0.37; 'could': 0.37; 'doing': 0.38; 'bunch': 0.38; 'received:209.85': 0.38; 'characters': 0.39; 'ways': 0.39; 'should': 0.39; 'mark': 0.39; 'subject: (': 0.40; 'received:209': 0.40; 'to:addr:python.org': 0.40; 'received:192.168': 0.40; 'more': 0.61; 'quick': 0.61; 'your': 0.61; 'efficient': 0.62; 'header:Message-Id:1': 0.62; 'back': 0.62; 'high': 0.67; 'hopefully,': 0.67; 'receive': 0.68; 'serial': 0.71; 'naughty': 0.84; 'sending,': 0.84; 'swap': 0.84; 'subject:Serial': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=content-type:mime-version:subject:from:in-reply-to:date :content-transfer-encoding:message-id:references:to:x-mailer; bh=3O/J1JnuxsTCbDs1/EBPrRNiq+lGZ+A+aeIG1grYPv0=; b=gR/u0AJSsWyZNJGXiIRoWnptQ+p2KmFxiFvwDA4rx1MCzpQsDY+aS9MdfGA+u+E94h 7w4WRCfTNgJvT/mo0ZMgcFj8C8EULqOa8dgj/dFBAR8tQJEH1dShtZAi8GjpkiGDIVlX nzKYm0792IjE0MM5sFDWq4/QiRFNJhQK+xd8c= Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Apple Message framework v1251.1) Subject: Re: Non-POSIX parity (mark/space) with Python-Serial on Linux. From: David Riley In-Reply-To: <22753805.1034.1321894337412.JavaMail.geo-discussion-forums@yqdr22> Date: Mon, 21 Nov 2011 12:22:28 -0500 Content-Transfer-Encoding: quoted-printable References: <27511132.925.1321884055247.JavaMail.geo-discussion-forums@yqnf38> <2723247.1219.1321892899941.JavaMail.geo-discussion-forums@yqzz20> <22753805.1034.1321894337412.JavaMail.geo-discussion-forums@yqdr22> To: Matthew Lenz , python-list@python.org X-Mailer: Apple Mail (2.1251.1) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 69 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1321896151 news.xs4all.nl 6886 [2001:888:2000:d::a6]:41169 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:16031 On Nov 21, 2011, at 11:52 AM, Matthew Lenz wrote: > Ahh. Ok. So how would I go about doing that with python? I think in = perl (sorry for the naughty word) I could use the tr// (translate) but = is there a quick way to do so with python? Is it going to be necessary = to convert commands I SEND to the device or only convert what I receive? The high-level overview is that you'll want to OR in 0x80 on transmit, = and AND 0x7F on receive (thus inserting the high bit when you send it = out and removing it when you receive). If you wanted to be extra-sure = you're receiving things correctly, you should also check to see if your = value ANDed with 0x80 is not zero. In Python 2.x, as mentioned, when you iterate over a string, you get a = bunch of tiny one-character strings, which you then need to convert into = numbers with ord() and back into strings with chr() when you = re-concatenate it. ord() and chr() may be familiar to you from Perl. = For example, you could do this on reception: ---- # However you get your data out of serial received_str =3D receive_my_string() ord_str =3D [ord(x) for x in received_str] # An exception may be extreme in this case, but hey. Note that # the filter() function actually returns a list of all the # characters that don't have the high bit set, so you could # actually use that if you wanted to. if filter((lambda x: x < 0x80), ord_str): raise IOError("Received character without mark parity") return "".join([chr(x & 0x7F) for x in received_str]) ---- In Python 3.x, iterating over a bytes array (which is, hopefully, what = your serial interface returns) will give you a bunch of ints, so you = shouldn't need to do the conversions: ---- # However you get your data out of serial received_bytes =3D receive_my_string() # In Python 3.x, filter() returns an iterator, which is generally a # better thing to return, but doesn't test as nicely for a bool. for b in received_bytes: if b < 0x80: raise IOError("Received character without mark parity") # None of this "".join() nonsense with byte arrays! return bytes([(x & 0x7F) for x in received_bytes]) ---- There are surely more efficient ways to do what I've typed up there, but = those should work pretty well for whatever you're looking to do. For = sending, you pretty much want to swap (x & 0x7F) with (x | 0x80) to = insert that high bit. - Dave