close_port
Close an active serial port connection to free system resources and prevent unauthorized access to UART devices.
Instructions
关闭指定串口连接
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| port | Yes | 串口路径 |
Implementation Reference
- src/uart_mcp/tools/port_ops.py:67-78 (handler)The main handler function for the 'close_port' tool. It retrieves the serial manager and calls its close_port method, returning the result as a dict.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 input, defining the required 'port' parameter.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)Registration of the 'close_port' tool in the MCP server's list_tools handler.types.Tool( name=CLOSE_PORT_TOOL["name"], description=CLOSE_PORT_TOOL["description"], inputSchema=CLOSE_PORT_TOOL["inputSchema"], ),
- src/uart_mcp/server.py:152-153 (registration)Dispatch logic in the MCP server's call_tool handler that invokes the close_port function.elif name == "close_port": result = close_port(**arguments)
- Core implementation in SerialManager that performs the actual port closing logic, removing the port from the managed ports and closing the serial connection.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}