Re: PySerial for Python 2 vs. Python 3

Discussion in 'Python' started by Terry Reedy, Jan 1, 2014.

  1. Terry Reedy

    Terry Reedy Guest

    On 1/1/2014 1:48 AM, Chris Angelico wrote:
    > On Wed, Jan 1, 2014 at 5:39 PM, Travis McGee <> 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
    Terry Reedy, Jan 1, 2014
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Seth
    Replies:
    6
    Views:
    3,164
    Gabriel Genellina
    Feb 25, 2009
  2. Terry Reedy

    Re: python 2.7.1 "serial" vs "pyserial"

    Terry Reedy, Mar 25, 2011, in forum: Python
    Replies:
    0
    Views:
    575
    Terry Reedy
    Mar 25, 2011
  3. Devin Jeanpierre

    Re: PySerial for Python 2 vs. Python 3

    Devin Jeanpierre, Jan 1, 2014, in forum: Python
    Replies:
    0
    Views:
    61
    Devin Jeanpierre
    Jan 1, 2014
  4. Chris Angelico

    Re: PySerial for Python 2 vs. Python 3

    Chris Angelico, Jan 1, 2014, in forum: Python
    Replies:
    0
    Views:
    63
    Chris Angelico
    Jan 1, 2014
  5. Steven D'Aprano

    Re: PySerial for Python 2 vs. Python 3

    Steven D'Aprano, Jan 1, 2014, in forum: Python
    Replies:
    0
    Views:
    58
    Steven D'Aprano
    Jan 1, 2014
Loading...

Share This Page