几分钟/几小时后无法从串行端口写入/读取。错误消息:stty:标准输入:设备的 ioctl 不合适

几分钟/几小时后无法从串行端口写入/读取。错误消息:stty:标准输入:设备的 ioctl 不合适

我正在尝试同时从串行端口写入和读取数据。这样做的目的是通过连接到串行端口的 DIO 卡打开和关闭设备。在进行这些开/关测试时,我还需要从连接到 DIO 卡的光传感器收集一些数据。

通常,前几分钟/几小时一切都正常。我可以向 DIO 卡发送命令,命令会得到执行,同时我还可以读取光传感器数据。不幸的是,几分钟/几小时后它就停止工作了,我根本无法通过端口发送任何命令。

串口设置:

我使用以下串行端口设置:

sudo stty -F /dev/ttyUSB0 115200 cs8 -cstopb -parenb -echo

当我尝试在终端发送命令时收到以下错误消息:

bash: /dev/ttyUSB0: Permission denied

当我尝试通过读取串行端口设置时stty < /dev/ttyUSB0 command,收到以下错误消息:

stty: standard input: Inappropriate ioctl for device

我还注意到,当发生此错误时,串行端口设置会被更改。我在终端中输入以下命令时注意到了这一点:

ls -la /dev/ttyUSB0

当一切正常时我得到以下输出:

crw-rw---- 1 root dialout 188, 0 Nov  8 11:12 /dev/ttyUSB0

错误发生后,我得到类似这样的信息(我忘记保存确切的输出了,抱歉):

rw-rw-rw- 1 root root 8, 0 Nov  8 11:12 /dev/ttyUSB0

缺少文件描述符,所以这可能是我之后无法发送任何命令的原因......


到目前为止我已经尝试过:

到目前为止,我只尝试更改串行端口设置。我基本上从一个能够从串行端口写入和读取的程序中复制了设置。不幸的是,问题仍然存在。

我也知道相关问题。

https://unix.stackexchange.com/questions/157852/echo-test-stty-echo-stty-standard-input-inappropriate-ioctl-for-device

https://unix.stackexchange.com/questions/82658/bash-script-error-stty-standard-input-inappropriate-ioctl-for-device


-echo感谢这些帖子,我得出结论,问题与stty 命令中的运算符有关,我目前正在运行没有此运算符的测试,它似乎正在工作,尽管现在下结论还为时过早。但是,由于没有这个-echo运算符,从光传感器收集的数据无法正确显示,我想让它与运算符一起工作,但我就是不知道该怎么做。

所以我的问题是:有什么办法可以让它与-echo操作员一起工作吗?

以下是这些脚本中的逻辑的概述。

  1. Main_Script.sh
  2. Light_Sensors.sh
  3. Test_Scenario.sh

  1. Main_Script.sh

    #!/bin/bash
    sudo stty -F /dev/ttyUSB0 115200 cs8 -cstopb -parenb -echo
    gnome-terminal -e ./Light_Sensor.sh
    for ((countOuter=1;countOuter<=$testRunsOuter;countOuter++))
    do
        for ((countInner=1;countInner<=$testRunsInner;countInner++))
        do
            ./Test_Scenario.sh
        done
    done
    
  2. Light_Sensor.sh

    #!/bin/bash
    while read -r line < /dev/ttyUSB0; do
        timestamp=$(date +%T.%3N)
        echo "$timestamp || $line" | sed -E 's/(DO[0-9]=[0-9];|ADC5.+ADC8=[0-9]*mV;)//g' >> $devicesFolder/lightSensor_log.txt
        echo $line
        idOfMainScript=$(ps -aux | grep -E "\/bin\/bash.+Main_Script\.sh")
        if [[ -z $idOfMainScript ]]; then
            break
        fi
    done
    printf "adc-loop;" > /dev/ttyUSB0
    
  3. Test_Scenario.sh

    #!/bin/bash
    printf "DO1=1;" > /dev/ttyUSB0
    sleep 50
    printf "DO1=0;" > /dev/ttyUSB0
    

编辑1: 所以昨天我尝试在没有-echo操作员的情况下运行测试,结果成功了!根据@user.dz的建议,我也尝试在udevadm monitor -u启用命令的情况下使用操作员运行测试,并且还存储了cat /proc/devices发生错误前后的命令输出。以下是输出:

udevadm monitor -u最近 2-3 次测试运行的命令输出,包括发生错误的运行:

