串行在启动后不久就停止工作

问题描述 投票:0回答:0

我正在使用运行 Mendel Linux(Debian 的衍生物)的 Coral Dev Board Mini 在启动时运行 python 脚本。

为了测试,我在 root 用户下添加一个 crontab @reboot

sudo crontab -e
或 systemctl 服务,调用
nohup script -c "sudo python3 main.py" -f log.txt
.

在 python 脚本上,我运行

from serial import Serial

serial = Serial("/dev/ttyS0", 1000000)
while True:
    try:
        serial.write(b"\x00")
        print("It worked")
    except:
        print("It didn't work :(")

如果我检查

log.txt
,我会看到它在停止工作之前工作了一段时间(~40 行)。如果我删除 try except,我会得到

Thread-5:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 537, in write
    n = os.write(self.fd, d)
OSError: [Errno 5] Input/output error

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
    self.run()
  File "/home/mendel/.../main.py", line 110, in main
    serial.write(b"\x00")
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 571, in write
    raise SerialException('write failed: {}'.format(e))
serial.serialutil.SerialException: write failed: [Errno 5] Input/output error

我不认为这是权限问题?我用相同的方法检查了 python run 并且我拥有所有相关权限:

print(os.access("/dev/ttyS0", os.R_OK))  # True
print(os.access("/dev/ttyS0", os.W_OK))  # True
print(os.access("/dev/ttyS0", os.X_OK))  # False
print(os.access("/dev/ttyS0", os.F_OK))  # True
$ ls -l /dev/ttyS0
crw--w---- 1 root tty 4, 64 Apr  7 19:28 /dev/ttyS0

有没有人知道为什么我的序列号在启动后一段时间就停止工作了?我真的想不出任何东西。如果能得到任何帮助,我将不胜感激。

python-3.x serial-port pyserial
© www.soinside.com 2019 - 2024. All rights reserved.