我在 Raspberry Pi 4 上使用 Ubuntu 20.04 我正在通过 USB 从另一个设备接收数据,该 USB 启动为串口,使用 ftdi_sio 和 udev 规则:
ACTION=="add", \
ATTRS{idVendor}=="0eb8", \
ATTRS{idProduct}=="ea02", \
RUN+="/usr/sbin/modprobe ftdi_sio" \
RUN+="/usr/bin/sh -c 'echo 0eb8 ea02 > /sys/bus/usb-serial/drivers/ftdi_sio/new_id'"
ACTION=="remove", \
ATTRS{idVendor}=="0eb8", \
ATTRS{idProduct}=="ea02", \
RUN+="/usr/bin/sh -c 'echo usb-Mettler-Toledo_Seven_Excellence-if00-port0 > /sys/bus/usb/drivers/ftdi_sio/unbind'"
我在 Windows 上使用 YAT 或 minicom 等少数终端进行了测试,它运行良好,数据格式良好。但在linux上却失败了。我发现每 62 个字符就会丢失 2 个字符。也尝试过直接用screen软件代替python。还验证了电源,我用的是官方的raspi4充电器。
无论我使用什么软件读取串口(screen、minicom、python 等),都会发生同样的错误
尝试了 dsrdtr、rtscts 和 xonxoff 的许多配置。从发送数据的其他设备来看,规则可能是波特率 9600、数据位 8、无奇偶校验、启用 xonxoff。 (不确定,我在设备的另一个功能上找到了这些参数)
数据样本,因此每 62 个字符,丢失 2 个:
b'{misses '<?'}xml version="1.0" encoding="utf-8" standalone="yes" ?><Telegra{misses 'm '}xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http:/{misses '/w'}ww.w3.org/2001/XMLSchema-instance" xmlns="LancePlatform"><EndO{misses 'fM'}ethod><m_timestamp>2023-10-24T16:20:35.0000</m_timestamp><m_eA{misses 'na'}lysisStatus>1</m_eAnalysisStatus><m_uiJobId>2</m_uiJobId><m_us{misses 'er'}></m_user></EndOfMethod></Telegram>\r\n'
更新
在 dmesg 中,找到了一些有用的日志,但在论坛上找不到真正的答案。
[ 7150.279471] ftdi_sio ttyUSB0: error from flowcontrol urb
[ 7151.045999] ftdi_sio ttyUSB0: ftdi_set_termios FAILED to set databits/stopbits/parity
[ 7151.054621] ftdi_sio ttyUSB0: ftdi_set_termios urb failed to set baudrate
[ 7151.062250] ftdi_sio ttyUSB0: failed to set flow control: -32
[ 7151.069459] ftdi_sio ttyUSB0: failed to get modem status: -32
[ 7151.075976] ftdi_sio ttyUSB0: error from flowcontrol urb
[ 7258.303870] ftdi_sio ttyUSB0: ftdi_set_termios FAILED to set databits/stopbits/parity
[ 7258.312491] ftdi_sio ttyUSB0: ftdi_set_termios urb failed to set baudrate
[ 7258.320253] ftdi_sio ttyUSB0: failed to set flow control: -32
[ 7258.327452] ftdi_sio ttyUSB0: failed to get modem status: -32
[ 7258.333835] ftdi_sio ttyUSB0: ftdi_set_termios urb failed to set baudrate
[ 7258.341355] ftdi_sio ttyUSB0: failed to set flow control: -32
[ 7258.347832] ftdi_sio ttyUSB0: failed to get modem status: -32
[ 7264.638935] ttyUSB ttyUSB0: 1 input overrun(s)
[ 7268.853097] ttyUSB ttyUSB0: 32 input overrun(s)
[ 7275.768053] ttyUSB ttyUSB0: 4 input overrun(s)
[ 7323.578412] ftdi_sio ttyUSB0: failed to get modem status: -32
[ 8465.312963] ftdi_sio ttyUSB0: ftdi_set_termios FAILED to set databits/stopbits/parity
[ 8465.321780] ftdi_sio ttyUSB0: ftdi_set_termios urb failed to set baudrate
[ 8465.329355] ftdi_sio ttyUSB0: failed to set flow control: -32
[ 8465.336715] ftdi_sio ttyUSB0: failed to get modem status: -32
[ 8465.343213] ftdi_sio ttyUSB0: failed to set flow control: -32
[ 8465.349976] ftdi_sio ttyUSB0: failed to get modem status: -32
[ 8465.359343] ftdi_sio ttyUSB0: ftdi_set_termios FAILED to set databits/stopbits/parity
[ 8465.367975] ftdi_sio ttyUSB0: ftdi_set_termios urb failed to set baudrate
[ 8465.375468] ftdi_sio ttyUSB0: failed to set flow control: -32
[ 8538.369178] ftdi_sio ttyUSB0: failed to get modem status: -32
[ 8564.307661] ftdi_sio ttyUSB0: ftdi_set_termios FAILED to set databits/stopbits/parity
[ 8564.316412] ftdi_sio ttyUSB0: ftdi_set_termios urb failed to set baudrate
[ 8564.324032] ftdi_sio ttyUSB0: failed to set flow control: -32
[ 8564.331160] ftdi_sio ttyUSB0: failed to get modem status: -32
[ 8564.337661] ftdi_sio ttyUSB0: failed to set flow control: -32
[ 8564.344415] ftdi_sio ttyUSB0: failed to get modem status: -32
[ 8564.353790] ftdi_sio ttyUSB0: ftdi_set_termios FAILED to set databits/stopbits/parity
[ 8564.362411] ftdi_sio ttyUSB0: ftdi_set_termios urb failed to
set baudrate
[ 8564.369917] ftdi_sio ttyUSB0: failed to set flow control: -32
[ 8586.585832] ftdi_sio ttyUSB0: failed to get modem status: -32
答案1
该器件与 USB CDC Class 标准兼容。您可以直接使用“通用”串行设备。在rpi3、Debian bookworm上测试,可以打开端口并正确读取设备信息报文,响应约1.8kByte
ACTION=="add", \
ATTRS{idVendor}=="0eb8", \
ATTRS{idProduct}=="ea02", \
RUN+="/usr/sbin/modprobe usbserial" \
RUN+="/bin/sh -c 'echo 0eb8 ea02 > /sys/bus/usb-serial/drivers/generic/new_id'"
日志
[ 6303.984478] usb 1-1.3: new full-speed USB device number 11 using dwc2
[ 6304.095741] usb 1-1.3: not running at top speed; connect to a high speed hub
[ 6304.116616] usb 1-1.3: New USB device found, idVendor=0eb8, idProduct=ea02, bcdDevice= 0.00
[ 6304.129901] usb 1-1.3: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 6304.139771] usb 1-1.3: Product: Seven Excellence
[ 6304.146830] usb 1-1.3: Manufacturer: Mettler-Toledo
[ 6304.154119] usb 1-1.3: SerialNumber: A000000000
[ 6304.162207] usbserial_generic 1-1.3:1.0: The "generic" usb-serial driver is only for testing and one-off prototypes.
[ 6304.175985] usbserial_generic 1-1.3:1.0: Tell [email protected] to add your device to a proper driver.
[ 6304.189463] usbserial_generic 1-1.3:1.0: generic converter detected
[ 6304.198137] usb 1-1.3: generic converter now attached to ttyUSB0
答案2
对于任何有同样问题的人。最后是驱动问题,ftdi_sio不适合这个设备,不知道为什么,也不知道怎么办。
这是有效的驱动程序: https://github.com/pyusb/pyusb。
它迫使我使用 python,但至少它有效。