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 presentation.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()))

        try:
            self._overlappedRead = win32.OVERLAPPED()
            self._overlappedRead.hEvent = win32.CreateEvent(None, 1, 0, None)
            self._overlappedWrite = win32.OVERLAPPED()
            #~ self._overlappedWrite.hEvent = win32.CreateEvent(None, 1, 0, None)
            self._overlappedWrite.hEvent = win32.CreateEvent(None, 0, 0, None)

            # Setup a 4k buffer
            win32.SetupComm(self.hComPort, 4096, 4096)

            # Save original timeout values:
            self._orgTimeouts = win32.COMMTIMEOUTS()
            win32.GetCommTimeouts(self.hComPort, ctypes.byref(self._orgTimeouts))

            self._reconfigurePort()

            # Clear buffers:
            # Remove anything that was there
            win32.PurgeComm(self.hComPort,
                    win32.PURGE_TXCLEAR | win32.PURGE_TXABORT |
                    win32.PURGE_RXCLEAR | win32.PURGE_RXABORT)
        except:
            try:
                self._close()
            except:
                # ignore any exception when closing the port
                # also to keep original exception that happened when setting up
                pass
            self.hComPort = None
            raise
        else:
            self._isOpen = True
Esempio n. 2
0
    def open(self):
        if self._port is None:
            print("ERROR: Port must be configured before it can be used.")
            exit(1)
        if self.is_open:
            print("ERROR: Port is already opened.")
            exit(1)
        port = self.name
        try:
            if port.upper().startswith('COM') and int(port[3:]) > 8:
                port = '\\\\.\\' + port
        except ValueError:
            pass
        self._port_handle = 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)
        """
			Bad COM port
		"""
        if self._port_handle == win32.INVALID_HANDLE_VALUE:
            self._port_handle = None
            print("ERROR: Could not open port {}".format(self.port))
            exit(1)

        try:
            self._overlapped_read = win32.OVERLAPPED()
            self._overlapped_read.hEvent = win32.CreateEvent(None, 1, 0, None)
            self._overlapped_write = win32.OVERLAPPED()
            self._overlapped_write.hEvent = win32.CreateEvent(None, 0, 0, None)

            # Setup a 4k buffer
            win32.SetupComm(self._port_handle, 4096, 4096)

            # Save original timeout values:
            self._orgTimeouts = win32.COMMTIMEOUTS()
            win32.GetCommTimeouts(self._port_handle,
                                  ctypes.byref(self._orgTimeouts))

            self._reconfigure_port()

            win32.PurgeComm(
                self._port_handle, win32.PURGE_TXCLEAR | win32.PURGE_TXABORT
                | win32.PURGE_RXCLEAR | win32.PURGE_RXABORT)
        except:
            try:
                self._close()
            except:
                # ignore any exception when closing the port
                # also to keep original exception that happened when setting up
                pass
            self._port_handle = None
            raise
        else:
            self.is_open = True
Esempio n. 3
0
    def _open(self):
        dos_drive_path = self._drive
        ddk_drive_path = "\\\\.\\%s" % (self._drive,)

        # Check drive type
        drive_type = GetDriveType(dos_drive_path)
        if drive_type not in (DRIVE_REMOVABLE, DRIVE_CDROM):
            raise WindowsSCSIInterfaceException("Drive type is incorrect.")

        self._port_handle = win32.CreateFile(
            ddk_drive_path,
            win32.GENERIC_WRITE | win32.GENERIC_READ,
            FILE_SHARE_READ | FILE_SHARE_WRITE,
            None,
            win32.OPEN_EXISTING,
            win32.FILE_ATTRIBUTE_NORMAL,  # | win32.FILE_FLAG_OVERLAPPED,
            0,
        )

        if self._port_handle == win32.INVALID_HANDLE_VALUE:
            self._port_handle = None
            raise WindowsSCSIInterfaceException("Could not open drive.")
Esempio n. 4
0
        # 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.name
        try:
        		if isinstance(port, int):
							port = '\\\\.\\COM%d' % (port+1)
            elif port.upper().startswith('COM') and int(port[3:]) > 8:
                port = '\\\\.\\' + port

        except ValueError:
            # for like COMnotanumber
            pass
        self._port_handle = 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._port_handle == win32.INVALID_HANDLE_VALUE:
            self._port_handle = None    # 'cause __del__ is called anyway
            raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))

        try:
            self._overlapped_read = win32.OVERLAPPED()
            self._overlapped_read.hEvent = win32.CreateEvent(None, 1, 0, None)
            self._overlapped_write = win32.OVERLAPPED()
            #~ self._overlapped_write.hEvent = win32.CreateEvent(None, 1, 0, None)
            self._overlapped_write.hEvent = win32.CreateEvent(None, 0, 0, None)

            # Setup a 4k buffer