5

I have seen simple code in stackoverflow using pyserial in USB ports with Python 3.3 but I can't get this to work on my new installation of pyserial 2.7 [in Windows 7, 64 bit, with 3 USB ports]. Installation of pyserial went smoothly, I can import without error and methods are recognized in the Pyscripter IDE which boosts confidence in a good installation, however:

The code stripped down to its error producing essentials is:

import serial
def main():
  ser = serial.Serial(port='COM2')
  ser.close()

if __name__ == '__main__':
   main

From this I receive a dialog box with the error "SerialException: could not open port 'COM2': FileNotFoundError(2,'The system cannot find the file specified.',None,2)"

The Traceback states:

*** Remote Interpreter Reinitialized  ***
>>>
Traceback (most recent call last):
  File "<string>", line 420, in run_nodebug
  File "C:\Python33\Lib\site-packages\scanport2.py", line 19, in <module>
main()
  File "C:\Python33\Lib\site-packages\scanport2.py", line 15, in main
ser = serial.Serial(port='COM2')
  File "C:\Python33\Lib\site-packages\serial\serialwin32.py", line 38, in __init__
SerialBase.__init__(self, *args, **kwargs)
  File "C:\Python33\Lib\site-packages\serial\serialutil.py", line 282, in __init__
self.open()
  File "C:\Python33\Lib\site-packages\serial\serialwin32.py", line 66, in open
raise SerialException("could not open port %r: %r" % (self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM2': FileNotFoundError(2, 'The system cannot find the file specified.', None, 2)

And the code segment in the imported module which raises the SerialException is:

    # the "\\.\COMx" format is required for devices other than COM1-COM8
    # not all versions of windows seem to support this properly
    # so that the first few ports are used with the DOS device name
    port = self.portstr
    try:
        if port.upper().startswith('COM') and int(port[3:]) > 8:
            port = '\\\\.\\' + port
    except ValueError:
        # for like COMnotanumber
        pass
    self.hComPort = win32.CreateFile(port,
           win32.GENERIC_READ | win32.GENERIC_WRITE,
           0, # exclusive access
           None, # no security
           win32.OPEN_EXISTING,
           win32.FILE_ATTRIBUTE_NORMAL | win32.FILE_FLAG_OVERLAPPED,
           0)
    if self.hComPort == win32.INVALID_HANDLE_VALUE:
        self.hComPort = None    # 'cause __del__ is called anyway
        raise SerialException("could not open port %r: %r" % (self.portstr, ctypes.WinError()))

I do have an active device connected to COM2 as identified in the Windows device manager. I also have tried scanning all the ports, but the code stops on the first use of serial.Serial

This appears that something may be going on with win32?

I am a newbie for interfacing Python with hardware.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
user3153016
  • 59
  • 1
  • 1
  • 3
  • I'm not familiar with Windows' privileges but have you tried running your code as administrator? – skytreader Jan 03 '14 at 07:14
  • I found that using the `\\.\COMx` format was necessary for port `COM8` – Adam.at.Epsilon Oct 19 '15 at 14:48
  • do a "pip freeze" at the command prompt for each system. if you have different major versions of pyserial that may be the root cause. The com port references changed from 2.x to 3.x (was integer, now string), i.e., if your new installation has pyserial 2.x, you will need to call "ser = serial.Serial(port=2)" – grambo Sep 25 '17 at 16:45

2 Answers2

1

I would try the following:

  • Unplug and replug the device.
  • Reboot.
  • Run WinObj and look in the GLOBAL?? folder; you should see COM2 there as a symbolic link to something more driver-specific.
  • What type of device do you have connected to COM2? If it uses usbser.sys, you might have better luck substituting \\.\USBSER000 for COM2 in your code, but remember to escape those backslashes properly.
  • On some machines there are strange problems with low COM port numbers that I can't explain. Try reassigning the device to COM6 in the Device Manager.
David Grayson
  • 84,103
  • 24
  • 152
  • 189
  • Does pyserial work with gpsfeed+? gpsfeed+ generate data on COM1, 4800. But I get "serialutil.SerialException: [Errno 2] could not open port COM1, 4800: [Errno 2] No such file or directory: 'COM1, 4800'" – Dr.jacky Mar 11 '17 at 18:45
0

It looks like the pyserial download page only contains links for 32 bit python? This unofficial page seems to have links for 64 bit installations, however be cautious installing from unknown sources.

This answer also suggests installing it using pip: https://stackoverflow.com/a/8491164/66349

Community
  • 1
  • 1
Peter Gibson
  • 19,086
  • 7
  • 60
  • 64