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


Groups > comp.lang.python > #16031

Re: Non-POSIX parity (mark/space) with Python-Serial on Linux.

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 <fraveydank@gmail.com>
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 <fraveydank@gmail.com>
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> <mailman.2900.1321885611.27778.python-list@python.org> <2723247.1219.1321892899941.JavaMail.geo-discussion-forums@yqzz20> <mailman.2912.1321894079.27778.python-list@python.org> <22753805.1034.1321894337412.JavaMail.geo-discussion-forums@yqdr22>
To Matthew Lenz <matthew@nocturnal.org>, 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 <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.2917.1321896151.27778.python-list@python.org> (permalink)
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

Show key headers only | View raw


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 = receive_my_string()

ord_str = [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 = 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

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


Thread

Non-POSIX parity (mark/space) with Python-Serial on Linux. mlenz@nocturnal.org - 2011-11-21 06:00 -0800
  Re: Non-POSIX parity (mark/space) with Python-Serial on Linux. Matthew Lenz <matthew@nocturnal.org> - 2011-11-21 06:16 -0800
  Re: Non-POSIX parity (mark/space) with Python-Serial on Linux. Nizamov Shawkat <nizamov.shawkat@gmail.com> - 2011-11-21 15:26 +0100
    Re: Non-POSIX parity (mark/space) with Python-Serial on Linux. Matthew Lenz <matthew@nocturnal.org> - 2011-11-21 08:28 -0800
      Re: Non-POSIX parity (mark/space) with Python-Serial on Linux. Chris Angelico <rosuav@gmail.com> - 2011-11-22 03:41 +1100
      Re: Non-POSIX parity (mark/space) with Python-Serial on Linux. David Riley <fraveydank@gmail.com> - 2011-11-21 11:47 -0500
        Re: Non-POSIX parity (mark/space) with Python-Serial on Linux. Matthew Lenz <matthew@nocturnal.org> - 2011-11-21 08:52 -0800
        Re: Non-POSIX parity (mark/space) with Python-Serial on Linux. Matthew Lenz <matthew@nocturnal.org> - 2011-11-21 08:52 -0800
          Re: Non-POSIX parity (mark/space) with Python-Serial on Linux. David Riley <fraveydank@gmail.com> - 2011-11-21 12:22 -0500
            Re: Non-POSIX parity (mark/space) with Python-Serial on Linux. Matthew Lenz <matthew@nocturnal.org> - 2011-11-21 09:59 -0800
            Re: Non-POSIX parity (mark/space) with Python-Serial on Linux. Matthew Lenz <matthew@nocturnal.org> - 2011-11-21 09:59 -0800
              Re: Non-POSIX parity (mark/space) with Python-Serial on Linux. David Riley <fraveydank@gmail.com> - 2011-11-21 13:12 -0500
          Re: Non-POSIX parity (mark/space) with Python-Serial on Linux. MRAB <python@mrabarnett.plus.com> - 2011-11-21 18:20 +0000
      Re: Non-POSIX parity (mark/space) with Python-Serial on Linux. gene heskett <gheskett@wdtv.com> - 2011-11-21 12:25 -0500
      Re: Non-POSIX parity (mark/space) with Python-Serial on Linux. David Riley <fraveydank@gmail.com> - 2011-11-21 12:50 -0500
    Re: Non-POSIX parity (mark/space) with Python-Serial on Linux. Matthew Lenz <matthew@nocturnal.org> - 2011-11-21 08:28 -0800
  Re: Non-POSIX parity (mark/space) with Python-Serial on Linux. gene heskett <gheskett@wdtv.com> - 2011-11-21 13:33 -0500
    Re: Non-POSIX parity (mark/space) with Python-Serial on Linux. Matthew Lenz <matthew@nocturnal.org> - 2011-11-21 11:29 -0800
      Re: Non-POSIX parity (mark/space) with Python-Serial on Linux. Grant Edwards <invalid@invalid.invalid> - 2011-11-21 19:42 +0000
    Re: Non-POSIX parity (mark/space) with Python-Serial on Linux. Matthew Lenz <matthew@nocturnal.org> - 2011-11-21 11:29 -0800
      Re: Non-POSIX parity (mark/space) with Python-Serial on Linux. David Riley <fraveydank@gmail.com> - 2011-11-21 15:42 -0500
        Re: Non-POSIX parity (mark/space) with Python-Serial on Linux. Grant Edwards <invalid@invalid.invalid> - 2011-11-21 23:08 +0000
          Re: Non-POSIX parity (mark/space) with Python-Serial on Linux. Grant Edwards <invalid@invalid.invalid> - 2011-11-21 23:09 +0000
            Re: Non-POSIX parity (mark/space) with Python-Serial on Linux. Chris Angelico <rosuav@gmail.com> - 2011-11-22 11:32 +1100

csiph-web