首页 > 解决方案 > 拔下 USB 后,使用串口的脚本能否继续运行?

问题描述

我有一个差压压力计,通过 USB 连接到树莓派 3 上,用于数据记录,我已成功完成。

我想要完成的是,当我移除 USB 时,代码将进入睡眠模式,直到重新连接。

如果我的代码有点幼稚直截了当,我很抱歉,但我是一名物理学家,我对优化代码等知之甚少。我在这里错过了什么大事吗?

import serial
import time
import os.path

while True:

        if (os.path.exists('/dev/ttyUSB0') == True):
                print ("USB connected")
                time.sleep(1)
                # setup
                pceport = serial.Serial(port = '/dev/ttyUSB0',
                        baudrate = 9600,
                        parity = serial.PARITY_NONE,
                        bytesize = serial.EIGHTBITS,
                        writeTimeout = 0,
                        timeout = 1)

                # command words
                pceport.write([0x55, 0xaa, 0x01]) # connect to serial port to receive data
                print ("Start reading...")
                time.sleep(1)

                # read from the serial port
                while (pceport.isOpen() == True):
                        if (pceport.isOpen() == True):
                                pceport.read(5) # 2 ID bytes, Sign, Voltage and Unit skipped
                        else:
                                pceport.close()
                        if (pceport.isOpen() == True):
                                data = pceport.read(5) # Data bytes (5)
                        else:
                                pceport.close()
                        if (pceport.isOpen() == True):
                                pceport.read(1) # Checkcode byte skipped
                        else:
                                pceport.close()

                        print (data.decode('utf-8'), "mbar")

        else:
                print ("USB disconnected")
                time.sleep(2)
                print ("Trying to reconnect...")
                time.sleep(8)

Pi的终端:

pi@raspberrypi:~ $ python3 pcetest.py
USB connected
Start reading...
0.182 mbar
0.139 mbar
0.185 mbar
0.245 mbar
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 490, in read
    'device reports readiness to read but returned no data '
serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected or multiple access on port?)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "pcetest.py", line 26, in <module>
    pceport.read(5) # 2 ID bytes, Sign, Voltage and Unit skipped
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 497, in read
    raise SerialException('read failed: {}'.format(e))
serial.serialutil.SerialException: read failed: device reports readiness to read but returned no data (device disconnected or multiple access on port?)

这是拔掉USB时的错误。我希望它退出读取循环,返回顶部并进入 USB 断开循环,但事实并非如此。

当它被拔掉并插入时,它可以完全正常工作:

pi@raspberrypi:~ $ python3 pcetest.py
USB disconnected
Trying to reconnect...
USB connected
Start reading...
0.260 mbar
0.228 mbar
0.242 mbar
0.230 mbar

抱歉发了这么长的帖子,感谢任何评论

标签: python-3.xraspberry-pi3

解决方案


推荐阅读