Skip to main content
Glama

read_data

Reads data from an open serial port in text or binary mode for communication with UART devices.

Instructions

从已打开的串口读取数据,支持文本和二进制模式

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
portYes串口路径,如 /dev/ttyUSB0 或 COM1
sizeNo读取字节数,不指定则读取所有可用数据
timeout_msNo读取超时(毫秒),不指定则使用串口配置的超时
is_binaryNo是否为二进制模式,True 时返回 Base64 编码

Implementation Reference

  • Core implementation of the read_data tool: reads raw data from serial port via manager, encodes as base64 for binary or UTF-8 text, returns dict with data and bytes_read.
    def read_data( port: str, size: int | None = None, timeout_ms: int | None = None, is_binary: bool = False, ) -> dict[str, Any]: """从串口读取数据 Args: port: 串口路径 size: 读取字节数,None 表示读取所有可用数据 timeout_ms: 读取超时(毫秒),None 使用串口配置的超时 is_binary: 是否为二进制模式 Returns: 读取结果,包含数据和字节数 """ manager = get_serial_manager() raw_data = manager.read_data(port, size, timeout_ms) # 解码转换 if is_binary: result_data = base64.b64encode(raw_data).decode("ascii") else: result_data = raw_data.decode("utf-8", errors="replace") return {"data": result_data, "bytes_read": len(raw_data)}
  • JSON schema definition for the read_data tool, used for MCP tool registration, specifying input parameters: port (required), size, timeout_ms, is_binary.
    READ_DATA_TOOL: dict[str, Any] = { "name": "read_data", "description": "从已打开的串口读取数据,支持文本和二进制模式", "inputSchema": { "type": "object", "properties": { "port": { "type": "string", "description": "串口路径,如 /dev/ttyUSB0 或 COM1", }, "size": { "type": "integer", "description": "读取字节数,不指定则读取所有可用数据", }, "timeout_ms": { "type": "integer", "description": "读取超时(毫秒),不指定则使用串口配置的超时", }, "is_binary": { "type": "boolean", "description": "是否为二进制模式,True 时返回 Base64 编码", "default": False, }, }, "required": ["port"], }, }
  • Tool registration in MCP list_tools handler: includes read_data tool with its name, description, and inputSchema from READ_DATA_TOOL.
    types.Tool( name=READ_DATA_TOOL["name"], description=READ_DATA_TOOL["description"], inputSchema=READ_DATA_TOOL["inputSchema"], ),
  • Dispatch logic in MCP call_tool handler: calls the read_data function with unpacked arguments when tool name is 'read_data'.
    elif name == "read_data": result = read_data(**arguments)

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/donnel666/uart-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server