Discussion:
[Tutor] Pyserial and invalid handle
John Smith
2010-11-28 15:55:19 UTC
Permalink
Can anybody tell me why the handle below is invalid? I'm running Win7.

TIA,
John


Python 2.7 (r27:82525, Jul 4 2010, 07:43:08) [MSC v.1500 64 bit
(AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
import serial
ser = serial.Serial('com1', timeout = 5)
x = ser.read()
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
x = ser.read()
File "E:\Python27\lib\site-packages\serial\serialwin32.py", line 236,
in read
raise SerialException("ReadFile failed (%s)" % ctypes.WinError())
SerialException: ReadFile failed ([Error 6] The handle is invalid.)
Emile van Sebille
2010-11-28 16:57:15 UTC
Permalink
On 11/28/2010 7:55 AM John Smith said...
Post by John Smith
Can anybody tell me why the handle below is invalid? I'm running Win7.
TIA,
John
Python 2.7 (r27:82525, Jul 4 2010, 07:43:08) [MSC v.1500 64 bit (AMD64)]
on win32
Type "copyright", "credits" or "license()" for more information.
import serial
ser = serial.Serial('com1', timeout = 5)
What do you get when you add 'print ser' here? The docs at
http://pyserial.sourceforge.net/shortintro.html show you should get
something like

Serial<id=0xa81c10, open=False>(port='COM1', baudrate=19200, bytesize=8,
parity='N', stopbits=1, timeout=None, xonxoff=0, rtscts=0)

So if you're getting an invalid handle error I'd expect you'd get
something different.

Emile
Post by John Smith
x = ser.read()
File "<pyshell#2>", line 1, in <module>
x = ser.read()
File "E:\Python27\lib\site-packages\serial\serialwin32.py", line 236, in
read
raise SerialException("ReadFile failed (%s)" % ctypes.WinError())
SerialException: ReadFile failed ([Error 6] The handle is invalid.)
_______________________________________________
http://mail.python.org/mailman/listinfo/tutor
John Smith
2010-11-28 17:08:08 UTC
Permalink
Post by Emile van Sebille
On 11/28/2010 7:55 AM John Smith said...
Post by John Smith
Can anybody tell me why the handle below is invalid? I'm running Win7.
TIA,
John
Python 2.7 (r27:82525, Jul 4 2010, 07:43:08) [MSC v.1500 64 bit (AMD64)]
on win32
Type "copyright", "credits" or "license()" for more information.
Post by Emile van Sebille
import serial
ser = serial.Serial('com1', timeout = 5)
What do you get when you add 'print ser' here? The docs at
http://pyserial.sourceforge.net/shortintro.html show you should get
something like
Serial<id=0xa81c10, open=False>(port='COM1', baudrate=19200, bytesize=8,
parity='N', stopbits=1, timeout=None, xonxoff=0, rtscts=0)
So if you're getting an invalid handle error I'd expect you'd get
something different.
Emile
Hi, Emile -
Post by Emile van Sebille
Post by John Smith
Post by Emile van Sebille
print ser
Serial<id=0x224c240, open=True>(port='COM1', baudrate=9600, bytesize=8,
parity='N', stopbits=1, timeout=5, xonxoff=False, rtscts=False,
dsrdtr=False)
Thanks.
John
Walter Prins
2010-11-29 02:06:06 UTC
Permalink
John,
Python 2.7 (r27:82525, Jul 4 2010, 07:43:08) [MSC v.1500 64 bit (AMD64)]
on win32
Type "copyright", "credits" or "license()" for more information.
import serial
ser = serial.Serial('com1', timeout = 5)
x = ser.read()
File "<pyshell#2>", line 1, in <module>
x = ser.read()
File "E:\Python27\lib\site-packages\serial\serialwin32.py", line 236, in
read
raise SerialException("ReadFile failed (%s)" % ctypes.WinError())
SerialException: ReadFile failed ([Error 6] The handle is invalid.)
Ugh, you're probably not going to like this. I've done some googling and it
appears this may be a 64-bit issue with the "ctypes" module... apparently
"64-bit ctypes can only import 64-bit libraries". See here:
http://ur.ly/vSMQ

Then again, it also seems to be an open issue on the PySerial bug tracker:
http://ur.ly/vZNL

Note, the above ticket suggests that PySerial 2.4 works ok (impliedly even
on 64-bit XP, so I imagine also on Windows 7.) You should be able to try
this out by downloading the 2.4 version instead and installing it in the
same way you did the 2.5 version. In any case, it might be an idea to post
a report/add a comment to that bug report as well to maybe help get this
issue resolved.

Cheers,

Walter
Walter Prins
2010-11-29 02:16:42 UTC
Permalink
Also note this link: http://ur.ly/vVU9

It confirms that PySerial 2.4 works fine on Windows 7 64 bit. (I've also
just downloaded and checked that installing pyserial 2.4 replaces the
existing pyserial and it does on my Python installation.)

Walter
John Smith
2010-11-29 21:44:22 UTC
Permalink
Post by Walter Prins
John,
(snip stuff)
Post by Walter Prins
Ugh, you're probably not going to like this. I've done some googling
and it appears this may be a 64-bit issue with the "ctypes" module...
http://ur.ly/vSMQ
Then again, it also seems to be an open issue on the PySerial bug
tracker: http://ur.ly/vZNL
Note, the above ticket suggests that PySerial 2.4 works ok (impliedly
even on 64-bit XP, so I imagine also on Windows 7.) You should be able
to try this out by downloading the 2.4 version instead and installing it
in the same way you did the 2.5 version. In any case, it might be an
idea to post a report/add a comment to that bug report as well to maybe
help get this issue resolved.
Cheers,
Walter
Hi, Walter -

Thanks for all the research. This was my second attempt at installing
the 2.4 version. I did it thus:

E:\Python27\pyserial-2.4>..\python setup.py install
standart distutils
running install
running build
running build_py
creating build
creating build\lib
creating build\lib\serial
copying serial\serialcli.py -> build\lib\serial
copying serial\serialjava.py -> build\lib\serial
copying serial\serialposix.py -> build\lib\serial
copying serial\serialutil.py -> build\lib\serial
copying serial\serialwin32.py -> build\lib\serial
copying serial\sermsdos.py -> build\lib\serial
copying serial\__init__.py -> build\lib\serial
running install_lib
running install_egg_info
Removing E:\Python27\Lib\site-packages\pyserial-2.4-py2.7.egg-info
Writing E:\Python27\Lib\site-packages\pyserial-2.4-py2.7.egg-info

E:\Python27\pyserial-2.4>
Post by Walter Prins
import serial
ser = serial.Serial(0, timeout = 1)
ser
Serial<id=0x225c240, open=True>(port='COM1', baudrate=9600, bytesize=8,
parity='N', stopbits=1, timeout=1, xonxoff=False, rtscts=False,
dsrdtr=False)
Post by Walter Prins
ser.read()
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
ser.read()
File "E:\Python27\lib\site-packages\serial\serialwin32.py", line 236,
in read
raise SerialException("ReadFile failed (%s)" % ctypes.WinError())
SerialException: ReadFile failed ([Error 6] The handle is invalid.)
Cheers,
John
Emile van Sebille
2010-11-29 22:20:33 UTC
Permalink
On 11/29/2010 1:44 PM John Smith said...
Post by John Smith
import serial
ser = serial.Serial(0, timeout = 1)
out of curiosity, if you change the timeout above to 5....
Post by John Smith
ser
Serial<id=0x225c240, open=True>(port='COM1', baudrate=9600, bytesize=8,
parity='N', stopbits=1, timeout=1, xonxoff=False, rtscts=False,
dsrdtr=False)
ser.read()
... does the delay before printing the traceback below take about 5 seconds?
Post by John Smith
File "<pyshell#3>", line 1, in <module>
ser.read()
File "E:\Python27\lib\site-packages\serial\serialwin32.py", line 236, in
read
raise SerialException("ReadFile failed (%s)" % ctypes.WinError())
SerialException: ReadFile failed ([Error 6] The handle is invalid.)
Emile
John Smith
2010-11-29 23:25:04 UTC
Permalink
Post by Emile van Sebille
On 11/29/2010 1:44 PM John Smith said...
Post by John Smith
import serial
ser = serial.Serial(0, timeout = 1)
out of curiosity, if you change the timeout above to 5....
Post by John Smith
ser
Serial<id=0x225c240, open=True>(port='COM1', baudrate=9600, bytesize=8,
parity='N', stopbits=1, timeout=1, xonxoff=False, rtscts=False,
dsrdtr=False)
ser.read()
... does the delay before printing the traceback below take about 5 seconds?
No. There is no delay regardless of the timeout setting.

John
Emile van Sebille
2010-11-29 23:56:06 UTC
Permalink
On 11/29/2010 3:25 PM John Smith said...
Post by John Smith
Post by Emile van Sebille
On 11/29/2010 1:44 PM John Smith said...
Post by John Smith
import serial
ser = serial.Serial(0, timeout = 1)
out of curiosity, if you change the timeout above to 5....
Post by John Smith
ser
Serial<id=0x225c240, open=True>(port='COM1', baudrate=9600, bytesize=8,
parity='N', stopbits=1, timeout=1, xonxoff=False, rtscts=False,
dsrdtr=False)
ser.read()
... does the delay before printing the traceback below take about 5 seconds?
No. There is no delay regardless of the timeout setting.
Post by Emile van Sebille
File "<pyshell#3>", line 1, in <module>
ser.read()
File "E:\Python27\lib\site-packages\serial\serialwin32.py", line
236, in read
raise SerialException("ReadFile failed (%s)" % ctypes.WinError())
SerialException: ReadFile failed ([Error 6] The handle is invalid.)
Hmmm... any chance you don't have administrative rights on the account
performing this? I never got to Win7 (having stopped at XP) but I know
it's got a reputation for excessive permission asking.

Otherwise, I'd take this up on the main list. Chris Liechti, the
[author|current maintainer|significant contributor] of pyserial monitors
that list and would probably be interested in diagnosing what you're
describing. You could also ask him as per the 'send me a message' link
on his sourceforge page at

http://sourceforge.net/sendmessage.php?touser=403744

Emile
John Smith
2010-11-30 02:21:56 UTC
Permalink
On 11/29/2010 5:56 PM, Emile van Sebille wrote:
(snip)
Post by Emile van Sebille
Hmmm... any chance you don't have administrative rights on the account
performing this? I never got to Win7 (having stopped at XP) but I know
it's got a reputation for excessive permission asking.
You're right about that. It's like Win7 is paranoid. But, it tells me
when I need to supply administrative permission. Some day I'll find the
button that tells the system that nobody else uses this computer and to
shut up and get on with it.
Post by Emile van Sebille
Otherwise, I'd take this up on the main list. Chris Liechti, the
[author|current maintainer|significant contributor] of pyserial monitors
that list and would probably be interested in diagnosing what you're
describing. You could also ask him as per the 'send me a message' link
on his sourceforge page at
http://sourceforge.net/sendmessage.php?touser=403744
Emile
I'll consider that, Emile. First, though, I would like to hear from
Walter again after my last post.

Thanks for your suggestions.

Cheers,
John
Rance Hall
2010-11-30 03:41:17 UTC
Permalink
Post by John Smith
(snip)
Post by Emile van Sebille
Hmmm... any chance you don't have administrative rights on the account
performing this? I never got to Win7 (having stopped at XP) but I know
it's got a reputation for excessive permission asking.
You're right about that. It's like Win7 is paranoid. But, it tells me when I
need to supply administrative permission. Some day I'll find the button that
tells the system that nobody else uses this computer and to shut up and get
on with it.
Just so you know, Its called User Account Control or UAC. Google for
"disabling UAC on Windows 7" and you can find a tutorial or two on how
to make your computer "shut up and get on with it."
John Smith
2010-11-30 03:51:16 UTC
Permalink
Post by Rance Hall
Post by John Smith
(snip)
Post by Emile van Sebille
Hmmm... any chance you don't have administrative rights on the account
performing this? I never got to Win7 (having stopped at XP) but I know
it's got a reputation for excessive permission asking.
You're right about that. It's like Win7 is paranoid. But, it tells me when I
need to supply administrative permission. Some day I'll find the button that
tells the system that nobody else uses this computer and to shut up and get
on with it.
Just so you know, Its called User Account Control or UAC. Google for
"disabling UAC on Windows 7" and you can find a tutorial or two on how
to make your computer "shut up and get on with it."
Hey, thanks Rance. The info is appreciated.

Cheers,
John
Walter Prins
2010-11-30 16:37:34 UTC
Permalink
Hello John
Post by John Smith
Hi, Walter -
Thanks for all the research. This was my second attempt at installing the
E:\Python27\pyserial-2.4>..\python setup.py install
standart distutils
running install
running build
running build_py
creating build
creating build\lib
creating build\lib\serial
copying serial\serialcli.py -> build\lib\serial
copying serial\serialjava.py -> build\lib\serial
copying serial\serialposix.py -> build\lib\serial
copying serial\serialutil.py -> build\lib\serial
copying serial\serialwin32.py -> build\lib\serial
copying serial\sermsdos.py -> build\lib\serial
copying serial\__init__.py -> build\lib\serial
running install_lib
running install_egg_info
Removing E:\Python27\Lib\site-packages\pyserial-2.4-py2.7.egg-info
Writing E:\Python27\Lib\site-packages\pyserial-2.4-py2.7.egg-info
E:\Python27\pyserial-2.4>
import serial
ser = serial.Serial(0, timeout = 1)
ser
Serial<id=0x225c240, open=True>(port='COM1', baudrate=9600, bytesize=8,
parity='N', stopbits=1, timeout=1, xonxoff=False, rtscts=False,
dsrdtr=False)
ser.read()
File "<pyshell#3>", line 1, in <module>
ser.read()
File "E:\Python27\lib\site-packages\serial\serialwin32.py", line 236, in
read
raise SerialException("ReadFile failed (%s)" % ctypes.WinError())
SerialException: ReadFile failed ([Error 6] The handle is invalid.)
I've checked and I think it was a mistake to suggest installing pyserial 2.4
on top of 2.5 without first removing 2.5 explicitly. It appears that doing
so actually still retains the previous version of serialwin32.py in use,
based on the fact that I've manually had a look at serialwin32.py from both
pyserial2.4 and pyserial2.5 and that fact that line 236 in pyserial2.5 is
where the error is raised, and pySerial2.4 by contrast has "if err: #will be
ERROR_IO_PENDING:" on line 236. Consequently this implies that you're still
using pyserial2.5 above. (Aside: I originally tested my suggestion for
installing pyserial2.4 and tested it by importing the module (e.g. "import
serial") and then did a "help(serial)" which gave me the impression of it
having done the right thing and using pyserial2.4, but apparently that's not
definitve, or I made a mistake somewhere along the line and got the wrong
end of the stick.)

So. My apologies, but I think that suggestion has added to the confusion.

In any case, to fix it let's delete all instances of pySerial and then
install it again, as follows:

1.) Open up your Python "site-packages" folder in Windows Explorer, e.g.
open up:
E:\Python27\lib\site-packages

2.) Delete any folder named "serial" that you may see.

3.) Delete any *file* name pyserial*.* that you may see, probably you'll see
"pyserial-2.4-py2.7.egg", there may also be an info file.

