# 28.2.USB 虚拟串行端口

## 28.2.1. 配置 USB 设备模式串行端口

虚拟串行端口支持由模板 3、8 和 10 提供。需要注意的是，模板 3 可以在不需要特殊驱动程序和 INF 文件的情况下与 Microsoft Windows 10 配合使用。其他主机操作系统与所有三个模板兼容。必须加载 [usb\_template(4)](https://man.freebsd.org/cgi/man.cgi?query=usb_template\&sektion=4\&format=html) 和 [umodem(4)](https://man.freebsd.org/cgi/man.cgi?query=umodem\&sektion=4\&format=html) 内核模块。

要启用 USB 设备模式串行端口，请在 **/etc/ttys** 文件中添加以下行：

```sh
ttyU0	"/usr/libexec/getty 3wire"	vt100	onifconsole secure
ttyU1	"/usr/libexec/getty 3wire"	vt100	onifconsole secure
```

然后在 **/etc/devd.conf** 中添加以下行：

```ini
notify 100 {
	match "system"		"DEVFS";
	match "subsystem"	"CDEV";
	match "type"		"CREATE";
	match "cdev"		"ttyU[0-9]+";
	action "/sbin/init q";
};
```

如果 [devd(8)](https://man.freebsd.org/cgi/man.cgi?query=devd\&sektion=8\&format=html) 已经在运行，则重新加载配置：

```sh
# service devd restart
```

确保必要的模块已加载，并且在启动时设置了正确的模板，可以在 **/boot/loader.conf** 中添加以下行（如果该文件不存在，请创建它）：

```ini
umodem_load="YES"
hw.usb.template=3
```

要在不重启的情况下加载模块并设置模板，请使用：

```sh
# kldload umodem
# sysctl hw.usb.template=3
```

## 28.2.2. 从 FreeBSD 连接到 USB 设备模式串行端口

要连接到配置为提供 USB 设备模式串行端口的板子，请将 USB 主机（如笔记本电脑）连接到板子的 USB OTG 或 USB 客户端端口。使用 `pstat -t` 命令在主机上列出终端线路。在列表的末尾，你应该会看到一个 USB 串行端口，例如 "ttyU0"。要打开连接，请使用：

```sh
# cu -l /dev/ttyU0
```

按下几次 Enter 键后，你将看到登录提示。

## 28.2.3. 从 macOS 连接到 USB 设备模式串行端口

要连接到配置为提供 USB 设备模式串行端口的板子，请将 USB 主机（如笔记本电脑）连接到板子的 USB OTG 或 USB 客户端端口。要打开连接，请使用：

```sh
# cu -l /dev/cu.usbmodemFreeBSD1
```

## 28.2.4. 从 Linux 连接到 USB 设备模式串行端口

要连接到配置为提供 USB 设备模式串行端口的板子，请将 USB 主机（如笔记本电脑）连接到板子的 USB OTG 或 USB 客户端端口。要打开连接，请使用：

```sh
# minicom -D /dev/ttyACM0
```

## 28.2.5. 从 Microsoft Windows 10 连接到 USB 设备模式串行端口

要连接到配置为提供 USB 设备模式串行端口的板子，请将 USB 主机（如笔记本电脑）连接到板子的 USB OTG 或 USB 客户端端口。要打开连接，你需要一个串行终端程序，如 PuTTY。要检查 Windows 使用的 COM 端口名称，请运行设备管理器，展开 "端口 (COM & LPT)"。你将看到类似 "USB Serial Device (COM4)" 的名称。运行你选择的串行终端程序，例如 PuTTY。在 PuTTY 对话框中，将 "连接类型" 设置为 "串行"，在 "串行线路" 对话框中输入从设备管理器获得的 COMx 名称，然后点击 "打开"。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://book.bsdcn.org/hanbook/di-28-zhang-usb-she-bei-mo-shi-usb-otg/28.2.-usb-xu-ni-chuan-hang-duan-kou.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
