You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过串口向OHAUS天平发送命令失败问题求助

OHAUS Explorer EX124/AD天平串口通信问题:发送命令无响应

问题概况

  • 设备连接:OHAUS Explorer EX124/AD天平通过RS232转USB适配器连接树莓派5或Windows 10 PC,两台设备表现一致
  • 通信现状:能正常接收天平自动打印或手动按打印键发送的数据,但发送控制命令时完全无响应:
    • opendoor()函数执行后,天平自动门无动作
    • ip()函数尝试获取当前读数,返回空消息
  • 已排查项:波特率匹配、消息编码正确、未遗漏\r\n换行符,串口连接正常(无报错)
  • 命令来源:天平手册EN-151页9.4.1节,核心需求命令为IP(获取读数)和WI 1 1(打开自动门)

用户提供的Python代码如下:

import serial
from time import sleep
import re

# Connecting to the balance - this works
try: 
    #ser = serial.Serial('COM3', # Works with Windows
    ser = serial.Serial('/dev/ttyUSB0', # For use with Raspberry Pi - also works
    baudrate=9600,
    parity=serial.PARITY_NONE, 
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    timeout = 1)
except Exception as scaleserror:
    print("Unable to connect to balance:\n", scaleserror) # I do not get this message


def autoprint(): # This works as expected when the balance is autoprinting, or the print button is pressed on the balance - as expected
    ser.flushInput()
    ser.flushOutput()
    weight = 0
    sleep(0.1)
    
    while weight == 0:
        x = ser.readline()
        if str(x)[2] == " ": # Interpreting the output of the balance
            weight = re.findall(r'\b\d+[.,]?\d+\b', str(x))
            return weight

def opendoor(): # This doesn't do anything
    sleep(0.1)
    ser.flushInput()
    ser.flushOutput()
    ser.write('WI 0 1\r\n'.encode()) # Three variations, none work, nothing happens
    ser.write('WI11\r\n'.encode())
    ser.write(b'WI11\r\n')
    
def ip(): # This doesn't work - returns blank
    sleep(0.1)
    ser.flushInput()
    ser.flushOutput()
    
    ser.write('IP\r\n'.encode())
    sleep(0.1)
    returnMsg = ser.readline()
    print(returnMsg) # Returned message is blank (b' ')

opendoor()
ip()

排查方向与解决方案

1. 确认天平的命令接收模式是否开启

部分天平默认关闭远程命令接收功能,需要在天平的系统菜单中手动启用“远程控制”或“串口命令响应”模式,否则会直接忽略外部发送的命令。查看手册中串口功能的配置章节,完成模式开启操作。

2. 严格匹配命令格式

  • 手册中WI命令的标准格式为WI 1 1(参数间必须有空格),代码中同时发送多种格式(如WI11)会造成通信混乱,保留唯一符合手册要求的命令即可。
  • 尝试替换换行符:有些设备仅识别\r或\n,可将\r\n改为\r单独测试。

3. 添加流控设置

RS232设备常需要硬件流控才能正常接收命令,在串口初始化时添加rtscts=True参数,同时确认天平端的流控设置与PC/树莓派一致。

4. 优化通信时序与读取逻辑

  • 发送命令后的等待时间过短(sleep(0.1)),天平响应需要更长处理时间,改为sleep(0.5)或更长。
  • 移除发送命令前的flushInput()和flushOutput(),避免清空天平的响应缓冲区或打乱通信时序。
  • 用ser.read_all()代替ser.readline(),避免因响应的换行符格式不匹配导致读取失败。

5. 用串口调试工具验证

使用Windows的Putty、树莓派的minicom等工具手动发送IP\r\n和WI 1 1\r\n,如果工具中也无响应,说明问题出在设备设置或硬件;如果工具中正常响应,再回到Python代码排查逻辑问题。

调整后的示例代码

import serial
from time import sleep
import re

# 初始化串口 - 添加流控并延长超时
try: 
    # ser = serial.Serial('COM3', 
    ser = serial.Serial('/dev/ttyUSB0',
        baudrate=9600,
        parity=serial.PARITY_NONE, 
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS,
        timeout = 2,
        rtscts=True
    )
except Exception as scaleserror:
    print("无法连接天平:\n", scaleserror)

def autoprint():
    ser.flushInput()
    weight = 0
    while weight == 0:
        x = ser.readline()
        # 更安全的响应判断方式,避免字符串转换问题
        if x and len(x) > 2 and chr(x[1]) == " ":
            weight = re.findall(r'\b\d+[.,]?\d+\b', x.decode('ascii'))
            return weight

def opendoor():
    # 仅发送手册指定的正确格式命令
    cmd = 'WI 1 1\r\n'.encode('ascii')
    ser.write(cmd)
    sleep(0.5)

def ip():
    ser.write('IP\r\n'.encode('ascii'))
    sleep(0.5)
    returnMsg = ser.read_all()
    print("响应内容:", returnMsg)
    if returnMsg:
        return returnMsg.decode('ascii')
    return None

# 测试执行
opendoor()
ip()

内容的提问来源于stack exchange,提问作者user23509042

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 12:02:05