Pyserial example program error: win32file.SetupComm reports 'Incorrect function.'

Ron Jackson Ron at FascinatingElectronics.com
Thu Feb 8 16:04:52 EST 2007


Dennis Lee Bieber wrote:
> On Wed, 07 Feb 2007 11:14:39 -0800, Ron Jackson
> <Ron at FascinatingElectronics.com> declaimed the following in
> comp.lang.python:
> 
> 
>>I am using Python 2.5 on Windows XP. I have installed Pyserial and
>>win32all extensions.
>>
> 
> 	2.4 on XP Pro SP2...
> 
> 
>>When I try to run the example program scan.py (included below), or any 
>>other program using pyserial, as soon as it hits the statement:
>>
>>s = serial.Serial(i)
>>
> 
> 
>>>>import serial
>>>>for i in range(256):
> 
> ... 	try:
> ... 		print i,
> ... 		s = serial.Serial(i)
> ... 		print s.portstr
> ... 		s.close()
> ... 	except serial.SerialException:
> ... 		print
> ... 		
> 0 COM1
> 1
> 2 COM3
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> 10
> 11
> and on and on...
> 
> 
>>What do I need to do to fix this? Thanks for the help!
> 
> 
> 	Does the serial port module require a compile for use with 2.5?
> Well, with only one download since Python 2.2, guess not...
> 
> 	Something glitched in win32? Sorry, I don't know... However, since
> those are Python source files, you could always plug in some debugging
> lines around that win32 call to see what actually is there. Do you have
> any unnatural serial ports on the machine? (Like a USB<>serial
> converter?)

Trying your program, I get the same error 'Incorrect function.':

Traceback (most recent call last):
   File "<pyshell#9>", line 4, in <module>
     s = serial.Serial(i)
   File "C:\Python25\Lib\site-packages\serial\serialutil.py", line 156, in __init__
     self.open()
   File "C:\Python25\lib\site-packages\serial\serialwin32.py", line 57, in open
     win32file.SetupComm(self.hComPort, 4096, 4096)
error: (1, 'SetupComm', 'Incorrect function.')


I tried PySerial on a laptop, also running XP Home SP2, and both the example 
program and the program you suggested work fine on the laptop.

The desktop computer that is giving me the error doesn't have any unnatural 
serial ports on it currently. The laptop worked fine, either with a USB device 
emulating COMM6 present or with the USB device disconnected.

I checked and both machines are running the same version of win32file, which is 
site-packages\win32\win32file.pyd, 88 KB dated 9/22/2006.

So my question is:

Why would the statement win32file.SetupComm(self.hComPort, 4096, 4096)

work just fine on one machine and not the other?

win32file.pyd can't be opened like a .py file, and I don't know what the rather 
cryptic error 'Incorrect function.' is trying to tell me. Does anyone who is 
familiar with win32file have an idea what the problem is?

Thanks for the help!

   -- Ron



More information about the Python-list mailing list