UDEV  [100869.423382] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3 (usb)
UDEV  [100869.425837] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0 (usb)
UDEV  [100869.426233] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.1 (usb)
UDEV  [100869.426394] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.2 (usb)
UDEV  [100869.461100] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/enp0s20u3u3 (net)
UDEV  [100869.462613] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/usb0/queues/tx-0 (queues)
UDEV  [100869.462891] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/usb0/queues/rx-0 (queues)
UDEV  [100869.463766] move     /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/enp0s20u3u3 (net)
UDEV  [100889.037286] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/enp0s20u3u3/queues/rx-0 (queues)
UDEV  [100889.037463] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/enp0s20u3u3/queues/tx-0 (queues)
UDEV  [100889.042298] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/enp0s20u3u3 (net)
UDEV  [100889.057916] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0 (usb)
UDEV  [100889.058325] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.1 (usb)
UDEV  [100889.058743] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.2 (usb)
UDEV  [100889.060074] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3 (usb)
UDEV  [100923.807331] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3 (usb)
UDEV  [100923.810610] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0 (usb)
UDEV  [100923.811127] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.1 (usb)
UDEV  [100923.811365] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.2 (usb)
UDEV  [100923.864802] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/enp0s20u3u3 (net)
UDEV  [100923.865748] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/usb0/queues/tx-0 (queues)
UDEV  [100923.865774] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/usb0/queues/rx-0 (queues)
UDEV  [100923.866450] move     /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/enp0s20u3u3 (net)
UDEV  [100964.705261] remove   /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4/1-1.4.4/1-1.4.4.4/1-1.4.4.4:1.0/ttyUSB0/tty/ttyUSB0 (tty)
UDEV  [100964.706493] remove   /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4/1-1.4.4/1-1.4.4.4/1-1.4.4.4:1.0/ttyUSB0 (usb-serial)
UDEV  [100964.707476] remove   /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4/1-1.4.4/1-1.4.4.4/1-1.4.4.4:1.0 (usb)
UDEV  [100964.708629] remove   /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4/1-1.4.4/1-1.4.4.4 (usb)
UDEV  [100965.052742] add      /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4/1-1.4.4/1-1.4.4.4 (usb)
UDEV  [100966.060907] add      /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4/1-1.4.4/1-1.4.4.4/1-1.4.4.4:1.0 (usb)
UDEV  [100966.062528] add      /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4/1-1.4.4/1-1.4.4.4/1-1.4.4.4:1.0/ttyUSB0 (usb-serial)
UDEV  [100966.068074] add      /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4/1-1.4.4/1-1.4.4.4/1-1.4.4.4:1.0/ttyUSB0/tty/ttyUSB0 (tty)
UDEV  [101423.034335] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/enp0s20u3u3/queues/rx-0 (queues)
UDEV  [101423.034684] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/enp0s20u3u3/queues/tx-0 (queues)
UDEV  [101423.039793] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/enp0s20u3u3 (net)
UDEV  [101423.074013] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0 (usb)
UDEV  [101423.074736] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.1 (usb)
UDEV  [101423.075057] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.2 (usb)
UDEV  [101423.076590] remove   /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3 (usb)
UDEV  [101423.383684] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3 (usb)
UDEV  [101423.386304] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0 (usb)
UDEV  [101423.386653] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.1 (usb)
UDEV  [101423.388152] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.2 (usb)
UDEV  [101423.412796] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/enp0s20u3u3 (net)
UDEV  [101423.413835] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/usb0/queues/tx-0 (queues)
UDEV  [101423.414278] add      /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/usb0/queues/rx-0 (queues)
UDEV  [101423.414689] move     /devices/pci0000:00/0000:00:14.0/usb4/4-3/4-3.3/4-3.3:1.0/net/enp0s20u3u3 (net)

cat /proc/devices命令输出(错误发生之前):

Character devices:
  1 mem
  4 /dev/vc/0
  4 tty
  4 ttyS
  5 /dev/tty
  5 /dev/console
  5 /dev/ptmx
  5 ttyprintk
  6 lp
  7 vcs
    ....
166 ttyACM
180 usb
188 ttyUSB
189 usb_device
204 ttyMAX
    ...
254 gpiochip

Block devices:
  7 loop
  8 sd
  9 md
    ....
259 blkext

cat /proc/devices命令输出(发生错误后):

Character devices:
  1 mem
  4 /dev/vc/0
  4 tty
  4 ttyS
  5 /dev/tty
  5 /dev/console
  5 /dev/ptmx
  5 ttyprintk
  6 lp
  7 vcs
    ....
166 ttyACM
180 usb
188 ttyUSB
189 usb_device
204 ttyMAX
    ....
254 gpiochip

Block devices:
  7 loop
  8 sd
  9 md
    ....
259 blkext

这次我还存储了命令的输出ls -la /dev/ttyUSB0

-rw-r--r-- 1 root root 6 Nov  9 17:59 /dev/ttyUSB0

相关内容