别再死记硬背了!用Python脚本自动化测试EC20模块的AT指令(附完整代码)
用Python脚本自动化测试EC20模块的AT指令实战指南每次调试EC20模块时在串口工具里反复输入AT指令的场景是否让你感到疲惫作为一名长期与物联网模组打交道的开发者我深刻理解手动测试的低效与痛苦——敲错一个字符就得重来测试不同网络状态需要反复插拔SIM卡批量验证功能时更是耗时耗力。本文将分享如何用Python的serial库构建自动化测试方案让你从此告别重复劳动。1. 环境搭建与基础连接在开始编写自动化脚本前需要准备以下硬件和软件环境硬件准备EC20模块开发板带USB接口支持4G网络的SIM卡USB转串口线如CP2102可选外接天线增强信号稳定性Python库安装pip install pyserial pip install pytest # 用于后续自动化测试框架连接EC20模块后首先需要确认设备节点。在Linux系统下通常为/dev/ttyUSB0或/dev/ttyACM0Windows下则为COM3等端口。可以通过以下代码快速检测可用串口import serial.tools.list_ports ports serial.tools.list_ports.comports() for port in ports: print(port.device)建立基础连接时需要注意三个关键参数波特率默认115200、超时时间和流控设置。以下是推荐的基础连接配置import serial ser serial.Serial( port/dev/ttyUSB0, baudrate115200, timeout1, # 秒 rtsctsFalse, # 禁用硬件流控 dsrdtrFalse # 禁用DTR流控 )提示如果遇到连接问题尝试在EC20模块上执行ATIPR?确认当前波特率或使用ATIFC0,0关闭流控。2. AT指令交互封装实战直接使用串口发送原始指令不仅容易出错也难以维护。我们需要构建一个健壮的指令交互层包含以下核心功能自动添加\r\n结束符统一处理模块响应支持超时重试机制错误代码解析2.1 基础指令发送函数def send_at_command(ser, command, expected_responseOK, timeout3, retries3): original_timeout ser.timeout ser.timeout timeout for attempt in range(retries): ser.write(f{command}\r\n.encode()) response b while True: line ser.readline() if not line: break response line decoded response.decode().strip() if expected_response in decoded: return decoded raise Exception(fCommand {command} failed after {retries} retries)2.2 常用指令封装示例将高频使用的AT指令封装成语义化的函数大幅提升代码可读性def get_signal_quality(ser): response send_at_command(ser, ATCSQ) # 典型响应CSQ: 24,99 parts response.split(:)[1].strip().split(,) return {rssi: int(parts[0]), ber: int(parts[1])} def check_network_registration(ser): response send_at_command(ser, ATCREG?) # 响应示例CREG: 2,1,3747,A23C2,100 status int(response.split(,)[1]) return { 0: 未注册, 1: 已注册, 2: 正在注册, 3: 注册被拒绝 }.get(status, 未知状态)2.3 错误处理增强EC20模块支持通过ATCMEE2开启详细错误模式这对调试至关重要def enable_verbose_errors(ser): send_at_command(ser, ATCMEE2) def get_sim_info(ser): try: iccid send_at_command(ser, ATQCCID) imei send_at_command(ser, ATGSN) return {iccid: iccid.split(:)[1].strip(), imei: imei.strip()} except Exception as e: print(f获取SIM卡信息失败: {str(e)}) return None3. 构建自动化测试框架当需要批量验证模块功能时一个结构化的测试框架能显著提升效率。我们基于pytest构建可扩展的测试套件。3.1 测试用例设计创建test_ec20.py文件包含核心功能测试import pytest pytest.fixture(scopemodule) def ec20_connection(): ser serial.Serial(/dev/ttyUSB0, 115200, timeout1) yield ser ser.close() def test_signal_quality(ec20_connection): quality get_signal_quality(ec20_connection) assert 0 quality[rssi] 31, 信号强度值异常 def test_network_registration(ec20_connection): status check_network_registration(ec20_connection) assert status 已注册, f网络注册失败: {status} def test_sim_card(ec20_connection): info get_sim_info(ec20_connection) assert info[iccid] and len(info[iccid]) 20, ICCID无效 assert info[imei] and len(info[imei]) 15, IMEI无效3.2 参数化测试使用pytest的参数化功能批量测试不同配置pytest.mark.parametrize(baudrate, [9600, 19200, 38400, 57600, 115200]) def test_baudrate_change(ec20_connection, baudrate): send_at_command(ec20_connection, fATIPR{baudrate}) send_at_command(ec20_connection, ATW) # 保存设置 # 需要重新初始化连接 new_ser serial.Serial(/dev/ttyUSB0, baudrate, timeout1) assert new_ser.baudrate baudrate new_ser.close()3.3 测试报告生成添加以下命令生成美观的HTML报告pytest --htmlreport.html --self-contained-html4. 高级应用与性能优化当基础功能稳定后可以进一步优化测试效率和扩展性。4.1 并发测试策略使用多线程同时测试多个功能模块from threading import Thread def concurrent_test(): results {} def test_func(target, name): results[name] target() threads [ Thread(targettest_func, args(lambda: get_signal_quality(ser), signal)), Thread(targettest_func, args(lambda: check_network_registration(ser), network)), Thread(targettest_func, args(lambda: get_sim_info(ser), sim)) ] for t in threads: t.start() for t in threads: t.join() return results4.2 响应时间分析通过装饰器记录关键操作的执行时间import time from functools import wraps def timing(func): wraps(func) def wrapper(*args, **kwargs): start time.time() result func(*args, **kwargs) end time.time() print(f{func.__name__} executed in {end-start:.3f}s) return result return wrapper timing def extended_test_sequence(ser): send_at_command(ser, ATCSQ) send_at_command(ser, ATCREG?) send_at_command(ser, ATGSN)4.3 自动化测试CI集成将测试脚本集成到Jenkins或GitHub Actions中实现持续验证# .github/workflows/test.yml 示例 name: EC20 Module Test on: [push, pull_request] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkoutv2 - name: Set up Python uses: actions/setup-pythonv2 with: python-version: 3.9 - name: Install dependencies run: | python -m pip install --upgrade pip pip install pyserial pytest pytest-html - name: Run tests run: | python -m pytest test_ec20.py --htmlreport.html --self-contained-html - name: Upload report uses: actions/upload-artifactv2 with: name: test-report path: report.html在实际项目中这套自动化测试方案将测试时间从原来手动操作的30分钟缩短到2分钟以内且避免了人为错误。一个特别有用的技巧是在测试前自动执行ATCMEE2开启详细错误模式这让我们快速定位到一处波特率配置不匹配的问题节省了大量调试时间。