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


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

PySerial for Python 2 vs. Python 3

Started byTravis McGee <nobody@gmail.com>
First post2014-01-01 01:39 -0500
Last post2014-01-01 14:54 -0500
Articles 5 — 5 participants

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


Contents

  PySerial for Python 2 vs. Python 3 Travis McGee <nobody@gmail.com> - 2014-01-01 01:39 -0500
    Re: PySerial for Python 2 vs. Python 3 Devin Jeanpierre <jeanpierreda@gmail.com> - 2013-12-31 22:43 -0800
    Re: PySerial for Python 2 vs. Python 3 Chris Angelico <rosuav@gmail.com> - 2014-01-01 17:48 +1100
    Re: PySerial for Python 2 vs. Python 3 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-01-01 17:57 +1100
    Re: PySerial for Python 2 vs. Python 3 Terry Reedy <tjreedy@udel.edu> - 2014-01-01 14:54 -0500

#62947 — PySerial for Python 2 vs. Python 3

FromTravis McGee <nobody@gmail.com>
Date2014-01-01 01:39 -0500
SubjectPySerial for Python 2 vs. Python 3
Message-ID<SKOwu.77170$VG.23751@fx12.iad>
I've been working with a simple serial device that attaches to a USB 
port. It takes as commands short strings.

I wanted to use PySerial under Python 3, and, of course had the Devil's 
own time getting it installed and working since everything is geared 
towards Python 2.

Anyway, I finally got it installed, but when I try to use a statement of 
the sort ser.write("string") I get an exception which seems to imply 
that the argument needs to be an integer, rather than a string.

With some minor adjustments it works just fine under Python 2, so, in a 
sense, this is a non-issue. However, I'd be interested to hear from 
anyone who can comment on what the problem is.

Thanks,

Travis

[toc] | [next] | [standalone]


#62948

FromDevin Jeanpierre <jeanpierreda@gmail.com>
Date2013-12-31 22:43 -0800
Message-ID<mailman.4772.1388558657.18130.python-list@python.org>
In reply to#62947
On Tue, Dec 31, 2013 at 10:39 PM, Travis McGee <nobody@gmail.com> wrote:
> Anyway, I finally got it installed, but when I try to use a statement of the
> sort ser.write("string") I get an exception which seems to imply that the
> argument needs to be an integer, rather than a string.

You will get the most help if you provide a minimal, self-contained,
runnable example, and an example of its exact output.

-- Devin

[toc] | [prev] | [next] | [standalone]


#62949

FromChris Angelico <rosuav@gmail.com>
Date2014-01-01 17:48 +1100
Message-ID<mailman.4773.1388558890.18130.python-list@python.org>
In reply to#62947
On Wed, Jan 1, 2014 at 5:39 PM, Travis McGee <nobody@gmail.com> wrote:
> Anyway, I finally got it installed, but when I try to use a statement of the
> sort ser.write("string") I get an exception which seems to imply that the
> argument needs to be an integer, rather than a string.

Quoting the full exception would help!

My suspicion is that it works with byte strings, not Unicode strings.
So you could do:

ser.write(b"string")

or:

ser.write("string".encode())

to turn it into a stream of bytes (the latter uses UTF-8, the former
would use your source file encoding).

ChrisA

[toc] | [prev] | [next] | [standalone]


#62951

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-01-01 17:57 +1100
Message-ID<52c3bc64$0$29967$c3e8da3$5496439d@news.astraweb.com>
In reply to#62947
Travis McGee wrote:

> I've been working with a simple serial device that attaches to a USB
> port. It takes as commands short strings.
> 
> I wanted to use PySerial under Python 3, and, of course had the Devil's
> own time getting it installed and working since everything is geared
> towards Python 2.
> 
> Anyway, I finally got it installed, but when I try to use a statement of
> the sort ser.write("string") I get an exception which seems to imply
> that the argument needs to be an integer, rather than a string.

"Seems to imply"?


> With some minor adjustments it works just fine under Python 2, so, in a
> sense, this is a non-issue. However, I'd be interested to hear from
> anyone who can comment on what the problem is.

While I'd love to tell you exactly what the problem is, my crystal ball is
out of action. If you'd be so kind as to copy and paste the actual
exception, assuming it isn't a secret, perhaps we'll be able to tell you
what it actually says.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#62968

FromTerry Reedy <tjreedy@udel.edu>
Date2014-01-01 14:54 -0500
Message-ID<mailman.4784.1388606061.18130.python-list@python.org>
In reply to#62947
On 1/1/2014 1:48 AM, Chris Angelico wrote:
> On Wed, Jan 1, 2014 at 5:39 PM, Travis McGee <nobody@gmail.com> wrote:

What OS? If Windows, did you install the -py3k version for 3.x?

>> Anyway, I finally got it installed, but when I try to use a statement of the
>> sort ser.write("string") I get an exception which seems to imply that the
>> argument needs to be an integer, rather than a string.

According to a Stackoverflow issue, .write(n) will write n 0 bytes 
because it will send bytes(n) == n * bytes(b'\0').

PySerial is written in Python, so you could look at the .write method of 
the Serial class (presuming that 'ser' is an instance thereof) to see 
what it does.

> Quoting the full exception would help!
>
> My suspicion is that it works with byte strings, not Unicode strings.

That is what the doc either says or implies.

> So you could do:
>
> ser.write(b"string")
>
> or:
>
> ser.write("string".encode())
>
> to turn it into a stream of bytes (the latter uses UTF-8, the former
> would use your source file encoding).

-- 
Terry Jan Reedy

[toc] | [prev] | [standalone]


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


csiph-web