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


Groups > comp.lang.python > #107682 > unrolled thread

Re: How to read from serial port?

Started byGrant Edwards <grant.b.edwards@gmail.com>
First post2016-04-26 18:29 +0000
Last post2016-04-26 18:29 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#107682 — Re: How to read from serial port?

FromGrant Edwards <grant.b.edwards@gmail.com>
Date2016-04-26 18:29 +0000
SubjectRe: How to read from serial port?
Message-ID<mailman.124.1461695415.32212.python-list@python.org>
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!!

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web