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


Groups > comp.lang.python > #107680

Re: How to read from serial port?

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: How to read from serial port?
Date Tue, 26 Apr 2016 19:58:19 +0200
Organization None
Lines 54
Message-ID <mailman.122.1461693520.32212.python-list@python.org> (permalink)
References <41302A7145AC054FA7A96CFD03835A0A0BAC66F9@EX10MBX02.EU.NEC.COM> <nfoa7v$mbc$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de EyLv5sQ+zwfq+Czp+ohvrAcDAnq44fLOmJoMRu/IfGaw==
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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:How': 0.09; 'bytes,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'typeerror:': 0.09; 'url:unicode': 0.09; 'python': 0.10; '<>.': 0.11; 'concatenate': 0.16; 'decode': 0.16; 'executed,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:port': 0.16; 'true:': 0.16; 'wrote:': 0.16; 'string': 0.17; 'bytes': 0.18; '>>>': 0.20; 'fix': 0.21; '"",': 0.22; 'please?': 0.22; 'trying': 0.22; 'import': 0.24; '(most': 0.24; 'written': 0.24; 'header:User-Agent:1': 0.26; 'header:X -Complaints-To:1': 0.26; 'error': 0.27; 'skip:# 10': 0.27; 'implicitly': 0.29; 'str': 0.29; 'convert': 0.29; 'print': 0.30; 'skip:s 30': 0.31; "can't": 0.32; 'run': 0.33; 'url:python': 0.33; 'foo': 0.33; 'traceback': 0.33; 'file': 0.34; 'text': 0.35; 'url:org': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'stuff': 0.38; 'skip:p 20': 0.38; 'data': 0.39; 'subject:from': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'url:3': 0.60; 'avoid': 0.61; 'more': 0.63; 'serial': 0.70; 'treat': 0.72; 'subject:read': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd8a17.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
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 <nfoa7v$mbc$1@ger.gmane.org>
X-Mailman-Original-References <41302A7145AC054FA7A96CFD03835A0A0BAC66F9@EX10MBX02.EU.NEC.COM>
Xref csiph.com comp.lang.python:107680

Show key headers only | View raw


David Aldrich wrote:

> Hi
> 
> I have written a very simple program to read and print data from the
> serial port using pyserial:
> 
> #!/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:
> 
> C:\SVNProj\Raggio\trunk\hostconsole\gui\prototypes\serial_test>py
> serial_read.py 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
> 
> I am using Python 3.5.  How would I fix this error please?

Look at the traceback again. The line

>     out = ser.read()

is executed, you are reading successfully. What fails is

>     print('Receiving...'+out)

You are trying to concatenate a (unicode) string and bytes, like in

>>> print("foo" + b"bar")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't convert 'bytes' object to str implicitly

You can avoid that by printing the string and the bytes independently

>>> print("foo", b"bar")
foo b'bar'

If you don't like the b"..." stuff and want to treat the bytes as text 
rather than data you can decode them:

>>> print("foo", b"bar".decode())
foo bar

For more see <https://docs.python.org/3/howto/unicode.html>.

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


Thread

Re: How to read from serial port? Peter Otten <__peter__@web.de> - 2016-04-26 19:58 +0200

csiph-web