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


Groups > comp.lang.python > #107682

Re: How to read from serial port?

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Grant Edwards <grant.b.edwards@gmail.com>
Newsgroups comp.lang.python
Subject Re: How to read from serial port?
Date Tue, 26 Apr 2016 18:29:53 +0000 (UTC)
Lines 40
Message-ID <mailman.124.1461695415.32212.python-list@python.org> (permalink)
References <41302A7145AC054FA7A96CFD03835A0A0BAC66F9@EX10MBX02.EU.NEC.COM> <nfoc30$cr8$1@ger.gmane.org>
X-Trace news.uni-berlin.de Z1xeX1jVoIjIibY8JozKxg20Id/vUV4KjoR25MNumHxQ==
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.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; "'python": 0.07; 'subject:How': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:qwest.net': 0.09; 'typeerror:': 0.09; 'encoding': 0.15; 'decode': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'subject:port': 0.16; 'true:': 0.16; 'wrote:': 0.16; 'bytes': 0.18; 'string,': 0.18; 'this:': 0.23; "haven't": 0.24; 'import': 0.24; '(most': 0.24; 'url:view': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'skip:# 10': 0.27; 'specify': 0.27; 'implicitly': 0.29; 'str': 0.29; 'convert': 0.29; 'skip:s 30': 0.31; "can't": 0.32; 'run': 0.33; 'url:python': 0.33; 'sexual': 0.33; 'traceback': 0.33; 'file': 0.34; 'but': 0.36; 'should': 0.36; 'url:org': 0.36; '(and': 0.36; 'url:library': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'skip:p 20': 0.38; 'skip:o 20': 0.38; 'google': 0.39; 'data': 0.39; 'subject:from': 0.39; 'to:addr:python.org': 0.40; 'url:3': 0.60; 'six': 0.65; 'day': 0.67; 'serial': 0.70; 'subject:read': 0.84; 'edwards': 0.91; 'married': 0.96
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host 67-130-15-94.dia.static.qwest.net
User-Agent slrn/1.0.2 (Linux)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <nfoc30$cr8$1@ger.gmane.org>
X-Mailman-Original-References <41302A7145AC054FA7A96CFD03835A0A0BAC66F9@EX10MBX02.EU.NEC.COM>
Xref csiph.com comp.lang.python:107682

Show key headers only | View raw


On 2016-04-26, David Aldrich <David.Aldrich@EMEA.NEC.COM> wrote:

> #!/usr/bin/python3
> import serial
>
> ser=serial.Serial('COM1',115200)
> while True:
>     out = ser.read()
>     print('Receiving...'+out)
>
> When I run it and send data for it to read I get:

> Traceback (most recent call last):
>   File "serial_read.py", line 9, in <module>
>     print('Receiving...'+out)
> TypeError: Can't convert 'bytes' object to str implicitly

Try this:

  print('Receiving...',out)

If you want to convert the bytes you read from the serial port into a
string, you need to decode they bytes (and you have to specify what
encoding to use):

  print('Receiving...' + out.decode('ascii'))

If you google 'Python 3 bytes strings' you should find a bunch of info:

https://docs.python.org/3.5/library/stdtypes.html
http://www.diveintopython3.net/strings.html
http://pythoncentral.io/encoding-and-decoding-strings-in-python-3-x/
https://www.safaribooksonline.com/library/view/fluent-python/9781491946237/ch04.html

-- 
Grant Edwards               grant.b.edwards        Yow! I haven't been married
                                  at               in over six years, but we
                              gmail.com            had sexual counseling every
                                                   day from Oral Roberts!!

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


Thread

Re: How to read from serial port? Grant Edwards <grant.b.edwards@gmail.com> - 2016-04-26 18:29 +0000

csiph-web