4.) Open up a Python shell and confirm that you can't import pyserial
anymore (e.g. "import serial" fails with e.g. "ImportError: No module named
serial". If it still imports then you still have some vestiges of the
existing pyserial installation left over.

5.) After confirming the previous versions are gone, please try reinstalling
it again from scratch. (E.g. extract source to some suitable place and run
"python setup.py install" from there, which copies the required files into
site-packages etc.)

6.) After installing, confirm "import serial" works again, then try your
test again.

Apologies again for adding to the confusion, and hopefully we're getting
closer. :-(

Walter
John Smith
2010-11-30 16:57:57 UTC
Permalink
Post by Walter Prins
Hello John
(snip)
Post by Walter Prins
In any case, to fix it let's delete all instances of pySerial and then
1.) Open up your Python "site-packages" folder in Windows Explorer, e.g.
E:\Python27\lib\site-packages
2.) Delete any folder named "serial" that you may see.
3.) Delete any *file* name pyserial*.* that you may see, probably you'll
see "pyserial-2.4-py2.7.egg", there may also be an info file.
4.) Open up a Python shell and confirm that you can't import pyserial
anymore (e.g. "import serial" fails with e.g. "ImportError: No module
named serial". If it still imports then you still have some vestiges of
the existing pyserial installation left over.
5.) After confirming the previous versions are gone, please try
reinstalling it again from scratch. (E.g. extract source to some
suitable place and run "python setup.py install" from there, which
copies the required files into site-packages etc.)
6.) After installing, confirm "import serial" works again, then try your
test again.
Apologies again for adding to the confusion, and hopefully we're getting
closer. :-(
Walter
Hi, Walter -
Post by Walter Prins
import serial
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import serial
File "E:\Python27\lib\site-packages\serial\__init__.py", line 18, in
<module>
from serialwin32 import *
File "E:\Python27\lib\site-packages\serial\serialwin32.py", line 9,
in <module>
import win32file # The base COM port and file IO functions.
ImportError: No module named win32file
I guess that file was included in 2.5 but not in 2.4?

Thanks,
John
Walter Prins
2010-12-01 00:23:19 UTC
Permalink
Hello John,
Post by John Smith
Hi, Walter -
import serial
File "<pyshell#0>", line 1, in <module>
import serial
File "E:\Python27\lib\site-packages\serial\__init__.py", line 18, in
<module>
from serialwin32 import *
File "E:\Python27\lib\site-packages\serial\serialwin32.py", line 9, in
<module>
import win32file # The base COM port and file IO functions.
ImportError: No module named win32file
I guess that file was included in 2.5 but not in 2.4?
Apparently so. Well, win32file is part of the PyWin32 package, which are a
set of modules that wrap many Windows API's. I'm not sure why it
was't/isn't required for PySerial 2.5 or whether as you say perhaps this
module is included in PySerial2.5 and isn't in 2.4.

But whatever the case may be, suffice it to say I've reproduced your issue
on my Win7 64bit box, and then resolved it by installing the PyWin32
modules. It's probably a good idea to install this package anyway -- if
you're working on Windows the PyWin32 modules are very useful - they
basically wrap and makes available a shedload of Windows specific API's to
Python. (Many people working with Python on Windows almost automatically
would install this, it's also why i didn't run into this issue in the first
place as I already had PyWin32 installed prior to testing my suggestion.
Sorry.)

Anyway. To download and install PyWin32, go here: http://ur.ly/vLwv

Presumably you want the AMD64 (64 bit) Py2.7 version. Install it then try
your test again.

Fingers crossed. ;)

