Files
sms_forwarding/PLATFORM.md
T
wuxu a37344253b feat: 添加Windows COM口支持和跨平台说明
- 支持Windows COM1/COM2/COM3等串口格式
- 支持数字串口号自动转换(如: 3 -> COM3)
- 新增PLATFORM.md跨平台使用说明文档
- 更新配置示例为Windows友好的COM3
- 包含Windows和Linux的串口查看、权限配置等详细说明
2026-06-23 17:53:38 +08:00

223 lines
4.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 跨平台支持说明
本项目支持在 **Windows** 和 **Linux** 上运行。
## 串口配置
### Windows
Windows 使用 `COMx` 格式:
```json
{
"serial": {
"path": "COM3",
"baudRate": 115200
}
}
```
也可以直接写数字,程序会自动添加 `COM` 前缀:
```json
{
"serial": {
"path": "3",
"baudRate": 115200
}
}
```
**查看可用串口**:
```powershell
# 方法1: 设备管理器
devmgmt.msc
# 方法2: PowerShell
Get-WmiObject Win32_SerialPort | Select-Object Name, DeviceID
# 方法3: 使用 mode 命令
mode
```
### Linux
Linux 使用 `/dev/ttyUSBx` 或 `/dev/ttyACMx` 格式:
```json
{
"serial": {
"path": "/dev/ttyUSB0",
"baudRate": 115200
}
}
```
**查看可用串口**:
```bash
# 查看所有USB串口设备
ls -l /dev/ttyUSB*
ls -l /dev/ttyACM*
# 查看详细信息
dmesg | grep tty
# 使用 lsusb 查看USB设备
lsusb
```
**权限配置**(Linux必需):
```bash
# 将当前用户添加到 dialout 组
sudo usermod -a -G dialout $USER
# 注销并重新登录后生效,或临时授权
sudo chmod 666 /dev/ttyUSB0
```
## 平台差异
### 文件路径
- Windows: 使用反斜杠 `\` 或正斜杠 `/` (Node.js会自动处理)
- Linux: 使用正斜杠 `/`
本项目使用 Node.js 的 `path` 模块,会自动处理平台差异。
### PM2 守护进程
Windows 和 Linux 都支持 PM2,但启动脚本略有不同:
**Windows**:
```powershell
pm2 start ecosystem.config.js
pm2 save
pm2 startup
# 按提示执行生成的命令(需要管理员权限)
```
**Linux**:
```bash
pm2 start ecosystem.config.js
pm2 save
pm2 startup
# 按提示执行生成的命令(使用 sudo)
```
### 日志目录
两个平台都会在项目根目录下创建 `logs/` 目录:
- Windows: `.\logs\`
- Linux: `./logs/`
## 测试串口连接
可以使用以下工具测试串口是否正常:
### Windows
- **PuTTY**: https://www.putty.org/
- **Tera Term**: https://ttssh2.osdn.jp/
- **串口调试助手**
### Linux
- **minicom**: `sudo apt install minicom && sudo minicom -D /dev/ttyUSB0 -b 115200`
- **screen**: `sudo screen /dev/ttyUSB0 115200`
- **picocom**: `sudo apt install picocom && sudo picocom /dev/ttyUSB0 -b 115200`
测试方法:
1. 打开串口工具,连接到对应COM口
2. 发送 `AT` 命令
3. 应该收到 `OK` 响应
## 常见问题
### Windows
**Q: 提示串口被占用**
```
Error: Port is already open
```
A: 检查是否有其他程序(如Arduino IDE、串口调试助手)占用了该端口
**Q: 找不到COM口**
A:
1. 检查设备管理器中4G模块是否正确识别
2. 确认驱动是否已安装
3. 尝试拔插USB重新识别
**Q: 权限不足**
A: Windows通常不需要特殊权限,如果遇到问题,尝试以管理员身份运行
### Linux
**Q: 权限不足**
```
Error: Opening COM1: Permission denied
```
A:
```bash
sudo usermod -a -G dialout $USER
# 注销重新登录
```
**Q: 串口不存在**
```
Error: No such file or directory, cannot open /dev/ttyUSB0
```
A:
```bash
# 检查设备是否识别
dmesg | tail -20
lsusb
# 可能是 ttyACM0 而不是 ttyUSB0
ls /dev/tty*
```
**Q: ModemManager占用串口**
A: 某些Linux发行版的ModemManager会自动占用4G模块
```bash
# 临时停用
sudo systemctl stop ModemManager
# 永久禁用(不推荐,可能影响系统网络功能)
sudo systemctl disable ModemManager
```
## 开发环境
### Node.js 版本
- 推荐: Node.js 18.x LTS 或更高
- 最低: Node.js 16.x
### 依赖库
所有依赖都是跨平台的:
- `serialport` - 底层使用 C++ 绑定,支持 Windows/Linux/macOS
- `express` - Web框架
- `nodemailer` - 邮件发送
- `axios` - HTTP客户端
- `winston` - 日志库
## 性能建议
### Windows
- 关闭Windows Defender实时扫描对 `node_modules/` 的监控
- 使用SSD存储项目文件
### Linux
- 使用 `noatime` 挂载选项减少磁盘IO
- 调整串口缓冲区大小(如有需要):
```bash
setserial /dev/ttyUSB0 low_latency
```
## 部署建议
### Windows Server
- 使用 PM2 作为守护进程
- 配置 Windows 防火墙允许 API 端口(默认3000)
- 设置 PM2 开机自启动
### Linux Server
- 使用 PM2 + systemd
- 配置 UFW/iptables 防火墙
- 考虑使用 Nginx 反向代理