close_port
Closes an active serial port connection to free system resources and terminate data transmission. Specify the port path to disconnect.
Instructions
关闭指定串口连接
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| port | Yes | 串口路径 |
Implementation Reference
- src/uart_mcp/tools/port_ops.py:67-77 (handler)The primary handler function for the MCP 'close_port' tool. It invokes the serial manager's close_port method to perform the actual closure.def close_port(port: str) -> dict[str, Any]: """关闭串口 Args: port: 串口路径 Returns: 操作结果 """ manager = get_serial_manager() return manager.close_port(port)
- The JSON schema definition for the 'close_port' tool's input parameters, used during MCP tool registration.CLOSE_PORT_TOOL: dict[str, Any] = { "name": "close_port", "description": "关闭指定串口连接", "inputSchema": { "type": "object", "properties": { "port": { "type": "string", "description": "串口路径", }, }, "required": ["port"], }, }
- src/uart_mcp/server.py:77-81 (registration)Registers the 'close_port' tool in the MCP server's list_tools() handler by creating a Tool object with its schema.types.Tool( name=CLOSE_PORT_TOOL["name"], description=CLOSE_PORT_TOOL["description"], inputSchema=CLOSE_PORT_TOOL["inputSchema"], ),
- src/uart_mcp/server.py:152-153 (handler)Dispatch logic in the MCP call_tool handler that routes 'close_port' calls to the specific close_port function.elif name == "close_port": result = close_port(**arguments)
- Low-level helper method in SerialManager that performs the actual serial port closure, handles locking, error cases, and returns success status.def close_port(self, port: str) -> dict[str, Any]: """关闭串口 Args: port: 串口路径 Returns: 操作结果 Raises: PortClosedError: 串口未打开 """ with self._lock: if port not in self._ports: raise PortClosedError(port) managed = self._ports.pop(port) try: managed.serial.close() except Exception as e: logger.warning("关闭串口时发生异常:%s - %s", port, e) logger.info("串口关闭成功:%s", port) return {"success": True, "port": port}