Walter
John Smith
2010-12-01 01:00:58 UTC
Permalink
Post by Walter Prins
Hello John,
(snip)
Post by Walter Prins
Apparently so. Well, win32file is part of the PyWin32 package, which
are a set of modules that wrap many Windows API's. I'm not sure why it
was't/isn't required for PySerial 2.5 or whether as you say perhaps this
module is included in PySerial2.5 and isn't in 2.4.
But whatever the case may be, suffice it to say I've reproduced your
issue on my Win7 64bit box, and then resolved it by installing the
PyWin32 modules. It's probably a good idea to install this package
anyway -- if you're working on Windows the PyWin32 modules are very
useful - they basically wrap and makes available a shedload of Windows
specific API's to Python. (Many people working with Python on Windows
almost automatically would install this, it's also why i didn't run into
this issue in the first place as I already had PyWin32 installed prior
to testing my suggestion. Sorry.)
Anyway. To download and install PyWin32, go here: http://ur.ly/vLwv
Presumably you want the AMD64 (64 bit) Py2.7 version. Install it then
try your test again.
Fingers crossed. ;)
Walter
Hi, Walter -

I got pywin32-214.win32-py2.7.exe because I have the Intel i7 (I'm
guessing that the AMD versions are for the AMD processor). However, all
of the exe offerings have the same "Python not found in registry"
problem that started this whole thing.

