Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Grant Edwards 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: References: <41302A7145AC054FA7A96CFD03835A0A0BAC66F9@EX10MBX02.EU.NEC.COM> X-Trace: news.uni-berlin.de Z1xeX1jVoIjIibY8JozKxg20Id/vUV4KjoR25MNumHxQ== Return-Path: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <41302A7145AC054FA7A96CFD03835A0A0BAC66F9@EX10MBX02.EU.NEC.COM> Xref: csiph.com comp.lang.python:107682 On 2016-04-26, David Aldrich 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 > 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!!