0

I'm writing a program that is supposed to communicate with an Arduino-unit using the serial-object. Within the init-method of a class this piece of code can be found:

    try:
        self.rotor  = serial.Serial(port = "COM22", baudrate=115200, timeout = 0.1, writeTimeout = 1)
    except serial.SerialException, e:
        print "Error when connecting to collimator: ", e

When I run it I get this error message:

SerialException: could not open port 'COM1': WindowsError(2, 'The system cannot find the file specified.')

I asked the computer to open COM22, and it responds that it cannot open COM1. What's that about? The Arduino-unit is plugged into COM22.

I have another program that I haven't written myself, but which utilizes the same class library. This program works, but I don't understand how. Is there some sort of initialization of the serial-object that I have missed to do?

1
  • The error might be happening somewhere else. Your try catch would still catch that error if the error was happening here in the code. Commented Oct 7, 2014 at 11:58

2 Answers 2

2

From the source code for the Win32Serial object in the PySerial SVN trunk (http://svn.code.sf.net/p/pyserial/code/trunk/pyserial/serial/serialwin32.py):

def open(self):
    """\
    Open port with current settings. This may throw a SerialException
    if the port cannot be opened.
    """
    if self._port is None:
        raise SerialException("Port must be configured before it can be used.")
    if self._isOpen:
        raise SerialException("Port is already open.")
    # 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

So changing your code to:

    try:
        self.rotor  = serial.Serial(port = r"\\.\COM22", baudrate=115200, timeout = 0.1, writeTimeout = 1)
    except serial.SerialException, e:
        print "Error when connecting to collimator: ", e

Should work correctly.

Sign up to request clarification or add additional context in comments.

Comments

0

I later found the error to stem from a wrongly defined path to the module with the class defintion. The path was directed to an older version of the same file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.