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


Groups > comp.lang.python > #107682

Re: How to read from serial port?

From Grant Edwards <grant.b.edwards@gmail.com>
Newsgroups comp.lang.python
Subject Re: How to read from serial port?
Date 2016-04-26 18:29 +0000
Message-ID <mailman.124.1461695415.32212.python-list@python.org> (permalink)
References <41302A7145AC054FA7A96CFD03835A0A0BAC66F9@EX10MBX02.EU.NEC.COM> <nfoc30$cr8$1@ger.gmane.org>

Show all headers | 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