open_port
Configure and establish a serial port connection for UART communication by specifying port path, baud rate, data bits, parity, stop bits, flow control, and timeout settings.
Instructions
打开指定串口,支持自定义配置参数
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| port | Yes | 串口路径,如 /dev/ttyUSB0 或 COM1 | |
| baudrate | No | 波特率,支持的值:[300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 57600, 115200, 230400, 460800, 921600] | |
| bytesize | No | 数据位,支持的值:[5, 6, 7, 8] | |
| parity | No | 校验位,支持的值:['N', 'E', 'O', 'M', 'S'] | N |
| stopbits | No | 停止位,支持的值:[1.0, 1.5, 2.0] | |
| flow_control | No | 流控制,支持的值:['none', 'hardware', 'software'] | none |
| read_timeout_ms | No | 读取超时(毫秒),范围 0-60000 | |
| write_timeout_ms | No | 写入超时(毫秒),范围 0-60000 | |
| auto_reconnect | No | 是否启用自动重连 |
Implementation Reference
- src/uart_mcp/tools/port_ops.py:25-65 (handler)The open_port function implements the core logic of the tool by invoking the serial manager to open the specified port with given configurations and returning the status dictionary.def open_port( port: str, baudrate: int = DEFAULT_BAUDRATE, bytesize: int = DEFAULT_BYTESIZE, parity: str = DEFAULT_PARITY.value, stopbits: float = float(DEFAULT_STOPBITS.value), flow_control: str = DEFAULT_FLOW_CONTROL.value, read_timeout_ms: int = DEFAULT_TIMEOUT_MS, write_timeout_ms: int = DEFAULT_WRITE_TIMEOUT_MS, auto_reconnect: bool = True, ) -> dict[str, Any]: """打开串口 Args: port: 串口路径(如 /dev/ttyUSB0 或 COM1) baudrate: 波特率,默认 9600 bytesize: 数据位,默认 8 parity: 校验位,默认 N(无校验) stopbits: 停止位,默认 1 flow_control: 流控制,默认 none read_timeout_ms: 读取超时(毫秒),默认 1000 write_timeout_ms: 写入超时(毫秒),默认 1000 auto_reconnect: 是否启用自动重连,默认 True Returns: 串口状态信息 """ manager = get_serial_manager() status = manager.open_port( port=port, baudrate=baudrate, bytesize=bytesize, parity=parity, stopbits=stopbits, flow_control=flow_control, read_timeout_ms=read_timeout_ms, write_timeout_ms=write_timeout_ms, auto_reconnect=auto_reconnect, ) return status.to_dict()
- The input schema (JSON Schema) defining the parameters, types, descriptions, defaults, and requirements for the open_port tool.OPEN_PORT_TOOL: dict[str, Any] = { "name": "open_port", "description": "打开指定串口,支持自定义配置参数", "inputSchema": { "type": "object", "properties": { "port": { "type": "string", "description": "串口路径,如 /dev/ttyUSB0 或 COM1", }, "baudrate": { "type": "integer", "description": f"波特率,支持的值:{list(SUPPORTED_BAUDRATES)}", "default": DEFAULT_BAUDRATE, }, "bytesize": { "type": "integer", "description": f"数据位,支持的值:{list(SUPPORTED_BYTESIZES)}", "default": DEFAULT_BYTESIZE, }, "parity": { "type": "string", "description": f"校验位,支持的值:{[p.value for p in Parity]}", "default": DEFAULT_PARITY.value, }, "stopbits": { "type": "number", "description": f"停止位,支持的值:{[s.value for s in StopBits]}", "default": float(DEFAULT_STOPBITS.value), }, "flow_control": { "type": "string", "description": f"流控制,支持的值:{[f.value for f in FlowControl]}", "default": DEFAULT_FLOW_CONTROL.value, }, "read_timeout_ms": { "type": "integer", "description": "读取超时(毫秒),范围 0-60000", "default": DEFAULT_TIMEOUT_MS, }, "write_timeout_ms": { "type": "integer", "description": "写入超时(毫秒),范围 0-60000", "default": DEFAULT_WRITE_TIMEOUT_MS, }, "auto_reconnect": { "type": "boolean", "description": "是否启用自动重连", "default": True, }, }, "required": ["port"], }, }
- src/uart_mcp/server.py:72-76 (registration)Registration of the open_port tool in the MCP server's list_tools() handler, including name, description, and schema.types.Tool( name=OPEN_PORT_TOOL["name"], description=OPEN_PORT_TOOL["description"], inputSchema=OPEN_PORT_TOOL["inputSchema"], ),
- src/uart_mcp/server.py:150-151 (registration)Dispatch to the open_port handler in the MCP server's call_tool() handler.elif name == "open_port": result = open_port(**arguments)