So, since the only source module available is pywin32-214.zip, I got it
and installed it. It does not work, maybe because I'm using Python 2.7
and the zip is for 3.2.

I really appreciate all the time you have put into my problems, Walter.
Thank you.

Cheers,
John
Adam Bark
2010-12-01 01:27:22 UTC
Permalink
Post by John Smith
Hi, Walter -
I got pywin32-214.win32-py2.7.exe because I have the Intel i7 (I'm
guessing that the AMD versions are for the AMD processor). However,
all of the exe offerings have the same "Python not found in registry"
problem that started this whole thing.
So, since the only source module available is pywin32-214.zip, I got
it and installed it. It does not work, maybe because I'm using Python
2.7 and the zip is for 3.2.
I really appreciate all the time you have put into my problems,
Walter. Thank you.
Cheers,
John
Actually, AMD 64 is now the standard x86-64. It was originally designed
by AMD because intel were making their Itanium thing but that didn't go
so well. Anyway if you're running 64 bit windows that's probably why the
32-bit python install is having a problem. Download the version Walter
suggested and you should be good to go.
John Smith
2010-12-01 01:52:40 UTC
Permalink
Post by Adam Bark
Post by John Smith
Hi, Walter -
I got pywin32-214.win32-py2.7.exe because I have the Intel i7 (I'm
guessing that the AMD versions are for the AMD processor). However,
all of the exe offerings have the same "Python not found in registry"
problem that started this whole thing.
So, since the only source module available is pywin32-214.zip, I got
it and installed it. It does not work, maybe because I'm using Python
2.7 and the zip is for 3.2.
I really appreciate all the time you have put into my problems,
Walter. Thank you.
Cheers,
John
Actually, AMD 64 is now the standard x86-64. It was originally designed
by AMD because intel were making their Itanium thing but that didn't go
so well. Anyway if you're running 64 bit windows that's probably why the
32-bit python install is having a problem. Download the version Walter
suggested and you should be good to go.
Yes!

