Convert PySerial to python 3.0

S

Seth

I am just messing around trying to get pyserial to work with 3.0.

I am stuck on this line:

if type(port) in [type(''), type(u'')]


how can I convert this to 3.0? I tried changing the u to a d that did
not do anything.

Thanks
 
C

Chris Rebert

I am just messing around trying to get pyserial to work with 3.0.

I am stuck on this line:

if type(port) in [type(''), type(u'')]


how can I convert this to 3.0? I tried changing the u to a d that did
not do anything.

Looks like it's doing the equivalent of the pre-3.0-ism
`isinstance(port, basestring)`, but in a roundabout way.
However, since basestring doesn't exist in 3.0, either:

if isinstance(port, str):

Or

if isinstance(port, bytes):

would be the appropriate replacement. Depends (obviously) on whether
'port' is supposed to be unicode or a byte sequence.
Without more context, I can't really say which is what you want.

Cheers,
Chris
 
S

Steve Holden

Seth said:
I am just messing around trying to get pyserial to work with 3.0.

I am stuck on this line:

if type(port) in [type(''), type(u'')]


how can I convert this to 3.0? I tried changing the u to a d that did
not do anything.
How about

if type(port) in (str, bytes):

Untested.

regards
Steve
 
S

Seth

I am just messing around trying to get pyserial to work with 3.0.
I am stuck on this line:
if type(port) in [type(''), type(u'')]
how can I convert this to 3.0? I tried changing the u to a d that did
not do anything.

Looks like it's doing the equivalent of the pre-3.0-ism
`isinstance(port, basestring)`, but in a roundabout way.
However, since basestring doesn't exist in 3.0, either:

if isinstance(port, str):

Or

if isinstance(port, bytes):

would be the appropriate replacement. Depends (obviously) on whether
'port' is supposed to be unicode or a byte sequence.
Without more context, I can't really say which is what you want.

Cheers,
Chris

I implemented "if isinstance(port, str): " that seems to work for now.

Currently I am running into:

err, n = win32file.WriteFile(self.hComPort, data,
self._overlappedWrite)
TypeError: expected an object with a buffer interface

Thanks
 
S

Seth

I tried all three ways you guys listed nothing seems to convert the
string to bytes.

It may have to do with the makeDeviceName function, but I can't find
where that is defined.

Any thoughts??

Here is the whole block of code:

if type(port) in (str, bytes): #strings are taken directly
Originally: if type(port) in [type(''), type(u'')]
self.portstr = port
else:
self.portstr = self.makeDeviceName(port)
 
S

Seth

This is not my code and I am fairly new to Python. I did not know how
much it would take to convert pyserial to 3.0. Someone more
knowledgeable than me could do it better and faster. I just want to
see if I could help get it to work.

I was wrong, it seems that if type(port) in (str, bytes): or isinstance
(port, str) works just fine for setting the ports.

The issue now is reading and writing the data.

I got the read() to kind of work with:
bytes(buf[:n]) replacing str(buf[:n])

but with readline() it seems to be missing the '\n' because it just
runs indefinitely( I can see the \n if I read enough bytes with read()


write seems to be related to a win32 issue. win32file.WriteFile does
not like anything I convert it to:
str gives the buffer error
bytes gives a "string argument without an encoding" error

Sorry for the confusion, I just want to be able to use all Py3k on
this project that I am working on and pyserial has not been converted
so I just started messing around with it.

Thanks for the help.

Seth




Seth said:
I tried all three ways you guys listed nothing seems to convert the
string to bytes.
It may have to do with the makeDeviceName function, but I can't find
where that is defined.
Any thoughts??
Here is the whole block of code:
if type(port) in (str, bytes):       #strings are taken directly
Originally:     if type(port) in [type(''), type(u'')]
                self.portstr = port
            else:
                self.portstr = self.makeDeviceName(port)

str and bytes are two totally unrelated things in Python 3.0. You can no
longer treat them equally like str and unicode in Python 2.x. Your could
should not work under Python 3.0 anyway. u'' is invalid in 3.x.

Also please don't use ugly type checks like type(something) == type('').
Starting with Python 2.2 you should use isinstance, for example
isinstance(number, (int, long)).

Christian
 
G

Gabriel Genellina

This is not my code and I am fairly new to Python. I did not know how
much it would take to convert pyserial to 3.0. Someone more
knowledgeable than me could do it better and faster. I just want to
see if I could help get it to work.

The last commit to pyserial repository was 8 days ago, so it's not like an
abandoned project.
Have you contacted the author?

That said, Python 3.0 is still very recent and doesn't have a big set of
3rd party libraries as earlier versions. Maybe you should stick to 2.6 or
even 2.5 for a while.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top