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: References: <41302A7145AC054FA7A96CFD03835A0A0BAC66F9@EX10MBX02.EU.NEC.COM> 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: 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 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:107680 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 > 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 "", line 1, in 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 .