I have gone no farther than to say ser.read() knowing that nothing is
attached to the port and expected a delay of 5 seconds. It now does
that, so I have a clue that it is working.

I had no idea that the AMD thing was now standard. Thanks for that. I
also found that the file Walter recommended did install from the exe
while the non-AMD file did not due to the registry thing.

Wow! All I can say is thanks to everybody for the help. Now I need to
start trying to get a modem to talk to me.

By the way, the whole purpose of doing this is to communicate with some
test instruments via GPIB/HPIB to automate some testing that is time
consuming. The last time I did this (using a BASIC program), it took
about 45 minutes (not due to BASIC, but due to instrument response
time). I was able to start the test, go to lunch, then analyze the data
when I returned.

Thanks again for the help.

Cheers,
John
Terry Carroll
2010-12-03 00:26:58 UTC
Permalink
Post by Walter Prins
But whatever the case may be, suffice it to say I've reproduced your issue
on my Win7 64bit box, and then resolved it by installing the PyWin32
modules.
I'd like to put in a plug for Activestate Python here. Activestate has a
free distribution of Python for Windows that not only includes the basic
Python program and libraries, but also commonly used extras including the
Win32 modules.

I've always used Activestate Python from the start, and recommend it.

They have distributions for both Python 2 and 3, each both in 32-bit and
64-bit.

http://www.activestate.com/activepython/downloads

I have no association with Activestate, I'm just a satisfied customer.
Well, not even a customer. I'm a satisfied freeloader.

Loading...